Skip to content

Commit

Permalink
Adjust the method ambiguity lint too
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Apr 17, 2024
1 parent bd12444 commit 0aafa34
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 19 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/method-lookup.html

mod confirm;
mod prelude2021;
mod prelude_edition_lints;
pub mod probe;
mod suggest;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
method::probe::{self, Pick},
FnCtxt,
};
use crate::method::probe::{self, Pick};
use crate::FnCtxt;

use hir::def_id::DefId;
use hir::HirId;
use hir::ItemKind;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_infer::infer::type_variable::TypeVariableOrigin;
use rustc_lint::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};
use rustc_middle::ty::{self, Ty};
use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS;
use rustc_span::symbol::kw::{Empty, Underscore};
Expand All @@ -32,22 +32,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
segment.ident, self_ty, call_expr, self_expr
);

// Rust 2021 and later is already using the new prelude
if span.at_least_rust_2021() {
return;
}

let prelude_or_array_lint = match segment.ident.name {
let (prelude_or_array_lint, edition) = match segment.ident.name {
// `try_into` was added to the prelude in Rust 2021.
sym::try_into => RUST_2021_PRELUDE_COLLISIONS,
sym::try_into if !span.at_least_rust_2021() => (RUST_2021_PRELUDE_COLLISIONS, "2021"),
// `into_iter` wasn't added to the prelude,
// but `[T; N].into_iter()` doesn't resolve to IntoIterator::into_iter
// before Rust 2021, which results in the same problem.
// It is only a problem for arrays.
sym::into_iter if let ty::Array(..) = self_ty.kind() => {
// In this case, it wasn't really a prelude addition that was the problem.
// Instead, the problem is that the array-into_iter hack will no longer apply in Rust 2021.
rustc_lint::ARRAY_INTO_ITER
sym::into_iter => {
if let ty::Array(..) = self_ty.kind()
&& !span.at_least_rust_2021()
{
// In this case, it wasn't really a prelude addition that was the problem.
// Instead, the problem is that the array-into_iter hack will no longer apply in Rust 2021.
(ARRAY_INTO_ITER, "2021")
} else if self_ty.is_box()
&& self_ty.boxed_ty().is_slice()
&& !span.at_least_rust_2024()
{
// In this case, it wasn't really a prelude addition that was the problem.
// Instead, the problem is that the array-into_iter hack will no longer apply in Rust 2021.
(BOXED_SLICE_INTO_ITER, "2024")
} else {
return;
}
}
_ => return,
};
Expand Down Expand Up @@ -81,7 +89,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
prelude_or_array_lint,
self_expr.hir_id,
self_expr.span,
format!("trait method `{}` will become ambiguous in Rust 2021", segment.ident.name),
format!(
"trait method `{}` will become ambiguous in Rust {edition}",
segment.ident.name
),
|lint| {
let sp = self_expr.span;

Expand Down Expand Up @@ -131,7 +142,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
prelude_or_array_lint,
call_expr.hir_id,
call_expr.span,
format!("trait method `{}` will become ambiguous in Rust 2021", segment.ident.name),
format!(
"trait method `{}` will become ambiguous in Rust {edition}",
segment.ident.name
),
|lint| {
let sp = call_expr.span;
let trait_name = self.trait_path_or_bare_name(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod types;
mod unit_bindings;
mod unused;

pub use shadowed_into_iter::ARRAY_INTO_ITER;
pub use shadowed_into_iter::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};

use rustc_hir::def_id::LocalModDefId;
use rustc_middle::query::Providers;
Expand Down
27 changes: 27 additions & 0 deletions tests/ui/rust-2024/box-slice-into-iter-ambiguous.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// See https://github.com/rust-lang/rust/issues/88475
//@ run-rustfix
//@ edition:2021
//@ check-pass
#![warn(boxed_slice_into_iter)]
#![allow(unused)]

struct FooIter;

trait MyIntoIter {
fn into_iter(self) -> FooIter;
}

impl<T> MyIntoIter for Box<[T]> {
fn into_iter(self) -> FooIter {
FooIter
}
}

struct Point;

pub fn main() {
let points: Box<[_]> = vec![Point].into_boxed_slice();
let y = MyIntoIter::into_iter(points);
//~^ WARNING trait method `into_iter` will become ambiguous in Rust 2024
//~| WARNING this changes meaning in Rust 2024
}
27 changes: 27 additions & 0 deletions tests/ui/rust-2024/box-slice-into-iter-ambiguous.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// See https://github.com/rust-lang/rust/issues/88475
//@ run-rustfix
//@ edition:2021
//@ check-pass
#![warn(boxed_slice_into_iter)]
#![allow(unused)]

struct FooIter;

trait MyIntoIter {
fn into_iter(self) -> FooIter;
}

impl<T> MyIntoIter for Box<[T]> {
fn into_iter(self) -> FooIter {
FooIter
}
}

struct Point;

pub fn main() {
let points: Box<[_]> = vec![Point].into_boxed_slice();
let y = points.into_iter();
//~^ WARNING trait method `into_iter` will become ambiguous in Rust 2024
//~| WARNING this changes meaning in Rust 2024
}
15 changes: 15 additions & 0 deletions tests/ui/rust-2024/box-slice-into-iter-ambiguous.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
warning: trait method `into_iter` will become ambiguous in Rust 2024
--> $DIR/box-slice-into-iter-ambiguous.rs:24:13
|
LL | let y = points.into_iter();
| ^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `MyIntoIter::into_iter(points)`
|
= warning: this changes meaning in Rust 2024
note: the lint level is defined here
--> $DIR/box-slice-into-iter-ambiguous.rs:5:9
|
LL | #![warn(boxed_slice_into_iter)]
| ^^^^^^^^^^^^^^^^^^^^^

warning: 1 warning emitted

0 comments on commit 0aafa34

Please sign in to comment.