don't warn for fully qual inherent methods
But do continue to warn for trait methods.
This commit is contained in:
parent
19ba219e99
commit
8d42f3da63
4 changed files with 51 additions and 1 deletions
|
@ -10,6 +10,7 @@ pub use self::suggest::{SelfSource, TraitInfo};
|
||||||
pub use self::CandidateSource::*;
|
pub use self::CandidateSource::*;
|
||||||
pub use self::MethodError::*;
|
pub use self::MethodError::*;
|
||||||
|
|
||||||
|
use crate::check::method::probe::PickKind;
|
||||||
use crate::check::FnCtxt;
|
use crate::check::FnCtxt;
|
||||||
use rustc_ast::ast::Mutability;
|
use rustc_ast::ast::Mutability;
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::Lrc;
|
||||||
|
@ -552,7 +553,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
|
|
||||||
if span.edition() < Edition::Edition2021 {
|
if span.edition() < Edition::Edition2021 {
|
||||||
if let sym::try_into | sym::try_from | sym::from_iter = method_name.name {
|
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| {
|
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
|
// "type" refers to either a type or, more likely, a trait from which
|
||||||
// the associated function or method is from.
|
// the associated function or method is from.
|
||||||
|
|
16
src/test/ui/rust-2021/generic-type-collision.fixed
Normal file
16
src/test/ui/rust-2021/generic-type-collision.fixed
Normal 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
|
||||||
|
}
|
16
src/test/ui/rust-2021/generic-type-collision.rs
Normal file
16
src/test/ui/rust-2021/generic-type-collision.rs
Normal 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
|
||||||
|
}
|
10
src/test/ui/rust-2021/generic-type-collision.stderr
Normal file
10
src/test/ui/rust-2021/generic-type-collision.stderr
Normal 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue