about summary refs log tree commit diff
path: root/tests/ui/loops
diff options
context:
space:
mode:
authorZachary S <zasample18+github@gmail.com>2025-03-17 00:25:15 -0500
committerZachary S <zasample18+github@gmail.com>2025-03-17 01:59:37 -0500
commitf478853f425fc0207add653b48c49c937acaa94e (patch)
tree3f7fe7d662114e8b9f812c37cfe967072858265d /tests/ui/loops
parent227690a258492c84ae9927d18289208d0180e62f (diff)
downloadrust-f478853f425fc0207add653b48c49c937acaa94e.tar.gz
rust-f478853f425fc0207add653b48c49c937acaa94e.zip
If a label is placed on the block of a loop instead of the header, suggest moving it to the header.
Diffstat (limited to 'tests/ui/loops')
-rw-r--r--tests/ui/loops/label-on-block-suggest-move.rs90
-rw-r--r--tests/ui/loops/label-on-block-suggest-move.stderr140
2 files changed, 230 insertions, 0 deletions
diff --git a/tests/ui/loops/label-on-block-suggest-move.rs b/tests/ui/loops/label-on-block-suggest-move.rs
new file mode 100644
index 00000000000..656034cd0e9
--- /dev/null
+++ b/tests/ui/loops/label-on-block-suggest-move.rs
@@ -0,0 +1,90 @@
+// see https://github.com/rust-lang/rust/issues/138585
+#![allow(break_with_label_and_loop)] // doesn't work locally
+
+fn main() {
+    loop 'a: {}
+    //~^ ERROR: block label not supported here
+    //~| HELP: if you meant to label the loop, move this label before the loop
+    while false 'a: {}
+    //~^ ERROR: block label not supported here
+    //~| HELP: if you meant to label the loop, move this label before the loop
+    for i in [0] 'a: {}
+    //~^ ERROR: block label not supported here
+    //~| HELP: if you meant to label the loop, move this label before the loop
+    'a: loop {
+        // first block is parsed as the break expr's value with or without parens
+        while break 'a 'b: {} 'c: {}
+        //~^ ERROR: block label not supported here
+        //~| HELP: if you meant to label the loop, move this label before the loop
+        while break 'a ('b: {}) 'c: {}
+        //~^ ERROR: block label not supported here
+        //~| HELP: if you meant to label the loop, move this label before the loop
+
+        // without the parens, the first block is parsed as the while-loop's body
+        // (see the 'no errors' section)
+        // #[allow(break_with_label_and_loop)] (doesn't work locally)
+        while (break 'a {}) 'c: {}
+        //~^ ERROR: block label not supported here
+        //~| HELP: if you meant to label the loop, move this label before the loop
+    }
+
+    // do not suggest moving the label if there is already a label on the loop
+    'a: loop 'b: {}
+    //~^ ERROR: block label not supported here
+    //~| HELP: remove this block label
+    'a: while false 'b: {}
+    //~^ ERROR: block label not supported here
+    //~| HELP: remove this block label
+    'a: for i in [0] 'b: {}
+    //~^ ERROR: block label not supported here
+    //~| HELP: remove this block label
+    'a: loop {
+        // first block is parsed as the break expr's value with or without parens
+        'd: while break 'a 'b: {} 'c: {}
+        //~^ ERROR: block label not supported here
+        //~| HELP: remove this block label
+        'd: while break 'a ('b: {}) 'c: {}
+        //~^ ERROR: block label not supported here
+        //~| HELP: remove this block label
+
+        // without the parens, the first block is parsed as the while-loop's body
+        // (see the 'no errors' section)
+        // #[allow(break_with_label_and_loop)] (doesn't work locally)
+        'd: while (break 'a {}) 'c: {}
+        //~^ ERROR: block label not supported here
+        //~| HELP: remove this block label
+    }
+
+    // no errors
+    loop { 'a: {} }
+    'a: loop { 'b: {} }
+    while false { 'a: {} }
+    'a: while false { 'b: {} }
+    for i in [0] { 'a: {} }
+    'a: for i in [0] { 'b: {} }
+    'a: {}
+    'a: { 'b: {} }
+    'a: loop {
+        // first block is parsed as the break expr's value if it is a labeled block
+        while break 'a 'b: {} {}
+        'd: while break 'a 'b: {} {}
+        while break 'a ('b: {}) {}
+        'd: while break 'a ('b: {}) {}
+        // first block is parsed as the while-loop's body if it has no label
+        // (the break expr is parsed as having no value),
+        // so the second block is a normal stmt-block, and the label is allowed
+        while break 'a {} 'c: {}
+        while break 'a {} {}
+        'd: while break 'a {} 'c: {}
+        'd: while break 'a {} {}
+    }
+
+    // unrelated errors that should not be affected
+    'a: 'b: {}
+    //~^ ERROR: expected `while`, `for`, `loop` or `{` after a label
+    //~| HELP: consider removing the label
+    loop { while break 'b: {} {} }
+    //~^ ERROR: parentheses are required around this expression to avoid confusion with a labeled break expression
+    //~| HELP: wrap the expression in parentheses
+    //~| ERROR: `break` or `continue` with no label in the condition of a `while` loop [E0590]
+}
diff --git a/tests/ui/loops/label-on-block-suggest-move.stderr b/tests/ui/loops/label-on-block-suggest-move.stderr
new file mode 100644
index 00000000000..66866703ca6
--- /dev/null
+++ b/tests/ui/loops/label-on-block-suggest-move.stderr
@@ -0,0 +1,140 @@
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:5:10
+   |
+LL |     loop 'a: {}
+   |          ^^^ not supported here
+   |
+help: if you meant to label the loop, move this label before the loop
+   |
+LL -     loop 'a: {}
+LL +     'a: loop {}
+   |
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:8:17
+   |
+LL |     while false 'a: {}
+   |                 ^^^ not supported here
+   |
+help: if you meant to label the loop, move this label before the loop
+   |
+LL -     while false 'a: {}
+LL +     'a: while false {}
+   |
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:11:18
+   |
+LL |     for i in [0] 'a: {}
+   |                  ^^^ not supported here
+   |
+help: if you meant to label the loop, move this label before the loop
+   |
+LL -     for i in [0] 'a: {}
+LL +     'a: for i in [0] {}
+   |
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:16:31
+   |
+LL |         while break 'a 'b: {} 'c: {}
+   |                               ^^^ not supported here
+   |
+help: if you meant to label the loop, move this label before the loop
+   |
+LL -         while break 'a 'b: {} 'c: {}
+LL +         'c: while break 'a 'b: {} {}
+   |
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:19:33
+   |
+LL |         while break 'a ('b: {}) 'c: {}
+   |                                 ^^^ not supported here
+   |
+help: if you meant to label the loop, move this label before the loop
+   |
+LL -         while break 'a ('b: {}) 'c: {}
+LL +         'c: while break 'a ('b: {}) {}
+   |
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:26:29
+   |
+LL |         while (break 'a {}) 'c: {}
+   |                             ^^^ not supported here
+   |
+help: if you meant to label the loop, move this label before the loop
+   |
+LL -         while (break 'a {}) 'c: {}
+LL +         'c: while (break 'a {}) {}
+   |
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:32:14
+   |
+LL |     'a: loop 'b: {}
+   |              ^^^ not supported here
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:35:21
+   |
+LL |     'a: while false 'b: {}
+   |                     ^^^ not supported here
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:38:22
+   |
+LL |     'a: for i in [0] 'b: {}
+   |                      ^^^ not supported here
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:43:35
+   |
+LL |         'd: while break 'a 'b: {} 'c: {}
+   |                                   ^^^ not supported here
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:46:37
+   |
+LL |         'd: while break 'a ('b: {}) 'c: {}
+   |                                     ^^^ not supported here
+
+error: block label not supported here
+  --> $DIR/label-on-block-suggest-move.rs:53:33
+   |
+LL |         'd: while (break 'a {}) 'c: {}
+   |                                 ^^^ not supported here
+
+error: expected `while`, `for`, `loop` or `{` after a label
+  --> $DIR/label-on-block-suggest-move.rs:83:9
+   |
+LL |     'a: 'b: {}
+   |         ^^ expected `while`, `for`, `loop` or `{` after a label
+   |
+help: consider removing the label
+   |
+LL -     'a: 'b: {}
+LL +     'b: {}
+   |
+
+error: parentheses are required around this expression to avoid confusion with a labeled break expression
+  --> $DIR/label-on-block-suggest-move.rs:86:24
+   |
+LL |     loop { while break 'b: {} {} }
+   |                        ^^^^^^
+   |
+help: wrap the expression in parentheses
+   |
+LL |     loop { while break ('b: {}) {} }
+   |                        +      +
+
+error[E0590]: `break` or `continue` with no label in the condition of a `while` loop
+  --> $DIR/label-on-block-suggest-move.rs:86:18
+   |
+LL |     loop { while break 'b: {} {} }
+   |                  ^^^^^^^^^^^^ unlabeled `break` in the condition of a `while` loop
+
+error: aborting due to 15 previous errors
+
+For more information about this error, try `rustc --explain E0590`.