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
18 changes: 17 additions & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

Ok(())
})
})?;

if ident.name != kw::Underscore
&& binding.is_glob_import()
&& matches!(binding.res(), Res::Def(DefKind::Trait | DefKind::TraitAlias, _))
{
self.try_define_local(
module,
Ident::new(kw::Underscore, ident.span),
TypeNS,
binding,
false,
)
.expect("tralala");
}

Ok(())
}

fn new_ambiguity_binding(
Expand Down
78 changes: 78 additions & 0 deletions tests/ui/imports/ambiguous-trait-in-scope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//@ check-pass
//@ edition:2018
//@ aux-crate:external=ambiguous-trait-reexport.rs

mod m1 {
pub trait Trait {
fn method1(&self) {}
}
impl Trait for u8 {}
}
mod m2 {
pub trait Trait {
fn method2(&self) {}
}
impl Trait for u8 {}
}
mod m1_reexport {
pub use crate::m1::Trait;
}
mod m2_reexport {
pub use crate::m2::Trait;
}

mod ambig_reexport {
pub use crate::m1::*;
pub use crate::m2::*;
}

fn test1() {
// Create an ambiguous import for `Trait` in one order
use m1::*;
use m2::*;
0u8.method1();
0u8.method2();
}

fn test2() {
// Create an ambiguous import for `Trait` in another order
use m1::*;
use m2::*;
0u8.method1();
0u8.method2();
}

fn test_indirect_reexport() {
use m1_reexport::*;
use m2_reexport::*;
0u8.method1();
0u8.method2();
}

fn test_ambig_reexport() {
use ambig_reexport::*;
0u8.method1();
0u8.method2();
}

fn test_external() {
use external::m1::*;
use external::m2::*;
0u8.method1();
0u8.method2();
}

fn test_external_indirect_reexport() {
use external::m1_reexport::*;
use external::m2_reexport::*;
0u8.method1();
0u8.method2();
}

fn test_external_ambig_reexport() {
use external::ambig_reexport::*;
0u8.method1();
0u8.method2();
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/ui/imports/auxiliary/ambiguous-trait-reexport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pub mod m1 {
pub trait Trait {
fn method1(&self) {}
}
impl Trait for u8 {}
}
pub mod m2 {
pub trait Trait {
fn method2(&self) {}
}
impl Trait for u8 {}
}
pub mod m1_reexport {
pub use crate::m1::Trait;
}
pub mod m2_reexport {
pub use crate::m2::Trait;
}

pub mod ambig_reexport {
pub use crate::m1::*;
pub use crate::m2::*;
}
2 changes: 1 addition & 1 deletion tests/ui/macros/macro-context.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ error[E0412]: cannot find type `i` in this scope
--> $DIR/macro-context.rs:3:13
|
LL | () => ( i ; typeof );
| ^ help: a builtin type with a similar name exists: `i8`
| ^ not found in this scope
...
LL | let a: m!();
| ---- in this macro invocation
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/rust-2021/future-prelude-collision-shadow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ check-pass
//@ edition:2018

#![warn(rust_2021_prelude_collisions)]
#![allow(dead_code)]
#![allow(unused_imports)]
Expand All @@ -22,10 +24,10 @@ mod d {
use crate::m::*;

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

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

error: aborting due to 1 previous error
warning: 1 warning emitted

For more information about this error, try `rustc --explain E0599`.
6 changes: 4 additions & 2 deletions tests/ui/shadowed/shadowed-trait-methods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Test that methods from shadowed traits cannot be used
// Test that methods from shadowed traits can be used

//@ check-pass

mod foo {
pub trait T { fn f(&self) {} }
Expand All @@ -10,5 +12,5 @@ mod bar { pub use crate::foo::T; }
fn main() {
pub use bar::*;
struct T;
().f() //~ ERROR no method
().f() // OK
}
18 changes: 0 additions & 18 deletions tests/ui/shadowed/shadowed-trait-methods.stderr

This file was deleted.

Loading