Skip to content

Commit 5c85e3f

Browse files
Auto merge of #147421 - Kivooeo:ice-fix51621, r=<try>
Add check if span is from macro expansion try-job: aarch64-msvc-1
2 parents 5dbf406 + 47384f7 commit 5c85e3f

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

compiler/rustc_lint/src/shadowed_into_iter.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
124124
return;
125125
};
126126

127+
// This check needs to avoid ICE from when `receiver_arg` is from macro expansion
128+
// Which leads to empty span in span arithmetic below
129+
// cc: https://github.com/rust-lang/rust/issues/147408
130+
let span = receiver_arg.span.find_ancestor_in_same_ctxt(expr.span);
131+
127132
// If this expression comes from the `IntoIter::into_iter` inside of a for loop,
128133
// we should just suggest removing the `.into_iter()` or changing it to `.iter()`
129134
// to disambiguate if we want to iterate by-value or by-ref.
@@ -134,14 +139,15 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
134139
&& let hir::ExprKind::Call(path, [_]) = &arg.kind
135140
&& let hir::ExprKind::Path(qpath) = path.kind
136141
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::IntoIterIntoIter)
142+
&& let Some(span) = span
137143
{
138144
Some(ShadowedIntoIterDiagSub::RemoveIntoIter {
139-
span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
145+
span: span.shrink_to_hi().to(expr.span.shrink_to_hi()),
140146
})
141-
} else if can_suggest_ufcs {
147+
} else if can_suggest_ufcs && let Some(span) = span {
142148
Some(ShadowedIntoIterDiagSub::UseExplicitIntoIter {
143149
start_span: expr.span.shrink_to_lo(),
144-
end_span: receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
150+
end_span: span.shrink_to_hi().to(expr.span.shrink_to_hi()),
145151
})
146152
} else {
147153
None
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
//@ compile-flags: -Afor_loops_over_fallibles -Warray_into_iter
3+
4+
fn main() {
5+
macro_rules! mac {
6+
(iter $e:expr) => {
7+
$e.iter()
8+
};
9+
(into_iter $e:expr) => {
10+
$e.into_iter() //~ WARN this method call resolves to
11+
//~^ WARN this changes meaning in Rust 2021
12+
};
13+
(next $e:expr) => {
14+
$e.iter().next()
15+
};
16+
}
17+
18+
for _ in dbg!([1, 2]).iter() {}
19+
for _ in dbg!([1, 2]).into_iter() {} //~ WARN this method call resolves to
20+
//~^ WARN this changes meaning in Rust 2021
21+
for _ in mac!(iter [1, 2]) {}
22+
for _ in mac!(into_iter [1, 2]) {}
23+
for _ in mac!(next [1, 2]) {}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
2+
--> $DIR/macro-expansion-empty-span-147408.rs:19:27
3+
|
4+
LL | for _ in dbg!([1, 2]).into_iter() {}
5+
| ^^^^^^^^^
6+
|
7+
= warning: this changes meaning in Rust 2021
8+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html>
9+
= note: requested on the command line with `-W array-into-iter`
10+
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity
11+
|
12+
LL - for _ in dbg!([1, 2]).into_iter() {}
13+
LL + for _ in dbg!([1, 2]).iter() {}
14+
|
15+
help: or remove `.into_iter()` to iterate by value
16+
|
17+
LL - for _ in dbg!([1, 2]).into_iter() {}
18+
LL + for _ in dbg!([1, 2]) {}
19+
|
20+
21+
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to `<[T; N] as IntoIterator>::into_iter` in Rust 2021
22+
--> $DIR/macro-expansion-empty-span-147408.rs:10:16
23+
|
24+
LL | $e.into_iter()
25+
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
26+
...
27+
LL | for _ in mac!(into_iter [1, 2]) {}
28+
| ---------------------- in this macro invocation
29+
|
30+
= warning: this changes meaning in Rust 2021
31+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/IntoIterator-for-arrays.html>
32+
= note: this warning originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
33+
34+
warning: 2 warnings emitted
35+

0 commit comments

Comments
 (0)