Skip to content

Commit ed60662

Browse files
Auto merge of #147995 - petrochenkov:gamtinsc, r=<try>
resolve: Do not consider traits from ambiguous imports to be in scope
2 parents f5e2df7 + 2a03c50 commit ed60662

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ impl<'ra> Module<'ra> {
703703
if traits.is_none() {
704704
let mut collected_traits = Vec::new();
705705
self.for_each_child(resolver, |r, name, ns, binding| {
706-
if ns != TypeNS {
706+
if ns != TypeNS || binding.is_ambiguity_recursive() {
707707
return;
708708
}
709709
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
mod m1 {
2+
pub trait Trait {
3+
fn method1(&self) {}
4+
}
5+
impl Trait for u8 {}
6+
}
7+
mod m2 {
8+
pub trait Trait {
9+
fn method2(&self) {}
10+
}
11+
impl Trait for u8 {}
12+
}
13+
14+
fn test1() {
15+
// Create an ambiguous import for `Trait` in one order
16+
use m1::*;
17+
use m2::*;
18+
0u8.method1(); //~ ERROR no method named `method1` found for type `u8` in the current scope
19+
0u8.method2(); //~ ERROR no method named `method2` found for type `u8` in the current scope
20+
}
21+
22+
fn test2() {
23+
// Create an ambiguous import for `Trait` in another order
24+
use m2::*;
25+
use m1::*;
26+
0u8.method1(); //~ ERROR no method named `method1` found for type `u8` in the current scope
27+
0u8.method2(); //~ ERROR no method named `method2` found for type `u8` in the current scope
28+
}
29+
30+
fn main() {}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
error[E0599]: no method named `method1` found for type `u8` in the current scope
2+
--> $DIR/ambiguous-trait-in-scope.rs:18:9
3+
|
4+
LL | fn method1(&self) {}
5+
| ------- the method is available for `u8` here
6+
...
7+
LL | 0u8.method1();
8+
| ^^^^^^^
9+
|
10+
= help: items from traits can only be used if the trait is in scope
11+
help: trait `Trait` which provides `method1` is implemented but not in scope; perhaps you want to import it
12+
|
13+
LL + use m1::Trait;
14+
|
15+
help: there is a method `method2` with a similar name
16+
|
17+
LL - 0u8.method1();
18+
LL + 0u8.method2();
19+
|
20+
21+
error[E0599]: no method named `method2` found for type `u8` in the current scope
22+
--> $DIR/ambiguous-trait-in-scope.rs:19:9
23+
|
24+
LL | fn method2(&self) {}
25+
| ------- the method is available for `u8` here
26+
...
27+
LL | 0u8.method2();
28+
| ^^^^^^^
29+
|
30+
= help: items from traits can only be used if the trait is in scope
31+
help: trait `Trait` which provides `method2` is implemented but not in scope; perhaps you want to import it
32+
|
33+
LL + use m2::Trait;
34+
|
35+
help: there is a method `method1` with a similar name
36+
|
37+
LL - 0u8.method2();
38+
LL + 0u8.method1();
39+
|
40+
41+
error[E0599]: no method named `method1` found for type `u8` in the current scope
42+
--> $DIR/ambiguous-trait-in-scope.rs:26:9
43+
|
44+
LL | fn method1(&self) {}
45+
| ------- the method is available for `u8` here
46+
...
47+
LL | 0u8.method1();
48+
| ^^^^^^^
49+
|
50+
= help: items from traits can only be used if the trait is in scope
51+
help: trait `Trait` which provides `method1` is implemented but not in scope; perhaps you want to import it
52+
|
53+
LL + use m1::Trait;
54+
|
55+
help: there is a method `method2` with a similar name
56+
|
57+
LL - 0u8.method1();
58+
LL + 0u8.method2();
59+
|
60+
61+
error[E0599]: no method named `method2` found for type `u8` in the current scope
62+
--> $DIR/ambiguous-trait-in-scope.rs:27:9
63+
|
64+
LL | fn method2(&self) {}
65+
| ------- the method is available for `u8` here
66+
...
67+
LL | 0u8.method2();
68+
| ^^^^^^^
69+
|
70+
= help: items from traits can only be used if the trait is in scope
71+
help: trait `Trait` which provides `method2` is implemented but not in scope; perhaps you want to import it
72+
|
73+
LL + use m2::Trait;
74+
|
75+
help: there is a method `method1` with a similar name
76+
|
77+
LL - 0u8.method2();
78+
LL + 0u8.method1();
79+
|
80+
81+
error: aborting due to 4 previous errors
82+
83+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)