don't warn for fully qual inherent methods

But do continue to warn for trait methods.
This commit is contained in:
Niko Matsakis 2021-06-14 13:17:15 -04:00
parent 19ba219e99
commit 8d42f3da63
4 changed files with 51 additions and 1 deletions

View File

@ -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.

View File

@ -0,0 +1,16 @@
// check-pass
// run-rustfix
// edition 2018
trait MyTrait<A> {
fn from_iter(x: Option<A>);
}
impl<T> MyTrait<()> for Vec<T> {
fn from_iter(_: Option<()>) {}
}
fn main() {
<Vec<i32> as MyTrait<_>>::from_iter(None);
//~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
}

View File

@ -0,0 +1,16 @@
// check-pass
// run-rustfix
// edition 2018
trait MyTrait<A> {
fn from_iter(x: Option<A>);
}
impl<T> MyTrait<()> for Vec<T> {
fn from_iter(_: Option<()>) {}
}
fn main() {
<Vec<i32>>::from_iter(None);
//~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
}

View File

@ -0,0 +1,10 @@
warning: trait-associated function `from_iter` will become ambiguous in Rust 2021
--> $DIR/generic-type-collision.rs:14:5
|
LL | <Vec<i32>>::from_iter(None);
| ^^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `<Vec<i32> as MyTrait<_>>::from_iter`
|
= note: `#[warn(future_prelude_collision)]` on by default
warning: 1 warning emitted