Skip to content

Commit bc6b70f

Browse files
committedMay 20, 2024
Adjust the method ambiguity lint too
1 parent a502e7a commit bc6b70f

File tree

6 files changed

+108
-21
lines changed

6 files changed

+108
-21
lines changed
 

‎compiler/rustc_hir_typeck/src/method/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/method-lookup.html
44
55
mod confirm;
6-
mod prelude2021;
6+
mod prelude_edition_lints;
77
pub mod probe;
88
mod suggest;
99

@@ -186,7 +186,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
186186
let pick =
187187
self.lookup_probe(segment.ident, self_ty, call_expr, ProbeScope::TraitsInScope)?;
188188

189-
self.lint_dot_call_from_2018(self_ty, segment, span, call_expr, self_expr, &pick, args);
189+
self.lint_edition_dependent_dot_call(
190+
self_ty, segment, span, call_expr, self_expr, &pick, args,
191+
);
190192

191193
for &import_id in &pick.import_ids {
192194
debug!("used_trait_import: {:?}", import_id);

‎compiler/rustc_hir_typeck/src/method/prelude2021.rs renamed to ‎compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::{
2-
method::probe::{self, Pick},
3-
FnCtxt,
4-
};
1+
use crate::method::probe::{self, Pick};
2+
use crate::FnCtxt;
3+
54
use hir::def_id::DefId;
65
use hir::HirId;
76
use hir::ItemKind;
87
use rustc_errors::Applicability;
98
use rustc_hir as hir;
9+
use rustc_lint::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};
1010
use rustc_middle::span_bug;
1111
use rustc_middle::ty::{self, Ty};
1212
use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS;
@@ -17,7 +17,7 @@ use rustc_trait_selection::infer::InferCtxtExt;
1717
use std::fmt::Write;
1818

