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
24 changes: 15 additions & 9 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,18 +702,24 @@ impl<'ra> Module<'ra> {
}

/// This modifies `self` in place. The traits will be stored in `self.traits`.
fn ensure_traits<'tcx>(self, resolver: &impl AsRef<Resolver<'ra, 'tcx>>) {
let mut traits = self.traits.borrow_mut(resolver.as_ref());
fn ensure_traits<'tcx>(self, resolver: &Resolver<'ra, 'tcx>) {
let mut traits = self.traits.borrow_mut(resolver);
if traits.is_none() {
let mut collected_traits = Vec::new();
self.for_each_child(resolver, |r, name, ns, binding| {
if ns != TypeNS {
return;
}
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() {
collected_traits.push((name, binding, r.as_ref().get_module(def_id)))
for (key, entry) in resolver.resolutions(self).borrow().iter() {
if key.ns == TypeNS {
let entry = entry.borrow();
for binding in [entry.non_glob_binding, entry.glob_binding] {
if let Some(binding) = binding
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) =
binding.res()
{
collected_traits.push((key.ident, binding, resolver.get_module(def_id)))
}
}
}
});
}

*traits = Some(collected_traits.into_boxed_slice());
}
}
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