Skip to content

Commit 735969e

Browse files
authored
Rollup merge of #99891 - compiler-errors:suggest-slicing-carefully, r=oli-obk
Adjust an expr span to account for macros Fix this erroneous suggestion: ``` error[E0529]: expected an array or slice, found `Vec<{integer}>` --> /home/gh-compiler-errors/test.rs:2:9 | 2 | let [..] = vec![1, 2, 3]; | ^^^^ pattern cannot match with input type `Vec<{integer}>` | help: consider slicing here --> /home/gh-compiler-errors/rust2/library/alloc/src/macros.rs:50:36 | 50~ $crate::__rust_force_expr!(<[_]>::into_vec( 51+ #[rustc_box] 52+ $crate::boxed::Box::new([$($x),+]) 53~ )[..]) ```
2 parents 5c3b6d6 + dec29b1 commit 735969e

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

compiler/rustc_typeck/src/check/_match.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3939
let scrut_diverges = self.diverges.replace(Diverges::Maybe);
4040

4141
// #55810: Type check patterns first so we get types for all bindings.
42+
let scrut_span = scrut.span.find_ancestor_inside(expr.span).unwrap_or(scrut.span);
4243
for arm in arms {
43-
self.check_pat_top(&arm.pat, scrutinee_ty, Some(scrut.span), true);
44+
self.check_pat_top(&arm.pat, scrutinee_ty, Some(scrut_span), true);
4445
}
4546

4647
// Now typecheck the blocks.

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12341234
// Does the expected pattern type originate from an expression and what is the span?
12351235
let (origin_expr, ty_span) = match (decl.ty, decl.init) {
12361236
(Some(ty), _) => (false, Some(ty.span)), // Bias towards the explicit user type.
1237-
(_, Some(init)) => (true, Some(init.span)), // No explicit type; so use the scrutinee.
1237+
(_, Some(init)) => {
1238+
(true, Some(init.span.find_ancestor_inside(decl.span).unwrap_or(init.span)))
1239+
} // No explicit type; so use the scrutinee.
12381240
_ => (false, None), // We have `let $pat;`, so the expected type is unconstrained.
12391241
};
12401242

src/test/ui/suggestions/pattern-slice-vec.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ fn main() {
2424
//~^ ERROR: expected an array or slice
2525
_ => {}
2626
}
27+
28+
let [..] = vec![1, 2, 3][..];
29+
//~^ ERROR: expected an array or slice
30+
//~| HELP: consider slicing here
2731
}

src/test/ui/suggestions/pattern-slice-vec.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ fn main() {
2424
//~^ ERROR: expected an array or slice
2525
_ => {}
2626
}
27+
28+
let [..] = vec![1, 2, 3];
29+
//~^ ERROR: expected an array or slice
30+
//~| HELP: consider slicing here
2731
}

src/test/ui/suggestions/pattern-slice-vec.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ LL |
3131
LL | [5] => {}
3232
| ^^^ pattern cannot match with input type `Vec<_>`
3333

34-
error: aborting due to 4 previous errors
34+
error[E0529]: expected an array or slice, found `Vec<{integer}>`
35+
--> $DIR/pattern-slice-vec.rs:28:9
36+
|
37+
LL | let [..] = vec![1, 2, 3];
38+
| ^^^^ ------------- help: consider slicing here: `vec![1, 2, 3][..]`
39+
| |
40+
| pattern cannot match with input type `Vec<{integer}>`
41+
42+
error: aborting due to 5 previous errors
3543

3644
For more information about this error, try `rustc --explain E0529`.

0 commit comments

Comments
 (0)