Skip to content

Commit 152a029

Browse files
committed
recover from loop...else
1 parent 8820631 commit 152a029

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,7 @@ impl<'a> Parser<'a> {
25502550
fn parse_loop_expr(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> {
25512551
let loop_span = self.prev_token.span;
25522552
let (attrs, body) = self.parse_inner_attrs_and_block()?;
2553+
self.recover_loop_else("loop", lo)?;
25532554
Ok(self.mk_expr_with_attrs(
25542555
lo.to(self.prev_token.span),
25552556
ExprKind::Loop(body, opt_label, loop_span),

tests/ui/loops/loop-else-err.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
loop {
3+
//~^ NOTE `else` is attached to this loop
4+
} else {
5+
//~^ ERROR `loop...else` loops are not supported
6+
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run
7+
}
8+
}

tests/ui/loops/loop-else-err.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: `loop...else` loops are not supported
2+
--> $DIR/loop-else-err.rs:4:7
3+
|
4+
LL | loop {
5+
| ---- `else` is attached to this loop
6+
LL |
7+
LL | } else {
8+
| _______^
9+
LL | |
10+
LL | |
11+
LL | | }
12+
| |_____^
13+
|
14+
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run
15+
16+
error: aborting due to previous error
17+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let _ = loop {
3+
//~^ NOTE `else` is attached to this loop
4+
} else {
5+
//~^ ERROR `loop...else` loops are not supported
6+
//~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run
7+
};
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: `loop...else` loops are not supported
2+
--> $DIR/loop-else-let-else-err.rs:4:7
3+
|
4+
LL | let _ = loop {
5+
| ---- `else` is attached to this loop
6+
LL |
7+
LL | } else {
8+
| _______^
9+
LL | |
10+
LL | |
11+
LL | | };
12+
| |_____^
13+
|
14+
= note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run
15+
16+
error: aborting due to previous error
17+

0 commit comments

Comments
 (0)