Skip to content

Commit 9d39a2c

Browse files
compiler-errorsMark-Simulacrum
authored andcommitted
Don't silently eat label before block in block-like expr
1 parent 6955b83 commit 9d39a2c

File tree

3 files changed

+227
-4
lines changed

3 files changed

+227
-4
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -2456,11 +2456,15 @@ impl<'a> Parser<'a> {
24562456
}
24572457

24582458
pub(crate) fn maybe_recover_unexpected_block_label(&mut self) -> bool {
2459-
let Some(label) = self.eat_label().filter(|_| {
2460-
self.eat(&token::Colon) && self.token.kind == token::OpenDelim(Delimiter::Brace)
2461-
}) else {
2459+
// Check for `'a : {`
2460+
if !(self.check_lifetime()
2461+
&& self.look_ahead(1, |tok| tok.kind == token::Colon)
2462+
&& self.look_ahead(2, |tok| tok.kind == token::OpenDelim(Delimiter::Brace)))
2463+
{
24622464
return false;
2463-
};
2465+
}
2466+
let label = self.eat_label().expect("just checked if a label exists");
2467+
self.bump(); // eat `:`
24642468
let span = label.ident.span.to(self.prev_token.span);
24652469
let mut err = self.struct_span_err(span, "block label not supported here");
24662470
err.span_label(span, "not supported here");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
fn a() {
2+
if let () = () 'a {}
3+
//~^ ERROR labeled expression must be followed by `:`
4+
//~| ERROR expected `{`, found `'a`
5+
}
6+
7+
fn b() {
8+
if true 'a {}
9+
//~^ ERROR labeled expression must be followed by `:`
10+
//~| ERROR expected `{`, found `'a`
11+
}
12+
13+
fn c() {
14+
loop 'a {}
15+
//~^ ERROR labeled expression must be followed by `:`
16+
//~| ERROR expected `{`, found `'a`
17+
}
18+
19+
fn d() {
20+
while true 'a {}
21+
//~^ ERROR labeled expression must be followed by `:`
22+
//~| ERROR expected `{`, found `'a`
23+
}
24+
25+
fn e() {
26+
while let () = () 'a {}
27+
//~^ ERROR labeled expression must be followed by `:`
28+
//~| ERROR expected `{`, found `'a`
29+
}
30+
31+
fn f() {
32+
for _ in 0..0 'a {}
33+
//~^ ERROR labeled expression must be followed by `:`
34+
//~| ERROR expected `{`, found `'a`
35+
}
36+
37+
fn g() {
38+
unsafe 'a {}
39+
//~^ ERROR labeled expression must be followed by `:`
40+
//~| ERROR expected `{`, found `'a`
41+
}
42+
43+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
error: labeled expression must be followed by `:`
2+
--> $DIR/label-after-block-like.rs:2:20
3+
|
4+
LL | if let () = () 'a {}
5+
| ---^^
6+
| | |
7+
| | help: add `:` after the label
8+
| the label
9+
|
10+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
11+
12+
error: expected `{`, found `'a`
13+
--> $DIR/label-after-block-like.rs:2:20
14+
|
15+
LL | if let () = () 'a {}
16+
| ^^ expected `{`
17+
|
18+
note: the `if` expression is missing a block after this condition
19+
--> $DIR/label-after-block-like.rs:2:8
20+
|
21+
LL | if let () = () 'a {}
22+
| ^^^^^^^^^^^
23+
help: try placing this code inside a block
24+
|
25+
LL | if let () = () { 'a {} }
26+
| + +
27+
28+
error: labeled expression must be followed by `:`
29+
--> $DIR/label-after-block-like.rs:8:13
30+
|
31+
LL | if true 'a {}
32+
| ---^^
33+
| | |
34+
| | help: add `:` after the label
35+
| the label
36+
|
37+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
38+
39+
error: expected `{`, found `'a`
40+
--> $DIR/label-after-block-like.rs:8:13
41+
|
42+
LL | if true 'a {}
43+
| ^^ expected `{`
44+
|
45+
note: the `if` expression is missing a block after this condition
46+
--> $DIR/label-after-block-like.rs:8:8
47+
|
48+
LL | if true 'a {}
49+
| ^^^^
50+
help: try placing this code inside a block
51+
|
52+
LL | if true { 'a {} }
53+
| + +
54+
55+
error: labeled expression must be followed by `:`
56+
--> $DIR/label-after-block-like.rs:14:10
57+
|
58+
LL | loop 'a {}
59+
| ---^^
60+
| | |
61+
| | help: add `:` after the label
62+
| the label
63+
|
64+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
65+
66+
error: expected `{`, found `'a`
67+
--> $DIR/label-after-block-like.rs:14:10
68+
|
69+
LL | loop 'a {}
70+
| ---- ^^ expected `{`
71+
| |
72+
| while parsing this `loop` expression
73+
|
74+
help: try placing this code inside a block
75+
|
76+
LL | loop { 'a {} }
77+
| + +
78+
79+
error: labeled expression must be followed by `:`
80+
--> $DIR/label-after-block-like.rs:20:16
81+
|
82+
LL | while true 'a {}
83+
| ---^^
84+
| | |
85+
| | help: add `:` after the label
86+
| the label
87+
|
88+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
89+
90+
error: expected `{`, found `'a`
91+
--> $DIR/label-after-block-like.rs:20:16
92+
|
93+
LL | while true 'a {}
94+
| ----- ---- ^^ expected `{`
95+
| | |
96+
| | this `while` condition successfully parsed
97+
| while parsing the body of this `while` expression
98+
|
99+
help: try placing this code inside a block
100+
|
101+
LL | while true { 'a {} }
102+
| + +
103+
104+
error: labeled expression must be followed by `:`
105+
--> $DIR/label-after-block-like.rs:26:23
106+
|
107+
LL | while let () = () 'a {}
108+
| ---^^
109+
| | |
110+
| | help: add `:` after the label
111+
| the label
112+
|
113+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
114+
115+
error: expected `{`, found `'a`
116+
--> $DIR/label-after-block-like.rs:26:23
117+
|
118+
LL | while let () = () 'a {}
119+
| ----- ----------- ^^ expected `{`
120+
| | |
121+
| | this `while` condition successfully parsed
122+
| while parsing the body of this `while` expression
123+
|
124+
help: try placing this code inside a block
125+
|
126+
LL | while let () = () { 'a {} }
127+
| + +
128+
129+
error: labeled expression must be followed by `:`
130+
--> $DIR/label-after-block-like.rs:32:19
131+
|
132+
LL | for _ in 0..0 'a {}
133+
| ---^^
134+
| | |
135+
| | help: add `:` after the label
136+
| the label
137+
|
138+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
139+
140+
error: expected `{`, found `'a`
141+
--> $DIR/label-after-block-like.rs:32:19
142+
|
143+
LL | for _ in 0..0 'a {}
144+
| ^^ expected `{`
145+
|
146+
help: try placing this code inside a block
147+
|
148+
LL | for _ in 0..0 { 'a {} }
149+
| + +
150+
151+
error: labeled expression must be followed by `:`
152+
--> $DIR/label-after-block-like.rs:38:12
153+
|
154+
LL | unsafe 'a {}
155+
| ---^^
156+
| | |
157+
| | help: add `:` after the label
158+
| the label
159+
|
160+
= note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
161+
162+
error: expected `{`, found `'a`
163+
--> $DIR/label-after-block-like.rs:38:12
164+
|
165+
LL | unsafe 'a {}
166+
| ------ ^^ expected `{`
167+
| |
168+
| while parsing this `unsafe` expression
169+
|
170+
help: try placing this code inside a block
171+
|
172+
LL | unsafe { 'a {} }
173+
| + +
174+
175+
error: aborting due to 14 previous errors
176+

0 commit comments

Comments
 (0)