about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2022-11-04 21:16:20 +0000
committerMichael Goulet <michael@errs.io>2022-11-04 21:35:58 +0000
commit4e7fc663cc674a33cc7078096ad1b0269618c456 (patch)
treec24495e83147d42d40e94f01c2519ce0539d78e4
parentc2a5c3a50fc3fb6d16cd140f55f7db61cbf08a01 (diff)
downloadrust-4e7fc663cc674a33cc7078096ad1b0269618c456.tar.gz
rust-4e7fc663cc674a33cc7078096ad1b0269618c456.zip
Don't silently eat label before block in block-like expr
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs12
-rw-r--r--src/test/ui/parser/label-after-block-like.rs43
-rw-r--r--src/test/ui/parser/label-after-block-like.stderr176
3 files changed, 227 insertions, 4 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index 7dc4fd0044f..c609aa93da3 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -2468,11 +2468,15 @@ impl<'a> Parser<'a> {
     }
 
     pub(crate) fn maybe_recover_unexpected_block_label(&mut self) -> bool {
-        let Some(label) = self.eat_label().filter(|_| {
-            self.eat(&token::Colon) && self.token.kind == token::OpenDelim(Delimiter::Brace)
-        }) else {
+        // Check for `'a : {`
+        if !(self.check_lifetime()
+            && self.look_ahead(1, |tok| tok.kind == token::Colon)
+            && self.look_ahead(2, |tok| tok.kind == token::OpenDelim(Delimiter::Brace)))
+        {
             return false;
-        };
+        }
+        let label = self.eat_label().expect("just checked if a label exists");
+        self.bump(); // eat `:`
         let span = label.ident.span.to(self.prev_token.span);
         let mut err = self.struct_span_err(span, "block label not supported here");
         err.span_label(span, "not supported here");
diff --git a/src/test/ui/parser/label-after-block-like.rs b/src/test/ui/parser/label-after-block-like.rs
new file mode 100644
index 00000000000..55f3f8f9f5f
--- /dev/null
+++ b/src/test/ui/parser/label-after-block-like.rs
@@ -0,0 +1,43 @@
+fn a() {
+    if let () = () 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn b() {
+    if true 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn c() {
+    loop 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn d() {
+    while true 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn e() {
+    while let () = () 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn f() {
+    for _ in 0..0 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn g() {
+    unsafe 'a {}
+    //~^ ERROR labeled expression must be followed by `:`
+    //~| ERROR expected `{`, found `'a`
+}
+
+fn main() {}
diff --git a/src/test/ui/parser/label-after-block-like.stderr b/src/test/ui/parser/label-after-block-like.stderr
new file mode 100644
index 00000000000..8ff50b124b3
--- /dev/null
+++ b/src/test/ui/parser/label-after-block-like.stderr
@@ -0,0 +1,176 @@
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:2:20
+   |
+LL |     if let () = () 'a {}
+   |                    ---^^
+   |                    | |
+   |                    | help: add `:` after the label
+   |                    the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:2:20
+   |
+LL |     if let () = () 'a {}
+   |                    ^^ expected `{`
+   |
+note: the `if` expression is missing a block after this condition
+  --> $DIR/label-after-block-like.rs:2:8
+   |
+LL |     if let () = () 'a {}
+   |        ^^^^^^^^^^^
+help: try placing this code inside a block
+   |
+LL |     if let () = () { 'a {} }
+   |                    +       +
+
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:8:13
+   |
+LL |     if true 'a {}
+   |             ---^^
+   |             | |
+   |             | help: add `:` after the label
+   |             the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:8:13
+   |
+LL |     if true 'a {}
+   |             ^^ expected `{`
+   |
+note: the `if` expression is missing a block after this condition
+  --> $DIR/label-after-block-like.rs:8:8
+   |
+LL |     if true 'a {}
+   |        ^^^^
+help: try placing this code inside a block
+   |
+LL |     if true { 'a {} }
+   |             +       +
+
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:14:10
+   |
+LL |     loop 'a {}
+   |          ---^^
+   |          | |
+   |          | help: add `:` after the label
+   |          the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:14:10
+   |
+LL |     loop 'a {}
+   |     ---- ^^ expected `{`
+   |     |
+   |     while parsing this `loop` expression
+   |
+help: try placing this code inside a block
+   |
+LL |     loop { 'a {} }
+   |          +       +
+
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:20:16
+   |
+LL |     while true 'a {}
+   |                ---^^
+   |                | |
+   |                | help: add `:` after the label
+   |                the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:20:16
+   |
+LL |     while true 'a {}
+   |     ----- ---- ^^ expected `{`
+   |     |     |
+   |     |     this `while` condition successfully parsed
+   |     while parsing the body of this `while` expression
+   |
+help: try placing this code inside a block
+   |
+LL |     while true { 'a {} }
+   |                +       +
+
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:26:23
+   |
+LL |     while let () = () 'a {}
+   |                       ---^^
+   |                       | |
+   |                       | help: add `:` after the label
+   |                       the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:26:23
+   |
+LL |     while let () = () 'a {}
+   |     ----- ----------- ^^ expected `{`
+   |     |     |
+   |     |     this `while` condition successfully parsed
+   |     while parsing the body of this `while` expression
+   |
+help: try placing this code inside a block
+   |
+LL |     while let () = () { 'a {} }
+   |                       +       +
+
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:32:19
+   |
+LL |     for _ in 0..0 'a {}
+   |                   ---^^
+   |                   | |
+   |                   | help: add `:` after the label
+   |                   the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:32:19
+   |
+LL |     for _ in 0..0 'a {}
+   |                   ^^ expected `{`
+   |
+help: try placing this code inside a block
+   |
+LL |     for _ in 0..0 { 'a {} }
+   |                   +       +
+
+error: labeled expression must be followed by `:`
+  --> $DIR/label-after-block-like.rs:38:12
+   |
+LL |     unsafe 'a {}
+   |            ---^^
+   |            | |
+   |            | help: add `:` after the label
+   |            the label
+   |
+   = note: labels are used before loops and blocks, allowing e.g., `break 'label` to them
+
+error: expected `{`, found `'a`
+  --> $DIR/label-after-block-like.rs:38:12
+   |
+LL |     unsafe 'a {}
+   |     ------ ^^ expected `{`
+   |     |
+   |     while parsing this `unsafe` expression
+   |
+help: try placing this code inside a block
+   |
+LL |     unsafe { 'a {} }
+   |            +       +
+
+error: aborting due to 14 previous errors
+