Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too familiar with rustc_resolve - are there be other instances of for_each_child that would benefit from a similar is_ambiguity_recursive check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at the moment, #57199 describes the logic behind these checks, this was the one place missing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do have for_each_child_mut?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do have for_each_child_mut?

Its only use in process_macro_use_imports doesn't need this check either.

return;
}
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() {
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/imports/ambiguous-trait-in-scope.rs
Original file line number Diff line number Diff line change
@@ -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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could you add #[rustfmt::skip] to this and test2, I'd be worried about the test no longer testing the right thing if someone decided to reformat these.

// 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() {}
83 changes: 83 additions & 0 deletions tests/ui/imports/ambiguous-trait-in-scope.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading