Skip to content

Commit 8820631

Browse files
committed
add loop keyword to error diag
1 parent 8574085 commit 8820631

File tree

11 files changed

+23
-7
lines changed

11 files changed

+23
-7
lines changed

compiler/rustc_parse/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ parse_missing_expression_in_for_loop = missing expression to iterate on in `for`
153153
154154
parse_loop_else = `{$loop_kind}...else` loops are not supported
155155
.note = consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run
156+
.loop_keyword = `else` is attached to this loop
156157
157158
parse_missing_comma_after_match_arm = expected `,` following `match` arm
158159
.suggestion = missing a comma here to end this `match` arm

compiler/rustc_parse/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ pub(crate) struct LoopElseNotSupported {
458458
#[primary_span]
459459
pub span: Span,
460460
pub loop_kind: &'static str,
461+
#[label(parse_loop_keyword)]
462+
pub loop_kw: Span,
461463
}
462464

463465
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,20 +2492,21 @@ impl<'a> Parser<'a> {
24922492

24932493
let kind = ExprKind::ForLoop(pat, expr, loop_block, opt_label);
24942494

2495-
self.recover_loop_else("for")?;
2495+
self.recover_loop_else("for", lo)?;
24962496

24972497
Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs))
24982498
}
24992499

25002500
/// Recovers from an `else` clause after a loop (`for...else`, `while...else`)
2501-
fn recover_loop_else(&mut self, loop_kind: &'static str) -> PResult<'a, ()> {
2501+
fn recover_loop_else(&mut self, loop_kind: &'static str, loop_kw: Span) -> PResult<'a, ()> {
25022502
if self.token.is_keyword(kw::Else) && self.may_recover() {
25032503
let else_span = self.token.span;
25042504
self.bump();
25052505
let else_clause = self.parse_else_expr()?;
25062506
self.sess.emit_err(errors::LoopElseNotSupported {
25072507
span: else_span.to(else_clause.span),
25082508
loop_kind,
2509+
loop_kw,
25092510
});
25102511
}
25112512
Ok(())
@@ -2536,7 +2537,7 @@ impl<'a> Parser<'a> {
25362537
err
25372538
})?;
25382539

2539-
self.recover_loop_else("while")?;
2540+
self.recover_loop_else("while", lo)?;
25402541

25412542
Ok(self.mk_expr_with_attrs(
25422543
lo.to(self.prev_token.span),

tests/ui/for/for-else-err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
for _ in 0..1 {
3-
3+
//~^ NOTE `else` is attached to this loop
44
} else {
55
//~^ ERROR `for...else` loops are not supported
66
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run

tests/ui/for/for-else-err.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: `for...else` loops are not supported
22
--> $DIR/for-else-err.rs:4:7
33
|
4+
LL | for _ in 0..1 {
5+
| --- `else` is attached to this loop
6+
LL |
47
LL | } else {
58
| _______^
69
LL | |

tests/ui/for/for-else-let-else-err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
let _ = for _ in 0..1 {
3-
3+
//~^ NOTE `else` is attached to this loop
44
} else {
55
//~^ ERROR `for...else` loops are not supported
66
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run

tests/ui/for/for-else-let-else-err.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: `for...else` loops are not supported
22
--> $DIR/for-else-let-else-err.rs:4:7
33
|
4+
LL | let _ = for _ in 0..1 {
5+
| --- `else` is attached to this loop
6+
LL |
47
LL | } else {
58
| _______^
69
LL | |

tests/ui/while/while-else-err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
while false {
3-
3+
//~^ NOTE `else` is attached to this loop
44
} else {
55
//~^ ERROR `while...else` loops are not supported
66
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run

tests/ui/while/while-else-err.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: `while...else` loops are not supported
22
--> $DIR/while-else-err.rs:4:7
33
|
4+
LL | while false {
5+
| ----- `else` is attached to this loop
6+
LL |
47
LL | } else {
58
| _______^
69
LL | |

tests/ui/while/while-else-let-else-err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
22
let _ = while false {
3-
3+
//~^ NOTE `else` is attached to this loop
44
} else {
55
//~^ ERROR `while...else` loops are not supported
66
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run

tests/ui/while/while-else-let-else-err.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error: `while...else` loops are not supported
22
--> $DIR/while-else-let-else-err.rs:4:7
33
|
4+
LL | let _ = while false {
5+
| ----- `else` is attached to this loop
6+
LL |
47
LL | } else {
58
| _______^
69
LL | |

0 commit comments

Comments
 (0)