Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8d42f3d

Browse files
committed
don't warn for fully qual inherent methods
But do continue to warn for trait methods.
1 parent 19ba219 commit 8d42f3d

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

compiler/rustc_typeck/src/check/method/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use self::suggest::{SelfSource, TraitInfo};
1010
pub use self::CandidateSource::*;
1111
pub use self::MethodError::*;
1212

13+
use crate::check::method::probe::PickKind;
1314
use crate::check::FnCtxt;
1415
use rustc_ast::ast::Mutability;
1516
use rustc_data_structures::sync::Lrc;
@@ -552,7 +553,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
552553

553554
if span.edition() < Edition::Edition2021 {
554555
if let sym::try_into | sym::try_from | sym::from_iter = method_name.name {
555-
if !matches!(tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core) {
556+
// No need to warn if either:
557+
//
558+
// * The method comes from std/core, since ten it's the built-in trait.
559+
// * This is an inherent method called on a specific type, like `Vec::foo(...)`,
560+
// since such methods take precedence over trait methods.
561+
if !matches!(tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core)
562+
&& !matches!(pick.kind, PickKind::InherentImplPick)
563+
{
556564
tcx.struct_span_lint_hir(FUTURE_PRELUDE_COLLISION, expr_id, span, |lint| {
557565
// "type" refers to either a type or, more likely, a trait from which
558566
// the associated function or method is from.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
// run-rustfix
3+
// edition 2018
4+
5+
trait MyTrait<A> {
6+
fn from_iter(x: Option<A>);
7+
}
8+
9+
impl<T> MyTrait<()> for Vec<T> {
10+
fn from_iter(_: Option<()>) {}
11+
}
12+
13+
fn main() {
14+
<Vec<i32> as MyTrait<_>>::from_iter(None);
15+
//~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
// run-rustfix
3+
// edition 2018
4+
5+
trait MyTrait<A> {
6+
fn from_iter(x: Option<A>);
7+
}
8+
9+
impl<T> MyTrait<()> for Vec<T> {
10+
fn from_iter(_: Option<()>) {}
11+
}
12+
13+
fn main() {
14+
<Vec<i32>>::from_iter(None);
15+
//~^ WARNING trait-associated function `from_iter` will become ambiguous in Rust 2021
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: trait-associated function `from_iter` will become ambiguous in Rust 2021
2+
--> $DIR/generic-type-collision.rs:14:5
3+
|
4+
LL | <Vec<i32>>::from_iter(None);
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `<Vec<i32> as MyTrait<_>>::from_iter`
6+
|
7+
= note: `#[warn(future_prelude_collision)]` on by default
8+
9+
warning: 1 warning emitted
10+

0 commit comments

Comments
 (0)