diff --git a/compiler/rustc_typeck/src/check/method/mod.rs b/compiler/rustc_typeck/src/check/method/mod.rs index 4a855d6e408..f321d62edf6 100644 --- a/compiler/rustc_typeck/src/check/method/mod.rs +++ b/compiler/rustc_typeck/src/check/method/mod.rs @@ -10,6 +10,7 @@ pub use self::suggest::{SelfSource, TraitInfo}; pub use self::CandidateSource::*; pub use self::MethodError::*; +use crate::check::method::probe::PickKind; use crate::check::FnCtxt; use rustc_ast::ast::Mutability; use rustc_data_structures::sync::Lrc; @@ -552,7 +553,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if span.edition() < Edition::Edition2021 { if let sym::try_into | sym::try_from | sym::from_iter = method_name.name { - if !matches!(tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core) { + // No need to warn if either: + // + // * The method comes from std/core, since ten it's the built-in trait. + // * This is an inherent method called on a specific type, like `Vec::foo(...)`, + // since such methods take precedence over trait methods. + if !matches!(tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core) + && !matches!(pick.kind, PickKind::InherentImplPick) + { tcx.struct_span_lint_hir(FUTURE_PRELUDE_COLLISION, expr_id, span, |lint| { // "type" refers to either a type or, more likely, a trait from which // the associated function or method is from. diff --git a/src/test/ui/rust-2021/generic-type-collision.fixed b/src/test/ui/rust-2021/generic-type-collision.fixed new file mode 100644 index 00000000000..1ae2b95d515 --- /dev/null +++ b/src/test/ui/rust-2021/generic-type-collision.fixed @@ -0,0 +1,16 @@ +// check-pass +// run-rustfix +// edition 2018 + +trait MyTrait { + fn from_iter(x: Option); +} + +impl MyTrait<()> for Vec { + fn from_iter(_: Option<()>) {} +} + +fn main() { + as MyTrait<_>>::from_iter(None); + //~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021 +} diff --git a/src/test/ui/rust-2021/generic-type-collision.rs b/src/test/ui/rust-2021/generic-type-collision.rs new file mode 100644 index 00000000000..e203656163c --- /dev/null +++ b/src/test/ui/rust-2021/generic-type-collision.rs @@ -0,0 +1,16 @@ +// check-pass +// run-rustfix +// edition 2018 + +trait MyTrait { + fn from_iter(x: Option); +} + +impl MyTrait<()> for Vec { + fn from_iter(_: Option<()>) {} +} + +fn main() { + >::from_iter(None); + //~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021 +} diff --git a/src/test/ui/rust-2021/generic-type-collision.stderr b/src/test/ui/rust-2021/generic-type-collision.stderr new file mode 100644 index 00000000000..3acc6185a61 --- /dev/null +++ b/src/test/ui/rust-2021/generic-type-collision.stderr @@ -0,0 +1,10 @@ +warning: trait-associated function `from_iter` will become ambiguous in Rust 2021 + --> $DIR/generic-type-collision.rs:14:5 + | +LL | >::from_iter(None); + | ^^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: ` as MyTrait<_>>::from_iter` + | + = note: `#[warn(future_prelude_collision)]` on by default + +warning: 1 warning emitted +