diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 1636605b234f4..43e145c58ff66 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -703,7 +703,7 @@ impl<'ra> Module<'ra> { if traits.is_none() { let mut collected_traits = Vec::new(); self.for_each_child(resolver, |r, name, ns, binding| { - if ns != TypeNS { + if ns != TypeNS || binding.is_ambiguity_recursive() { return; } if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() { diff --git a/tests/ui/imports/ambiguous-trait-in-scope.rs b/tests/ui/imports/ambiguous-trait-in-scope.rs new file mode 100644 index 0000000000000..ed51d88b82f0b --- /dev/null +++ b/tests/ui/imports/ambiguous-trait-in-scope.rs @@ -0,0 +1,30 @@ +mod m1 { + pub trait Trait { + fn method1(&self) {} + } + impl Trait for u8 {} +} +mod m2 { + pub trait Trait { + fn method2(&self) {} + } + impl Trait for u8 {} +} + +fn test1() { + // Create an ambiguous import for `Trait` in one order + use m1::*; + use m2::*; + 0u8.method1(); //~ ERROR no method named `method1` found for type `u8` in the current scope + 0u8.method2(); //~ ERROR no method named `method2` found for type `u8` in the current scope +} + +fn test2() { + // Create an ambiguous import for `Trait` in another order + use m2::*; + use m1::*; + 0u8.method1(); //~ ERROR no method named `method1` found for type `u8` in the current scope + 0u8.method2(); //~ ERROR no method named `method2` found for type `u8` in the current scope +} + +fn main() {} diff --git a/tests/ui/imports/ambiguous-trait-in-scope.stderr b/tests/ui/imports/ambiguous-trait-in-scope.stderr new file mode 100644 index 0000000000000..f41443270745e --- /dev/null +++ b/tests/ui/imports/ambiguous-trait-in-scope.stderr @@ -0,0 +1,83 @@ +error[E0599]: no method named `method1` found for type `u8` in the current scope + --> $DIR/ambiguous-trait-in-scope.rs:18:9 + | +LL | fn method1(&self) {} + | ------- the method is available for `u8` here +... +LL | 0u8.method1(); + | ^^^^^^^ + | + = help: items from traits can only be used if the trait is in scope +help: trait `Trait` which provides `method1` is implemented but not in scope; perhaps you want to import it + | +LL + use m1::Trait; + | +help: there is a method `method2` with a similar name + | +LL - 0u8.method1(); +LL + 0u8.method2(); + | + +error[E0599]: no method named `method2` found for type `u8` in the current scope + --> $DIR/ambiguous-trait-in-scope.rs:19:9 + | +LL | fn method2(&self) {} + | ------- the method is available for `u8` here +... +LL | 0u8.method2(); + | ^^^^^^^ + | + = help: items from traits can only be used if the trait is in scope +help: trait `Trait` which provides `method2` is implemented but not in scope; perhaps you want to import it + | +LL + use m2::Trait; + | +help: there is a method `method1` with a similar name + | +LL - 0u8.method2(); +LL + 0u8.method1(); + | + +error[E0599]: no method named `method1` found for type `u8` in the current scope + --> $DIR/ambiguous-trait-in-scope.rs:26:9 + | +LL | fn method1(&self) {} + | ------- the method is available for `u8` here +... +LL | 0u8.method1(); + | ^^^^^^^ + | + = help: items from traits can only be used if the trait is in scope +help: trait `Trait` which provides `method1` is implemented but not in scope; perhaps you want to import it + | +LL + use m1::Trait; + | +help: there is a method `method2` with a similar name + | +LL - 0u8.method1(); +LL + 0u8.method2(); + | + +error[E0599]: no method named `method2` found for type `u8` in the current scope + --> $DIR/ambiguous-trait-in-scope.rs:27:9 + | +LL | fn method2(&self) {} + | ------- the method is available for `u8` here +... +LL | 0u8.method2(); + | ^^^^^^^ + | + = help: items from traits can only be used if the trait is in scope +help: trait `Trait` which provides `method2` is implemented but not in scope; perhaps you want to import it + | +LL + use m2::Trait; + | +help: there is a method `method1` with a similar name + | +LL - 0u8.method2(); +LL + 0u8.method1(); + | + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0599`.