Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust an expr span to account for macros #99891

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_typeck/src/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let scrut_diverges = self.diverges.replace(Diverges::Maybe);

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

// Now typecheck the blocks.
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Does the expected pattern type originate from an expression and what is the span?
let (origin_expr, ty_span) = match (decl.ty, decl.init) {
(Some(ty), _) => (false, Some(ty.span)), // Bias towards the explicit user type.
(_, Some(init)) => (true, Some(init.span)), // No explicit type; so use the scrutinee.
(_, Some(init)) => {
(true, Some(init.span.find_ancestor_inside(decl.span).unwrap_or(init.span)))
} // No explicit type; so use the scrutinee.
_ => (false, None), // We have `let $pat;`, so the expected type is unconstrained.
};

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/suggestions/pattern-slice-vec.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ fn main() {
//~^ ERROR: expected an array or slice
_ => {}
}

let [..] = vec![1, 2, 3][..];
//~^ ERROR: expected an array or slice
//~| HELP: consider slicing here
}
4 changes: 4 additions & 0 deletions src/test/ui/suggestions/pattern-slice-vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ fn main() {
//~^ ERROR: expected an array or slice
_ => {}
}

let [..] = vec![1, 2, 3];
//~^ ERROR: expected an array or slice
//~| HELP: consider slicing here
}
10 changes: 9 additions & 1 deletion src/test/ui/suggestions/pattern-slice-vec.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ LL |
LL | [5] => {}
| ^^^ pattern cannot match with input type `Vec<_>`

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

error: aborting due to 5 previous errors

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