Skip to content

Commit e72df7e

Browse files
committed
parse_labeled_expr: add a suggestion on missing colon.
1 parent fe848b4 commit e72df7e

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,14 +1106,24 @@ impl<'a> Parser<'a> {
11061106
}?;
11071107

11081108
if !ate_colon {
1109-
self.struct_span_err(expr.span, "labeled expression must be followed by `:`")
1110-
.span_label(lo, "the label")
1111-
.emit();
1109+
self.error_labeled_expr_must_be_followed_by_colon(lo, expr.span);
11121110
}
11131111

11141112
Ok(expr)
11151113
}
11161114

1115+
fn error_labeled_expr_must_be_followed_by_colon(&self, lo: Span, span: Span) {
1116+
self.struct_span_err(span, "labeled expression must be followed by `:`")
1117+
.span_label(lo, "the label")
1118+
.span_suggestion_short(
1119+
lo.shrink_to_hi(),
1120+
"add `:` after the label",
1121+
": ".to_string(),
1122+
Applicability::MachineApplicable,
1123+
)
1124+
.emit();
1125+
}
1126+
11171127
/// Recover on the syntax `do catch { ... }` suggesting `try { ... }` instead.
11181128
fn recover_do_catch(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> {
11191129
let lo = self.token.span;

src/test/ui/parser/labeled-no-colon-expr.stderr

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@ error: labeled expression must be followed by `:`
22
--> $DIR/labeled-no-colon-expr.rs:4:5
33
|
44
LL | 'l0 while false {}
5-
| ---^^^^^^^^^^^^^^^
6-
| |
5+
| ----^^^^^^^^^^^^^^
6+
| | |
7+
| | help: add `:` after the label
78
| the label
89

910
error: labeled expression must be followed by `:`
1011
--> $DIR/labeled-no-colon-expr.rs:5:5
1112
|
1213
LL | 'l1 for _ in 0..1 {}
13-
| ---^^^^^^^^^^^^^^^^^
14-
| |
14+
| ----^^^^^^^^^^^^^^^^
15+
| | |
16+
| | help: add `:` after the label
1517
| the label
1618

1719
error: labeled expression must be followed by `:`
1820
--> $DIR/labeled-no-colon-expr.rs:6:5
1921
|
2022
LL | 'l2 loop {}
21-
| ---^^^^^^^^
22-
| |
23+
| ----^^^^^^^
24+
| | |
25+
| | help: add `:` after the label
2326
| the label
2427

2528
error: labeled expression must be followed by `:`
2629
--> $DIR/labeled-no-colon-expr.rs:7:5
2730
|
2831
LL | 'l3 {}
29-
| ---^^^
30-
| |
32+
| ----^^
33+
| | |
34+
| | help: add `:` after the label
3135
| the label
3236

3337
error: expected `while`, `for`, `loop` or `{` after a label
@@ -40,8 +44,9 @@ error: labeled expression must be followed by `:`
4044
--> $DIR/labeled-no-colon-expr.rs:8:9
4145
|
4246
LL | 'l4 0;
43-
| --- ^
44-
| |
47+
| ----^
48+
| | |
49+
| | help: add `:` after the label
4550
| the label
4651

4752
error: cannot use a `block` macro fragment here
@@ -61,7 +66,9 @@ error: labeled expression must be followed by `:`
6166
--> $DIR/labeled-no-colon-expr.rs:16:8
6267
|
6368
LL | 'l5 $b;
64-
| --- the label
69+
| ---- help: add `:` after the label
70+
| |
71+
| the label
6572
...
6673
LL | m!({});
6774
| ^^

0 commit comments

Comments
 (0)