Skip to content

Commit 58765d6

Browse files
committed
Emit clearer diagnostics for parens around for loop heads
1 parent 78a46ef commit 58765d6

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,9 @@ impl<'a> Parser<'a> {
13461346
.span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
13471347
.unwrap_or_else(|_| pprust::pat_to_string(&pat));
13481348

1349-
self.struct_span_err(self.prev_token.span, "unexpected closing `)`")
1350-
.span_label(begin_par_sp, "opening `(`")
1349+
let sp = MultiSpan::from_spans(vec![begin_par_sp, self.prev_token.span]);
1350+
1351+
self.struct_span_err(sp, "unexpected parenthesis surrounding `for` loop head")
13511352
.span_suggestion(
13521353
begin_par_sp.to(self.prev_token.span),
13531354
"remove parenthesis in `for` loop",

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ 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-
| --------------^
11+
| ^-------------^
1212
| |
13-
| opening `(`
1413
| help: remove parenthesis in `for` loop: `elem in vec`
1514

1615
error[E0308]: mismatched types

0 commit comments

Comments
 (0)