Skip to content

Commit b6d0cca

Browse files
Rollup merge of #86422 - JohnTitor:clearer-parens-err-for-loop, r=estebank
Emit clearer diagnostics for parens around `for` loop heads Fixes #63113 r? `@estebank`
2 parents e0c38af + e9bf73c commit b6d0cca

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -1334,30 +1334,25 @@ impl<'a> Parser<'a> {
13341334
pub(super) fn recover_parens_around_for_head(
13351335
&mut self,
13361336
pat: P<Pat>,
1337-
expr: &Expr,
13381337
begin_paren: Option<Span>,
13391338
) -> P<Pat> {
13401339
match (&self.token.kind, begin_paren) {
13411340
(token::CloseDelim(token::Paren), Some(begin_par_sp)) => {
13421341
self.bump();
13431342

1344-
let pat_str = self
1345-
// Remove the `(` from the span of the pattern:
1346-
.span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
1347-
.unwrap_or_else(|_| pprust::pat_to_string(&pat));
1348-
1349-
self.struct_span_err(self.prev_token.span, "unexpected closing `)`")
1350-
.span_label(begin_par_sp, "opening `(`")
1351-
.span_suggestion(
1352-
begin_par_sp.to(self.prev_token.span),
1353-
"remove parenthesis in `for` loop",
1354-
format!("{} in {}", pat_str, pprust::expr_to_string(&expr)),
1355-
// With e.g. `for (x) in y)` this would replace `(x) in y)`
1356-
// with `x) in y)` which is syntactically invalid.
1357-
// However, this is prevented before we get here.
1358-
Applicability::MachineApplicable,
1359-
)
1360-
.emit();
1343+
self.struct_span_err(
1344+
MultiSpan::from_spans(vec![begin_par_sp, self.prev_token.span]),
1345+
"unexpected parenthesis surrounding `for` loop head",
1346+
)
1347+
.multipart_suggestion(
1348+
"remove parenthesis in `for` loop",
1349+
vec![(begin_par_sp, String::new()), (self.prev_token.span, String::new())],
1350+
// With e.g. `for (x) in y)` this would replace `(x) in y)`
1351+
// with `x) in y)` which is syntactically invalid.
1352+
// However, this is prevented before we get here.
1353+
Applicability::MachineApplicable,
1354+
)
1355+
.emit();
13611356

13621357
// Unwrap `(pat)` into `pat` to avoid the `unused_parens` lint.
13631358
pat.and_then(|pat| match pat.kind {

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ impl<'a> Parser<'a> {
20422042
self.check_for_for_in_in_typo(self.prev_token.span);
20432043
let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?;
20442044

2045-
let pat = self.recover_parens_around_for_head(pat, &expr, begin_paren);
2045+
let pat = self.recover_parens_around_for_head(pat, begin_paren);
20462046

20472047
let (iattrs, loop_block) = self.parse_inner_attrs_and_block()?;
20482048
attrs.extend(iattrs);

src/test/ui/parser/recover-for-loop-parens-around-head.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99

1010
for ( elem in vec ) {
1111
//~^ ERROR expected one of `)`, `,`, `@`, or `|`, found keyword `in`
12-
//~| ERROR unexpected closing `)`
12+
//~| ERROR unexpected parenthesis surrounding `for` loop head
1313
const RECOVERY_WITNESS: () = 0; //~ ERROR mismatched types
1414
}
1515
}

src/test/ui/parser/recover-for-loop-parens-around-head.stderr

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ error: expected one of `)`, `,`, `@`, or `|`, found keyword `in`
44
LL | for ( elem in vec ) {
55
| ^^ expected one of `)`, `,`, `@`, or `|`
66

7-
error: unexpected closing `)`
8-
--> $DIR/recover-for-loop-parens-around-head.rs:10:23
7+
error: unexpected parenthesis surrounding `for` loop head
8+
--> $DIR/recover-for-loop-parens-around-head.rs:10:9
99
|
1010
LL | for ( elem in vec ) {
11-
| --------------^
12-
| |
13-
| opening `(`
14-
| help: remove parenthesis in `for` loop: `elem in vec`
11+
| ^ ^
12+
|
13+
help: remove parenthesis in `for` loop
14+
|
15+
LL - for ( elem in vec ) {
16+
LL + for elem in vec {
17+
|
1518

1619
error[E0308]: mismatched types
1720
--> $DIR/recover-for-loop-parens-around-head.rs:13:38

0 commit comments

Comments
 (0)