diff options
| author | y21 <30553356+y21@users.noreply.github.com> | 2023-02-23 20:12:01 +0100 |
|---|---|---|
| committer | y21 <30553356+y21@users.noreply.github.com> | 2023-03-01 13:26:59 +0100 |
| commit | 0758c05c9792700dbc482eda8cd464c39d5ebed5 (patch) | |
| tree | a6df85d272161a11a4945c2a3d730a39364f6d3d | |
| parent | 64165aac68af780182ff89a6eb3982e3c262266e (diff) | |
| download | rust-0758c05c9792700dbc482eda8cd464c39d5ebed5.tar.gz rust-0758c05c9792700dbc482eda8cd464c39d5ebed5.zip | |
recover from for-else and while-else
20 files changed, 218 insertions, 22 deletions
diff --git a/compiler/rustc_parse/locales/en-US.ftl b/compiler/rustc_parse/locales/en-US.ftl index 4ddeeed5b7e..e76e91fc1b1 100644 --- a/compiler/rustc_parse/locales/en-US.ftl +++ b/compiler/rustc_parse/locales/en-US.ftl @@ -151,6 +151,10 @@ parse_missing_in_in_for_loop = missing `in` in `for` loop parse_missing_expression_in_for_loop = missing expression to iterate on in `for` loop .suggestion = try adding an expression to the `for` loop +parse_loop_else = `{$loop_kind}...else` loops are not supported + .note = consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + .loop_keyword = `else` is attached to this loop + parse_missing_comma_after_match_arm = expected `,` following `match` arm .suggestion = missing a comma here to end this `match` arm diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index c746a870964..1662db36d10 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -452,6 +452,17 @@ pub(crate) struct MissingExpressionInForLoop { } #[derive(Diagnostic)] +#[diag(parse_loop_else)] +#[note] +pub(crate) struct LoopElseNotSupported { + #[primary_span] + pub span: Span, + pub loop_kind: &'static str, + #[label(parse_loop_keyword)] + pub loop_kw: Span, +} + +#[derive(Diagnostic)] #[diag(parse_missing_comma_after_match_arm)] pub(crate) struct MissingCommaAfterMatchArm { #[primary_span] diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 24d4c17f5d8..b2951e7a184 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2503,9 +2503,27 @@ impl<'a> Parser<'a> { let (attrs, loop_block) = self.parse_inner_attrs_and_block()?; let kind = ExprKind::ForLoop(pat, expr, loop_block, opt_label); + + self.recover_loop_else("for", lo)?; + Ok(self.mk_expr_with_attrs(lo.to(self.prev_token.span), kind, attrs)) } + /// Recovers from an `else` clause after a loop (`for...else`, `while...else`) + fn recover_loop_else(&mut self, loop_kind: &'static str, loop_kw: Span) -> PResult<'a, ()> { + if self.token.is_keyword(kw::Else) && self.may_recover() { + let else_span = self.token.span; + self.bump(); + let else_clause = self.parse_expr_else()?; + self.sess.emit_err(errors::LoopElseNotSupported { + span: else_span.to(else_clause.span), + loop_kind, + loop_kw, + }); + } + Ok(()) + } + fn error_missing_in_for_loop(&mut self) { let (span, sub): (_, fn(_) -> _) = if self.token.is_ident_named(sym::of) { // Possibly using JS syntax (#75311). @@ -2530,6 +2548,9 @@ impl<'a> Parser<'a> { err.span_label(cond.span, "this `while` condition successfully parsed"); err })?; + + self.recover_loop_else("while", lo)?; + Ok(self.mk_expr_with_attrs( lo.to(self.prev_token.span), ExprKind::While(cond, body, opt_label), @@ -2541,6 +2562,7 @@ impl<'a> Parser<'a> { fn parse_expr_loop(&mut self, opt_label: Option<Label>, lo: Span) -> PResult<'a, P<Expr>> { let loop_span = self.prev_token.span; let (attrs, body) = self.parse_inner_attrs_and_block()?; + self.recover_loop_else("loop", lo)?; Ok(self.mk_expr_with_attrs( lo.to(self.prev_token.span), ExprKind::Loop(body, opt_label, loop_span), diff --git a/tests/ui/for/for-else-err.rs b/tests/ui/for/for-else-err.rs new file mode 100644 index 00000000000..16252e980e4 --- /dev/null +++ b/tests/ui/for/for-else-err.rs @@ -0,0 +1,8 @@ +fn main() { + for _ in 0..1 { + //~^ NOTE `else` is attached to this loop + } else { + //~^ ERROR `for...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + } +} diff --git a/tests/ui/for/for-else-err.stderr b/tests/ui/for/for-else-err.stderr new file mode 100644 index 00000000000..b330d107647 --- /dev/null +++ b/tests/ui/for/for-else-err.stderr @@ -0,0 +1,17 @@ +error: `for...else` loops are not supported + --> $DIR/for-else-err.rs:4:7 + | +LL | for _ in 0..1 { + | --- `else` is attached to this loop +LL | +LL | } else { + | _______^ +LL | | +LL | | +LL | | } + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + diff --git a/tests/ui/for/for-else-let-else-err.rs b/tests/ui/for/for-else-let-else-err.rs new file mode 100644 index 00000000000..c0b96f97294 --- /dev/null +++ b/tests/ui/for/for-else-let-else-err.rs @@ -0,0 +1,8 @@ +fn main() { + let _ = for _ in 0..1 { + //~^ NOTE `else` is attached to this loop + } else { + //~^ ERROR `for...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + }; +} diff --git a/tests/ui/for/for-else-let-else-err.stderr b/tests/ui/for/for-else-let-else-err.stderr new file mode 100644 index 00000000000..a2396a8fbb1 --- /dev/null +++ b/tests/ui/for/for-else-let-else-err.stderr @@ -0,0 +1,17 @@ +error: `for...else` loops are not supported + --> $DIR/for-else-let-else-err.rs:4:7 + | +LL | let _ = for _ in 0..1 { + | --- `else` is attached to this loop +LL | +LL | } else { + | _______^ +LL | | +LL | | +LL | | }; + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + diff --git a/tests/ui/let-else/let-else-brace-before-else.fixed b/tests/ui/let-else/let-else-brace-before-else.fixed index a75c770ddf2..2d85e3878cc 100644 --- a/tests/ui/let-else/let-else-brace-before-else.fixed +++ b/tests/ui/let-else/let-else-brace-before-else.fixed @@ -7,10 +7,6 @@ fn main() { //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed return; }; - let Some(1) = (loop { break Some(1) }) else { - //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed - return; - }; let 2 = 1 + (match 1 { n => n }) else { //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed return; diff --git a/tests/ui/let-else/let-else-brace-before-else.rs b/tests/ui/let-else/let-else-brace-before-else.rs index 5603b946f38..5c3375b3f28 100644 --- a/tests/ui/let-else/let-else-brace-before-else.rs +++ b/tests/ui/let-else/let-else-brace-before-else.rs @@ -7,10 +7,6 @@ fn main() { //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed return; }; - let Some(1) = loop { break Some(1) } else { - //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed - return; - }; let 2 = 1 + match 1 { n => n } else { //~^ ERROR right curly brace `}` before `else` in a `let...else` statement not allowed return; diff --git a/tests/ui/let-else/let-else-brace-before-else.stderr b/tests/ui/let-else/let-else-brace-before-else.stderr index cb01e4c18a1..ee53213d8c2 100644 --- a/tests/ui/let-else/let-else-brace-before-else.stderr +++ b/tests/ui/let-else/let-else-brace-before-else.stderr @@ -10,18 +10,7 @@ LL | let Some(1) = ({ Some(1) }) else { | + + error: right curly brace `}` before `else` in a `let...else` statement not allowed - --> $DIR/let-else-brace-before-else.rs:10:40 - | -LL | let Some(1) = loop { break Some(1) } else { - | ^ - | -help: wrap the expression in parentheses - | -LL | let Some(1) = (loop { break Some(1) }) else { - | + + - -error: right curly brace `}` before `else` in a `let...else` statement not allowed - --> $DIR/let-else-brace-before-else.rs:14:34 + --> $DIR/let-else-brace-before-else.rs:10:34 | LL | let 2 = 1 + match 1 { n => n } else { | ^ @@ -32,7 +21,7 @@ LL | let 2 = 1 + (match 1 { n => n }) else { | + + error: right curly brace `}` before `else` in a `let...else` statement not allowed - --> $DIR/let-else-brace-before-else.rs:18:40 + --> $DIR/let-else-brace-before-else.rs:14:40 | LL | let Some(1) = unsafe { unsafe_fn() } else { | ^ @@ -42,5 +31,5 @@ help: wrap the expression in parentheses LL | let Some(1) = (unsafe { unsafe_fn() }) else { | + + -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors diff --git a/tests/ui/loops/loop-else-break-with-value.rs b/tests/ui/loops/loop-else-break-with-value.rs new file mode 100644 index 00000000000..670d8a145c0 --- /dev/null +++ b/tests/ui/loops/loop-else-break-with-value.rs @@ -0,0 +1,10 @@ +fn main() { + let Some(1) = loop { + //~^ NOTE `else` is attached to this loop + break Some(1) + } else { + //~^ ERROR `loop...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + return; + }; +} diff --git a/tests/ui/loops/loop-else-break-with-value.stderr b/tests/ui/loops/loop-else-break-with-value.stderr new file mode 100644 index 00000000000..972e2d341ec --- /dev/null +++ b/tests/ui/loops/loop-else-break-with-value.stderr @@ -0,0 +1,18 @@ +error: `loop...else` loops are not supported + --> $DIR/loop-else-break-with-value.rs:5:7 + | +LL | let Some(1) = loop { + | ---- `else` is attached to this loop +... +LL | } else { + | _______^ +LL | | +LL | | +LL | | return; +LL | | }; + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + diff --git a/tests/ui/loops/loop-else-err.rs b/tests/ui/loops/loop-else-err.rs new file mode 100644 index 00000000000..202a09c255c --- /dev/null +++ b/tests/ui/loops/loop-else-err.rs @@ -0,0 +1,8 @@ +fn main() { + loop { + //~^ NOTE `else` is attached to this loop + } else { + //~^ ERROR `loop...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + } +} diff --git a/tests/ui/loops/loop-else-err.stderr b/tests/ui/loops/loop-else-err.stderr new file mode 100644 index 00000000000..c2c5c84cded --- /dev/null +++ b/tests/ui/loops/loop-else-err.stderr @@ -0,0 +1,17 @@ +error: `loop...else` loops are not supported + --> $DIR/loop-else-err.rs:4:7 + | +LL | loop { + | ---- `else` is attached to this loop +LL | +LL | } else { + | _______^ +LL | | +LL | | +LL | | } + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + diff --git a/tests/ui/loops/loop-else-let-else-err.rs b/tests/ui/loops/loop-else-let-else-err.rs new file mode 100644 index 00000000000..e828ffef549 --- /dev/null +++ b/tests/ui/loops/loop-else-let-else-err.rs @@ -0,0 +1,8 @@ +fn main() { + let _ = loop { + //~^ NOTE `else` is attached to this loop + } else { + //~^ ERROR `loop...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + }; +} diff --git a/tests/ui/loops/loop-else-let-else-err.stderr b/tests/ui/loops/loop-else-let-else-err.stderr new file mode 100644 index 00000000000..a57c784ff6f --- /dev/null +++ b/tests/ui/loops/loop-else-let-else-err.stderr @@ -0,0 +1,17 @@ +error: `loop...else` loops are not supported + --> $DIR/loop-else-let-else-err.rs:4:7 + | +LL | let _ = loop { + | ---- `else` is attached to this loop +LL | +LL | } else { + | _______^ +LL | | +LL | | +LL | | }; + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + diff --git a/tests/ui/while/while-else-err.rs b/tests/ui/while/while-else-err.rs new file mode 100644 index 00000000000..36b60fbd4be --- /dev/null +++ b/tests/ui/while/while-else-err.rs @@ -0,0 +1,8 @@ +fn main() { + while false { + //~^ NOTE `else` is attached to this loop + } else { + //~^ ERROR `while...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + }; +} diff --git a/tests/ui/while/while-else-err.stderr b/tests/ui/while/while-else-err.stderr new file mode 100644 index 00000000000..88f715d5666 --- /dev/null +++ b/tests/ui/while/while-else-err.stderr @@ -0,0 +1,17 @@ +error: `while...else` loops are not supported + --> $DIR/while-else-err.rs:4:7 + | +LL | while false { + | ----- `else` is attached to this loop +LL | +LL | } else { + | _______^ +LL | | +LL | | +LL | | }; + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + diff --git a/tests/ui/while/while-else-let-else-err.rs b/tests/ui/while/while-else-let-else-err.rs new file mode 100644 index 00000000000..6d9909347c3 --- /dev/null +++ b/tests/ui/while/while-else-let-else-err.rs @@ -0,0 +1,8 @@ +fn main() { + let _ = while false { + //~^ NOTE `else` is attached to this loop + } else { + //~^ ERROR `while...else` loops are not supported + //~| NOTE consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + }; +} diff --git a/tests/ui/while/while-else-let-else-err.stderr b/tests/ui/while/while-else-let-else-err.stderr new file mode 100644 index 00000000000..431d37c007c --- /dev/null +++ b/tests/ui/while/while-else-let-else-err.stderr @@ -0,0 +1,17 @@ +error: `while...else` loops are not supported + --> $DIR/while-else-let-else-err.rs:4:7 + | +LL | let _ = while false { + | ----- `else` is attached to this loop +LL | +LL | } else { + | _______^ +LL | | +LL | | +LL | | }; + | |_____^ + | + = note: consider moving this `else` clause to a separate `if` statement and use a `bool` variable to control if it should run + +error: aborting due to previous error + |