1919
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20-
pub(super) fn lint_dot_call_from_2018(
20+
pub(super) fn lint_edition_dependent_dot_call(
2121
&self,
2222
self_ty: Ty<'tcx>,
2323
segment: &hir::PathSegment<'_>,
@@ -32,22 +32,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3232
segment.ident, self_ty, call_expr, self_expr
3333
);
3434

35-
// Rust 2021 and later is already using the new prelude
36-
if span.at_least_rust_2021() {
37-
return;
38-
}
39-
40-
let prelude_or_array_lint = match segment.ident.name {
35+
let (prelude_or_array_lint, edition) = match segment.ident.name {
4136
// `try_into` was added to the prelude in Rust 2021.
42-
sym::try_into => RUST_2021_PRELUDE_COLLISIONS,
37+
sym::try_into if !span.at_least_rust_2021() => (RUST_2021_PRELUDE_COLLISIONS, "2021"),
4338
// `into_iter` wasn't added to the prelude,
4439
// but `[T; N].into_iter()` doesn't resolve to IntoIterator::into_iter
4540
// before Rust 2021, which results in the same problem.
4641
// It is only a problem for arrays.
47-
sym::into_iter if let ty::Array(..) = self_ty.kind() => {
48-
// In this case, it wasn't really a prelude addition that was the problem.
49-
// Instead, the problem is that the array-into_iter hack will no longer apply in Rust 2021.
50-
rustc_lint::ARRAY_INTO_ITER
42+
sym::into_iter => {
43+
if let ty::Array(..) = self_ty.kind()
44+
&& !span.at_least_rust_2021()
45+
{
46+
// In this case, it wasn't really a prelude addition that was the problem.
47+
// Instead, the problem is that the array-into_iter hack will no longer
48+
// apply in Rust 2021.
49+
(ARRAY_INTO_ITER, "2021")
50+
} else if self_ty.is_box()
51+
&& self_ty.boxed_ty().is_slice()
52+
&& !span.at_least_rust_2024()
53+
{
54+
// In this case, it wasn't really a prelude addition that was the problem.
55+
// Instead, the problem is that the boxed-slice-into_iter hack will no
56+
// longer apply in Rust 2024.
57+
(BOXED_SLICE_INTO_ITER, "2024")
58+
} else {
59+
return;
60+
}
5161
}
5262
_ => return,
5363
};
@@ -81,7 +91,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8191
prelude_or_array_lint,
8292
self_expr.hir_id,
8393
self_expr.span,
84-
format!("trait method `{}` will become ambiguous in Rust 2021", segment.ident.name),
94+
format!(
95+
"trait method `{}` will become ambiguous in Rust {edition}",
96+
segment.ident.name
97+
),
8598
|lint| {
8699
let sp = self_expr.span;
87100

@@ -131,7 +144,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
131144
prelude_or_array_lint,
132145
call_expr.hir_id,
133146
call_expr.span,
134-
format!("trait method `{}` will become ambiguous in Rust 2021", segment.ident.name),
147+
format!(
148+
"trait method `{}` will become ambiguous in Rust {edition}",
149+
segment.ident.name
150+
),
135151
|lint| {
136152
let sp = call_expr.span;
137153
let trait_name = self.trait_path_or_bare_name(

‎compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mod types;
8181
mod unit_bindings;
8282
mod unused;
8383

84-
pub use shadowed_into_iter::ARRAY_INTO_ITER;
84+
pub use shadowed_into_iter::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};
8585

8686
use rustc_hir::def_id::LocalModDefId;
8787
use rustc_middle::query::Providers;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// See https://github.com/rust-lang/rust/issues/88475
2+
//@ run-rustfix
3+
//@ edition:2021
4+
//@ check-pass
5+
#![warn(boxed_slice_into_iter)]
6+
#![allow(unused)]
7+
8+
struct FooIter;
9+
10+
trait MyIntoIter {
11+
fn into_iter(self) -> FooIter;
12+
}
13+
14+
impl<T> MyIntoIter for Box<[T]> {
15+
fn into_iter(self) -> FooIter {
16+
FooIter
17+
}
18+
}
19+
20+
struct Point;
21+
22+
pub fn main() {
23+
let points: Box<[_]> = vec![Point].into_boxed_slice();
24+
let y = MyIntoIter::into_iter(points);
25+
//~^ WARNING trait method `into_iter` will become ambiguous in Rust 2024
26+
//~| WARNING this changes meaning in Rust 2024
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// See https://github.com/rust-lang/rust/issues/88475
2+
//@ run-rustfix
3+
//@ edition:2021
4+
//@ check-pass
5+
#![warn(boxed_slice_into_iter)]
6+
#![allow(unused)]
7+
8+
struct FooIter;
9+
10+
trait MyIntoIter {
11+
fn into_iter(self) -> FooIter;
12+
}
13+
14+
impl<T> MyIntoIter for Box<[T]> {
15+
fn into_iter(self) -> FooIter {
16+
FooIter
17+
}
18+
}
19+
20+
struct Point;
21+
22+
pub fn main() {
23+
let points: Box<[_]> = vec![Point].into_boxed_slice();
24+
let y = points.into_iter();
25+
//~^ WARNING trait method `into_iter` will become ambiguous in Rust 2024
26+
//~| WARNING this changes meaning in Rust 2024
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
warning: trait method `into_iter` will become ambiguous in Rust 2024
2+
--> $DIR/box-slice-into-iter-ambiguous.rs:24:13
3+
|
4+
LL | let y = points.into_iter();
5+
| ^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `MyIntoIter::into_iter(points)`
6+
|
7+
= warning: this changes meaning in Rust 2024
8+
note: the lint level is defined here
9+
--> $DIR/box-slice-into-iter-ambiguous.rs:5:9
10+
|
11+
LL | #![warn(boxed_slice_into_iter)]
12+
| ^^^^^^^^^^^^^^^^^^^^^
13+
14+
warning: 1 warning emitted
15+

0 commit comments

Comments
 (0)
Please sign in to comment.