Skip to content

Commit 9581360

Browse files
Auto merge of #148946 - petrochenkov:underglob, r=<try>
resolve: Consider all traits from ambiguous glob imports to be in scope
2 parents c8551d3 + 4925e53 commit 9581360

File tree

8 files changed

+138
-42
lines changed

8 files changed

+138
-42
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
433433
}
434434

435435
Ok(())
436-
})
436+
})?;
437+
438+
if ident.name != kw::Underscore
439+
&& binding.is_glob_import()
440+
&& matches!(binding.res(), Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
441+
{
442+
self.try_define_local(
443+
module,
444+
Ident::new(kw::Underscore, ident.span),
445+
TypeNS,
446+
binding,
447+
false,
448+
)
449+
.expect("tralala");
450+
}
451+
452+
Ok(())
437453
}
438454

439455
fn new_ambiguity_binding(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//@ check-pass
2+
//@ edition:2018
3+
//@ aux-crate:external=ambiguous-trait-reexport.rs
4+
5+
mod m1 {
6+
pub trait Trait {
7+
fn method1(&self) {}
8+
}
9+
impl Trait for u8 {}
10+
}
11+
mod m2 {
12+
pub trait Trait {
13+
fn method2(&self) {}
14+
}
15+
impl Trait for u8 {}
16+
}
17+
mod m1_reexport {
18+
pub use crate::m1::Trait;
19+
}
20+
mod m2_reexport {
21+
pub use crate::m2::Trait;
22+
}
23+
24+
mod ambig_reexport {
25+
pub use crate::m1::*;
26+
pub use crate::m2::*;
27+
}
28+
29+
fn test1() {
30+
// Create an ambiguous import for `Trait` in one order
31+
use m1::*;
32+
use m2::*;
33+
0u8.method1();
34+
0u8.method2();
35+
}
36+
37+
fn test2() {
38+
// Create an ambiguous import for `Trait` in another order
39+
use m1::*;
40+
use m2::*;
41+
0u8.method1();
42+
0u8.method2();
43+
}
44+
45+
fn test_indirect_reexport() {
46+
use m1_reexport::*;
47+
use m2_reexport::*;
48+
0u8.method1();
49+
0u8.method2();
50+
}
51+
52+
fn test_ambig_reexport() {
53+
use ambig_reexport::*;
54+
0u8.method1();
55+
0u8.method2();
56+
}
57+
58+
fn test_external() {
59+
use external::m1::*;
60+
use external::m2::*;
61+
0u8.method1();
62+
0u8.method2();
63+
}
64+
65+
fn test_external_indirect_reexport() {
66+
use external::m1_reexport::*;
67+
use external::m2_reexport::*;
68+
0u8.method1();
69+
0u8.method2();
70+
}
71+
72+
fn test_external_ambig_reexport() {
73+
use external::ambig_reexport::*;
74+
0u8.method1();
75+
0u8.method2();
76+
}
77+
78+
fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod m1 {
2+
pub trait Trait {
3+
fn method1(&self) {}
4+
}
5+
impl Trait for u8 {}
6+
}
7+
pub mod m2 {
8+
pub trait Trait {
9+
fn method2(&self) {}
10+
}
11+
impl Trait for u8 {}
12+
}
13+
pub mod m1_reexport {
14+
pub use crate::m1::Trait;
15+
}
16+
pub mod m2_reexport {
17+
pub use crate::m2::Trait;
18+
}
19+
20+
pub mod ambig_reexport {
21+
pub use crate::m1::*;
22+
pub use crate::m2::*;
23+
}

tests/ui/macros/macro-context.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ error[E0412]: cannot find type `i` in this scope
4646
--> $DIR/macro-context.rs:3:13
4747
|
4848
LL | () => ( i ; typeof );
49-
| ^ help: a builtin type with a similar name exists: `i8`
49+
| ^ not found in this scope
5050
...
5151
LL | let a: m!();
5252
| ---- in this macro invocation

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)