Skip to content

Commit 0d71676

Browse files
committed
Avoid ; -> , recovery and unclosed } recovery from being too verbose
Those two recovery attempts have a very bad interaction that causes too unnecessary output. Add a simple gate to avoid interpreting a `;` as a `,` when there are unclosed braces.
1 parent 8e81596 commit 0d71676

File tree

4 files changed

+21
-145
lines changed

4 files changed

+21
-145
lines changed

compiler/rustc_parse/src/parser/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ impl<'a> Parser<'a> {
703703
let mut recovered = false;
704704
let mut trailing = false;
705705
let mut v = vec![];
706+
let unclosed_delims = !self.unclosed_delims.is_empty();
707+
706708
while !self.expect_any_with_type(kets, expect) {
707709
if let token::CloseDelim(..) | token::Eof = self.token.kind {
708710
break;
@@ -723,7 +725,7 @@ impl<'a> Parser<'a> {
723725

724726
// Attempt to keep parsing if it was a similar separator.
725727
if let Some(ref tokens) = t.similar_tokens() {
726-
if tokens.contains(&self.token.kind) {
728+
if tokens.contains(&self.token.kind) && !unclosed_delims {
727729
self.bump();
728730
}
729731
}

src/test/ui/parser/issue-63116.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `;`
1212
LL | impl W <s(f;Y(;]
1313
| ^ expected one of 7 possible tokens
1414

15-
error: expected one of `!`, `&&`, `&`, `(`, `)`, `*`, `+`, `,`, `->`, `...`, `::`, `:`, `<`, `=`, `>`, `?`, `[`, `_`, `async`, `const`, `dyn`, `extern`, `fn`, `for`, `impl`, `unsafe`, lifetime, or path, found `;`
16-
--> $DIR/issue-63116.rs:3:15
15+
error: mismatched closing delimiter: `]`
16+
--> $DIR/issue-63116.rs:3:16
1717
|
1818
LL | impl W <s(f;Y(;]
19-
| -^ help: `)` may belong here
19+
| - ^ mismatched closing delimiter
2020
| |
2121
| unclosed delimiter
2222

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1+
// error-pattern: expected one of `,`, `::`, `as`, or `}`, found `;`
2+
// error-pattern: this file contains an unclosed delimiter
3+
// error-pattern: expected item, found `}`
14
use foo::{bar, baz;
2-
//~^ ERROR expected one of `,`, `::`, `as`, or `}`, found `;`
35

46
use std::fmt::Display;
5-
//~^ ERROR expected identifier, found keyword `use`
6-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `std`
7-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `;`
87

98
mod bar { }
10-
//~^ ERROR expected identifier, found keyword `mod`
11-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `bar`
12-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `{`
139

1410
mod baz { }
15-
//~^ ERROR expected identifier, found keyword `mod`
16-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `{`
17-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `baz`
18-
//~| ERROR expected one of `,` or `}`, found keyword `mod`
1911

2012
fn main() {}
21-
//~^ ERROR expected identifier, found keyword `fn`
22-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `(`
23-
//~| ERROR expected one of `,` or `}`, found keyword `fn`
24-
//~| ERROR expected one of `,`, `::`, `as`, or `}`, found `main`
25-
26-
//~ ERROR this file contains an unclosed delimiter
+12-124
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,27 @@
11
error: this file contains an unclosed delimiter
2-
--> $DIR/use-unclosed-brace.rs:26:51
2+
--> $DIR/use-unclosed-brace.rs:12:14
33
|
44
LL | use foo::{bar, baz;
55
| - unclosed delimiter
66
...
7-
LL |
8-
| ^
9-
10-
error: expected identifier, found keyword `use`
11-
--> $DIR/use-unclosed-brace.rs:4:1
12-
|
13-
LL | use std::fmt::Display;
14-
| ^^^ expected identifier, found keyword
7+
LL | fn main() {}
8+
| ^
159

1610
error: expected one of `,`, `::`, `as`, or `}`, found `;`
17-
--> $DIR/use-unclosed-brace.rs:1:19
11+
--> $DIR/use-unclosed-brace.rs:4:19
1812
|
1913
LL | use foo::{bar, baz;
20-
| - ^ expected one of `,`, `::`, `as`, or `}`
21-
| |
14+
| - ^
15+
| | |
16+
| | expected one of `,`, `::`, `as`, or `}`
17+
| | help: `}` may belong here
2218
| unclosed delimiter
23-
|
24-
help: `}` may belong here
25-
|
26-
LL | use foo::{bar, baz};
27-
| ^
28-
help: missing `,`
29-
|
30-
LL | use foo::{bar, baz,;
31-
| ^
32-
33-
error: expected one of `,`, `::`, `as`, or `}`, found `std`
34-
--> $DIR/use-unclosed-brace.rs:4:5
35-
|
36-
LL | use std::fmt::Display;
37-
| -^^^ expected one of `,`, `::`, `as`, or `}`
38-
| |
39-
| help: missing `,`
40-
41-
error: expected identifier, found keyword `mod`
42-
--> $DIR/use-unclosed-brace.rs:9:1
43-
|
44-
LL | mod bar { }
45-
| ^^^ expected identifier, found keyword
46-
47-
error: expected one of `,`, `::`, `as`, or `}`, found `;`
48-
--> $DIR/use-unclosed-brace.rs:4:22
49-
|
50-
LL | use std::fmt::Display;
51-
| ^
52-
| |
53-
| expected one of `,`, `::`, `as`, or `}`
54-
| help: missing `,`
55-
56-
error: expected one of `,`, `::`, `as`, or `}`, found `bar`
57-
--> $DIR/use-unclosed-brace.rs:9:5
58-
|
59-
LL | mod bar { }
60-
| -^^^ expected one of `,`, `::`, `as`, or `}`
61-
| |
62-
| help: missing `,`
63-
64-
error: expected one of `,`, `::`, `as`, or `}`, found `{`
65-
--> $DIR/use-unclosed-brace.rs:9:9
66-
|
67-
LL | mod bar { }
68-
| -^ expected one of `,`, `::`, `as`, or `}`
69-
| |
70-
| help: missing `,`
71-
72-
error: expected identifier, found keyword `mod`
73-
--> $DIR/use-unclosed-brace.rs:14:1
74-
|
75-
LL | mod baz { }
76-
| ^^^ expected identifier, found keyword
77-
78-
error: expected one of `,` or `}`, found keyword `mod`
79-
--> $DIR/use-unclosed-brace.rs:14:1
80-
|
81-
LL | mod bar { }
82-
| -
83-
| |
84-
| expected one of `,` or `}`
85-
| help: missing `,`
86-
...
87-
LL | mod baz { }
88-
| ^^^ unexpected token
89-
90-
error: expected one of `,`, `::`, `as`, or `}`, found `baz`
91-
--> $DIR/use-unclosed-brace.rs:14:5
92-
|
93-
LL | mod baz { }
94-
| -^^^ expected one of `,`, `::`, `as`, or `}`
95-
| |
96-
| help: missing `,`
97-
98-
error: expected one of `,`, `::`, `as`, or `}`, found `{`
99-
--> $DIR/use-unclosed-brace.rs:14:9
100-
|
101-
LL | mod baz { }
102-
| -^ expected one of `,`, `::`, `as`, or `}`
103-
| |
104-
| help: missing `,`
105-
106-
error: expected identifier, found keyword `fn`
107-
--> $DIR/use-unclosed-brace.rs:20:1
108-
|
109-
LL | fn main() {}
110-
| ^^ expected identifier, found keyword
111-
112-
error: expected one of `,` or `}`, found keyword `fn`
113-
--> $DIR/use-unclosed-brace.rs:20:1
114-
|
115-
LL | mod baz { }
116-
| -
117-
| |
118-
| expected one of `,` or `}`
119-
| help: missing `,`
120-
...
121-
LL | fn main() {}
122-
| ^^ unexpected token
123-
124-
error: expected one of `,`, `::`, `as`, or `}`, found `main`
125-
--> $DIR/use-unclosed-brace.rs:20:4
126-
|
127-
LL | fn main() {}
128-
| -^^^^ expected one of `,`, `::`, `as`, or `}`
129-
| |
130-
| help: missing `,`
13119

132-
error: expected one of `,`, `::`, `as`, or `}`, found `(`
133-
--> $DIR/use-unclosed-brace.rs:20:8
20+
error: expected item, found `}`
21+
--> $DIR/use-unclosed-brace.rs:12:14
13422
|
13523
LL | fn main() {}
136-
| ^ expected one of `,`, `::`, `as`, or `}`
24+
| ^ expected item
13725

138-
error: aborting due to 16 previous errors
26+
error: aborting due to 3 previous errors
13927

0 commit comments

Comments
 (0)