Skip to content

Commit 213ba58

Browse files
Auto merge of #148943 - petrochenkov:traitglobshadow, r=<try>
resolve: Consider traits from shadowed glob imports to be in scope
2 parents c880acd + 576dc67 commit 213ba58

File tree

5 files changed

+34
-49
lines changed

5 files changed

+34
-49
lines changed

compiler/rustc_resolve/src/lib.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -702,18 +702,24 @@ impl<'ra> Module<'ra> {
702702
}
703703

704704
/// This modifies `self` in place. The traits will be stored in `self.traits`.
705-
fn ensure_traits<'tcx>(self, resolver: &impl AsRef<Resolver<'ra, 'tcx>>) {
706-
let mut traits = self.traits.borrow_mut(resolver.as_ref());
705+
fn ensure_traits<'tcx>(self, resolver: &Resolver<'ra, 'tcx>) {
706+
let mut traits = self.traits.borrow_mut(resolver);
707707
if traits.is_none() {
708708
let mut collected_traits = Vec::new();
709-
self.for_each_child(resolver, |r, name, ns, binding| {
710-
if ns != TypeNS {
711-
return;
712-
}
713-
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() {
714-
collected_traits.push((name, binding, r.as_ref().get_module(def_id)))
709+
for (key, entry) in resolver.resolutions(self).borrow().iter() {
710+
if key.ns == TypeNS {
711+
let entry = entry.borrow();
712+
for binding in [entry.non_glob_binding, entry.glob_binding] {
713+
if let Some(binding) = binding
714+
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) =
715+
binding.res()
716+
{
717+
collected_traits.push((key.ident, binding, resolver.get_module(def_id)))
718+
}
719+
}
715720
}
716-
});
721+
}
722+
717723
*traits = Some(collected_traits.into_boxed_slice());
718724
}
719725
}

tests/ui/rust-2021/future-prelude-collision-shadow.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//@ check-pass
12
//@ edition:2018
3+
24
#![warn(rust_2021_prelude_collisions)]
35
#![allow(dead_code)]
46
#![allow(unused_imports)]
@@ -22,10 +24,10 @@ mod d {
2224
use crate::m::*;
2325

2426
fn main() {
25-
// Here, `TryIntoU32` is imported but shadowed, but in that case we don't permit its methods
26-
// to be available.
27+
// Here, `TryIntoU32` is imported and shadowed, but its methods are still available.
2728
let _: u32 = 3u8.try_into().unwrap();
28-
//~^ ERROR no method named `try_into` found for type `u8` in the current scope
29+
//~^ WARN trait method `try_into` will become ambiguous in Rust 2021
30+
//~| WARN this is accepted in the current edition
2931
}
3032
}
3133

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
error[E0599]: no method named `try_into` found for type `u8` in the current scope
2-
--> $DIR/future-prelude-collision-shadow.rs:27:26
1+
warning: trait method `try_into` will become ambiguous in Rust 2021
2+
--> $DIR/future-prelude-collision-shadow.rs:28:22
33
|
44
LL | let _: u32 = 3u8.try_into().unwrap();
5-
| ^^^^^^^^
5+
| ^^^^^^^^^^^^^^ help: disambiguate the associated function: `TryIntoU32::try_into(3u8)`
66
|
7-
= help: items from traits can only be used if the trait is in scope
8-
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
9-
help: the following traits which provide `try_into` are implemented but not in scope; perhaps you want to import one of them
10-
|
11-
LL + use crate::m::TryIntoU32;
12-
|
13-
LL + use std::convert::TryInto;
14-
|
15-
help: there is a method `into` with a similar name
16-
|
17-
LL - let _: u32 = 3u8.try_into().unwrap();
18-
LL + let _: u32 = 3u8.into().unwrap();
7+
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
8+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/prelude.html>
9+
note: the lint level is defined here
10+
--> $DIR/future-prelude-collision-shadow.rs:4:9
1911
|
12+
LL | #![warn(rust_2021_prelude_collisions)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2014

21-
error: aborting due to 1 previous error
15+
warning: 1 warning emitted
2216

23-
For more information about this error, try `rustc --explain E0599`.

tests/ui/shadowed/shadowed-trait-methods.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Test that methods from shadowed traits cannot be used
1+
// Test that methods from shadowed traits can be used
2+
3+
//@ check-pass
24

35
mod foo {
46
pub trait T { fn f(&self) {} }
@@ -10,5 +12,5 @@ mod bar { pub use crate::foo::T; }
1012
fn main() {
1113
pub use bar::*;
1214
struct T;
13-
().f() //~ ERROR no method
15+
().f() // OK
1416
}

tests/ui/shadowed/shadowed-trait-methods.stderr

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)