about summary refs log tree commit diff
path: root/tests/ui/loops
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/loops')
-rw-r--r--tests/ui/loops/for-each-loop-panic.rs9
-rw-r--r--tests/ui/loops/issue-82916.rs10
-rw-r--r--tests/ui/loops/issue-82916.stderr21
-rw-r--r--tests/ui/loops/loop-break-unsize.rs8
-rw-r--r--tests/ui/loops/loop-break-value-no-repeat.rs14
-rw-r--r--tests/ui/loops/loop-break-value-no-repeat.stderr16
-rw-r--r--tests/ui/loops/loop-break-value.rs98
-rw-r--r--tests/ui/loops/loop-break-value.stderr193
-rw-r--r--tests/ui/loops/loop-labeled-break-value.rs11
-rw-r--r--tests/ui/loops/loop-labeled-break-value.stderr30
-rw-r--r--tests/ui/loops/loop-no-implicit-break.rs31
-rw-r--r--tests/ui/loops/loop-no-implicit-break.stderr47
-rw-r--r--tests/ui/loops/loop-proper-liveness.rs32
-rw-r--r--tests/ui/loops/loop-proper-liveness.stderr18
-rw-r--r--tests/ui/loops/loop-properly-diverging-2.rs6
-rw-r--r--tests/ui/loops/loop-properly-diverging-2.stderr12
16 files changed, 556 insertions, 0 deletions
diff --git a/tests/ui/loops/for-each-loop-panic.rs b/tests/ui/loops/for-each-loop-panic.rs
new file mode 100644
index 00000000000..5156999f4db
--- /dev/null
+++ b/tests/ui/loops/for-each-loop-panic.rs
@@ -0,0 +1,9 @@
+// run-fail
+// error-pattern:moop
+// ignore-emscripten no processes
+
+fn main() {
+    for _ in 0_usize..10_usize {
+        panic!("moop");
+    }
+}
diff --git a/tests/ui/loops/issue-82916.rs b/tests/ui/loops/issue-82916.rs
new file mode 100644
index 00000000000..8633ea1e8cb
--- /dev/null
+++ b/tests/ui/loops/issue-82916.rs
@@ -0,0 +1,10 @@
+struct S(i32);
+
+fn foo(x: Vec<S>) {
+    for y in x {
+
+    }
+    let z = x; //~ ERROR use of moved value: `x`
+}
+
+fn main() {}
diff --git a/tests/ui/loops/issue-82916.stderr b/tests/ui/loops/issue-82916.stderr
new file mode 100644
index 00000000000..e6a60d7bc40
--- /dev/null
+++ b/tests/ui/loops/issue-82916.stderr
@@ -0,0 +1,21 @@
+error[E0382]: use of moved value: `x`
+  --> $DIR/issue-82916.rs:7:13
+   |
+LL | fn foo(x: Vec<S>) {
+   |        - move occurs because `x` has type `Vec<S>`, which does not implement the `Copy` trait
+LL |     for y in x {
+   |              - `x` moved due to this implicit call to `.into_iter()`
+...
+LL |     let z = x;
+   |             ^ value used here after move
+   |
+note: `into_iter` takes ownership of the receiver `self`, which moves `x`
+  --> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
+help: consider iterating over a slice of the `Vec<S>`'s content to avoid moving into the `for` loop
+   |
+LL |     for y in &x {
+   |              +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0382`.
diff --git a/tests/ui/loops/loop-break-unsize.rs b/tests/ui/loops/loop-break-unsize.rs
new file mode 100644
index 00000000000..974c63cea85
--- /dev/null
+++ b/tests/ui/loops/loop-break-unsize.rs
@@ -0,0 +1,8 @@
+// Regression test for #62312
+// check-pass
+
+fn main() {
+    let _ = loop {
+        break Box::new(()) as Box<dyn Send>;
+    };
+}
diff --git a/tests/ui/loops/loop-break-value-no-repeat.rs b/tests/ui/loops/loop-break-value-no-repeat.rs
new file mode 100644
index 00000000000..1c0b7a0181f
--- /dev/null
+++ b/tests/ui/loops/loop-break-value-no-repeat.rs
@@ -0,0 +1,14 @@
+#![allow(unused_variables)]
+
+use std::ptr;
+
+// Test that we only report **one** error here and that is that
+// `break` with an expression is illegal in this context. In
+// particular, we don't report any mismatched types error, which is
+// besides the point.
+
+fn main() {
+    for _ in &[1,2,3] {
+        break 22 //~ ERROR `break` with value from a `for` loop
+    }
+}
diff --git a/tests/ui/loops/loop-break-value-no-repeat.stderr b/tests/ui/loops/loop-break-value-no-repeat.stderr
new file mode 100644
index 00000000000..605a1841cf3
--- /dev/null
+++ b/tests/ui/loops/loop-break-value-no-repeat.stderr
@@ -0,0 +1,16 @@
+error[E0571]: `break` with value from a `for` loop
+  --> $DIR/loop-break-value-no-repeat.rs:12:9
+   |
+LL |     for _ in &[1,2,3] {
+   |     ----------------- you can't `break` with a value in a `for` loop
+LL |         break 22
+   |         ^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `for` loop
+   |
+LL |         break
+   |         ~~~~~
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0571`.
diff --git a/tests/ui/loops/loop-break-value.rs b/tests/ui/loops/loop-break-value.rs
new file mode 100644
index 00000000000..51c9a36a039
--- /dev/null
+++ b/tests/ui/loops/loop-break-value.rs
@@ -0,0 +1,98 @@
+#![feature(never_type)]
+
+fn main() {
+    let val: ! = loop { break break; };
+    //~^ ERROR mismatched types
+
+    loop {
+        if true {
+            break "asdf";
+        } else {
+            break 123; //~ ERROR mismatched types
+        }
+    };
+
+    let _: i32 = loop {
+        break "asdf"; //~ ERROR mismatched types
+    };
+
+    let _: i32 = 'outer_loop: loop {
+        loop {
+            break 'outer_loop "nope"; //~ ERROR mismatched types
+            break "ok";
+        };
+    };
+
+    'while_loop: while true { //~ WARN denote infinite loops with
+        break;
+        break (); //~ ERROR `break` with value from a `while` loop
+        loop {
+            break 'while_loop 123;
+            //~^ ERROR `break` with value from a `while` loop
+            break 456;
+            break 789;
+        };
+    }
+
+    while let Some(_) = Some(()) {
+        if break () { //~ ERROR `break` with value from a `while` loop
+        }
+    }
+
+    while let Some(_) = Some(()) {
+        break None;
+        //~^ ERROR `break` with value from a `while` loop
+    }
+
+    'while_let_loop: while let Some(_) = Some(()) {
+        loop {
+            break 'while_let_loop "nope";
+            //~^ ERROR `break` with value from a `while` loop
+            break 33;
+        };
+    }
+
+    for _ in &[1,2,3] {
+        break (); //~ ERROR `break` with value from a `for` loop
+        break [()];
+        //~^ ERROR `break` with value from a `for` loop
+    }
+
+    'for_loop: for _ in &[1,2,3] {
+        loop {
+            break Some(3);
+            break 'for_loop Some(17);
+            //~^ ERROR `break` with value from a `for` loop
+        };
+    }
+
+    let _: i32 = 'a: loop {
+        let _: () = 'b: loop {
+            break ('c: loop {
+                break;
+                break 'c 123; //~ ERROR mismatched types
+            });
+            break 'a 123;
+        };
+    };
+
+    loop {
+        break (break, break); //~ ERROR mismatched types
+    };
+
+    loop {
+        break;
+        break 2; //~ ERROR mismatched types
+    };
+
+    loop {
+        break 2;
+        break; //~ ERROR mismatched types
+        break 4;
+    };
+
+    'LOOP: for _ in 0 .. 9 {
+        break LOOP;
+        //~^ ERROR cannot find value `LOOP` in this scope
+    }
+}
diff --git a/tests/ui/loops/loop-break-value.stderr b/tests/ui/loops/loop-break-value.stderr
new file mode 100644
index 00000000000..ccb27c35070
--- /dev/null
+++ b/tests/ui/loops/loop-break-value.stderr
@@ -0,0 +1,193 @@
+error[E0425]: cannot find value `LOOP` in this scope
+  --> $DIR/loop-break-value.rs:95:15
+   |
+LL |     'LOOP: for _ in 0 .. 9 {
+   |     ----- a label with a similar name exists
+LL |         break LOOP;
+   |               ^^^^
+   |               |
+   |               not found in this scope
+   |               help: use the similarly named label: `'LOOP`
+
+warning: denote infinite loops with `loop { ... }`
+  --> $DIR/loop-break-value.rs:26:5
+   |
+LL |     'while_loop: while true {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^ help: use `loop`
+   |
+   = note: `#[warn(while_true)]` on by default
+
+error[E0571]: `break` with value from a `while` loop
+  --> $DIR/loop-break-value.rs:28:9
+   |
+LL |     'while_loop: while true {
+   |     ----------------------- you can't `break` with a value in a `while` loop
+LL |         break;
+LL |         break ();
+   |         ^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `while` loop
+   |
+LL |         break;
+   |         ~~~~~
+help: alternatively, you might have meant to use the available loop label
+   |
+LL |         break 'while_loop;
+   |               ~~~~~~~~~~~
+
+error[E0571]: `break` with value from a `while` loop
+  --> $DIR/loop-break-value.rs:30:13
+   |
+LL |     'while_loop: while true {
+   |     ----------------------- you can't `break` with a value in a `while` loop
+...
+LL |             break 'while_loop 123;
+   |             ^^^^^^^^^^^^^^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `while` loop
+   |
+LL |             break 'while_loop;
+   |             ~~~~~~~~~~~~~~~~~
+
+error[E0571]: `break` with value from a `while` loop
+  --> $DIR/loop-break-value.rs:38:12
+   |
+LL |     while let Some(_) = Some(()) {
+   |     ---------------------------- you can't `break` with a value in a `while` loop
+LL |         if break () {
+   |            ^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `while` loop
+   |
+LL |         if break {
+   |            ~~~~~
+
+error[E0571]: `break` with value from a `while` loop
+  --> $DIR/loop-break-value.rs:43:9
+   |
+LL |     while let Some(_) = Some(()) {
+   |     ---------------------------- you can't `break` with a value in a `while` loop
+LL |         break None;
+   |         ^^^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `while` loop
+   |
+LL |         break;
+   |         ~~~~~
+
+error[E0571]: `break` with value from a `while` loop
+  --> $DIR/loop-break-value.rs:49:13
+   |
+LL |     'while_let_loop: while let Some(_) = Some(()) {
+   |     --------------------------------------------- you can't `break` with a value in a `while` loop
+LL |         loop {
+LL |             break 'while_let_loop "nope";
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `while` loop
+   |
+LL |             break 'while_let_loop;
+   |             ~~~~~~~~~~~~~~~~~~~~~
+
+error[E0571]: `break` with value from a `for` loop
+  --> $DIR/loop-break-value.rs:56:9
+   |
+LL |     for _ in &[1,2,3] {
+   |     ----------------- you can't `break` with a value in a `for` loop
+LL |         break ();
+   |         ^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `for` loop
+   |
+LL |         break;
+   |         ~~~~~
+
+error[E0571]: `break` with value from a `for` loop
+  --> $DIR/loop-break-value.rs:57:9
+   |
+LL |     for _ in &[1,2,3] {
+   |     ----------------- you can't `break` with a value in a `for` loop
+LL |         break ();
+LL |         break [()];
+   |         ^^^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `for` loop
+   |
+LL |         break;
+   |         ~~~~~
+
+error[E0571]: `break` with value from a `for` loop
+  --> $DIR/loop-break-value.rs:64:13
+   |
+LL |     'for_loop: for _ in &[1,2,3] {
+   |     ---------------------------- you can't `break` with a value in a `for` loop
+...
+LL |             break 'for_loop Some(17);
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^ can only break with a value inside `loop` or breakable block
+   |
+help: use `break` on its own without a value inside this `for` loop
+   |
+LL |             break 'for_loop;
+   |             ~~~~~~~~~~~~~~~
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:4:31
+   |
+LL |     let val: ! = loop { break break; };
+   |                               ^^^^^ expected `!`, found `()`
+   |
+   = note:   expected type `!`
+           found unit type `()`
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:11:19
+   |
+LL |             break 123;
+   |                   ^^^ expected `&str`, found integer
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:16:15
+   |
+LL |         break "asdf";
+   |               ^^^^^^ expected `i32`, found `&str`
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:21:31
+   |
+LL |             break 'outer_loop "nope";
+   |                               ^^^^^^ expected `i32`, found `&str`
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:73:26
+   |
+LL |                 break 'c 123;
+   |                          ^^^ expected `()`, found integer
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:80:15
+   |
+LL |         break (break, break);
+   |               ^^^^^^^^^^^^^^ expected `()`, found tuple
+   |
+   = note: expected unit type `()`
+                  found tuple `(!, !)`
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:85:15
+   |
+LL |         break 2;
+   |               ^ expected `()`, found integer
+
+error[E0308]: mismatched types
+  --> $DIR/loop-break-value.rs:90:9
+   |
+LL |         break;
+   |         ^^^^^
+   |         |
+   |         expected integer, found `()`
+   |         help: give it a value of the expected type: `break value`
+
+error: aborting due to 17 previous errors; 1 warning emitted
+
+Some errors have detailed explanations: E0308, E0425, E0571.
+For more information about an error, try `rustc --explain E0308`.
diff --git a/tests/ui/loops/loop-labeled-break-value.rs b/tests/ui/loops/loop-labeled-break-value.rs
new file mode 100644
index 00000000000..3488b057bc5
--- /dev/null
+++ b/tests/ui/loops/loop-labeled-break-value.rs
@@ -0,0 +1,11 @@
+fn main() {
+    loop {
+        let _: i32 = loop { break }; //~ ERROR mismatched types
+    }
+    loop {
+        let _: i32 = 'inner: loop { break 'inner }; //~ ERROR mismatched types
+    }
+    loop {
+        let _: i32 = 'inner2: loop { loop { break 'inner2 } }; //~ ERROR mismatched types
+    }
+}
diff --git a/tests/ui/loops/loop-labeled-break-value.stderr b/tests/ui/loops/loop-labeled-break-value.stderr
new file mode 100644
index 00000000000..aa04d330f25
--- /dev/null
+++ b/tests/ui/loops/loop-labeled-break-value.stderr
@@ -0,0 +1,30 @@
+error[E0308]: mismatched types
+  --> $DIR/loop-labeled-break-value.rs:3:29
+   |
+LL |         let _: i32 = loop { break };
+   |                             ^^^^^
+   |                             |
+   |                             expected `i32`, found `()`
+   |                             help: give it a value of the expected type: `break 42`
+
+error[E0308]: mismatched types
+  --> $DIR/loop-labeled-break-value.rs:6:37
+   |
+LL |         let _: i32 = 'inner: loop { break 'inner };
+   |                                     ^^^^^^^^^^^^
+   |                                     |
+   |                                     expected `i32`, found `()`
+   |                                     help: give it a value of the expected type: `break 'inner 42`
+
+error[E0308]: mismatched types
+  --> $DIR/loop-labeled-break-value.rs:9:45
+   |
+LL |         let _: i32 = 'inner2: loop { loop { break 'inner2 } };
+   |                                             ^^^^^^^^^^^^^
+   |                                             |
+   |                                             expected `i32`, found `()`
+   |                                             help: give it a value of the expected type: `break 'inner2 42`
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/loops/loop-no-implicit-break.rs b/tests/ui/loops/loop-no-implicit-break.rs
new file mode 100644
index 00000000000..93078cb4b14
--- /dev/null
+++ b/tests/ui/loops/loop-no-implicit-break.rs
@@ -0,0 +1,31 @@
+fn main() {
+    let a: i8 = loop {
+        1 //~ ERROR mismatched types
+    };
+
+    let b: i8 = loop {
+        break 1;
+    };
+}
+
+fn foo() -> i8 {
+    let a: i8 = loop {
+        1 //~ ERROR mismatched types
+    };
+
+    let b: i8 = loop {
+        break 1;
+    };
+
+    loop {
+        1 //~ ERROR mismatched types
+    }
+
+    loop {
+        return 1;
+    }
+
+    loop {
+        1 //~ ERROR mismatched types
+    }
+}
diff --git a/tests/ui/loops/loop-no-implicit-break.stderr b/tests/ui/loops/loop-no-implicit-break.stderr
new file mode 100644
index 00000000000..8a1afdea249
--- /dev/null
+++ b/tests/ui/loops/loop-no-implicit-break.stderr
@@ -0,0 +1,47 @@
+error[E0308]: mismatched types
+  --> $DIR/loop-no-implicit-break.rs:3:9
+   |
+LL |         1
+   |         ^ expected `()`, found integer
+   |
+help: you might have meant to break the loop with this value
+   |
+LL |         break 1;
+   |         +++++  +
+
+error[E0308]: mismatched types
+  --> $DIR/loop-no-implicit-break.rs:13:9
+   |
+LL |         1
+   |         ^ expected `()`, found integer
+   |
+help: you might have meant to break the loop with this value
+   |
+LL |         break 1;
+   |         +++++  +
+
+error[E0308]: mismatched types
+  --> $DIR/loop-no-implicit-break.rs:21:9
+   |
+LL |         1
+   |         ^ expected `()`, found integer
+   |
+help: you might have meant to return this value
+   |
+LL |         return 1;
+   |         ++++++  +
+
+error[E0308]: mismatched types
+  --> $DIR/loop-no-implicit-break.rs:29:9
+   |
+LL |         1
+   |         ^ expected `()`, found integer
+   |
+help: you might have meant to return this value
+   |
+LL |         return 1;
+   |         ++++++  +
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/tests/ui/loops/loop-proper-liveness.rs b/tests/ui/loops/loop-proper-liveness.rs
new file mode 100644
index 00000000000..6546e397785
--- /dev/null
+++ b/tests/ui/loops/loop-proper-liveness.rs
@@ -0,0 +1,32 @@
+fn test1() {
+    // In this test the outer 'a loop may terminate without `x` getting initialised. Although the
+    // `x = loop { ... }` statement is reached, the value itself ends up never being computed and
+    // thus leaving `x` uninit.
+    let x: i32;
+    'a: loop {
+        x = loop { break 'a };
+    }
+    println!("{:?}", x); //~ ERROR E0381
+}
+
+// test2 and test3 should not fail.
+fn test2() {
+    // In this test the `'a` loop will never terminate thus making the use of `x` unreachable.
+    let x: i32;
+    'a: loop {
+        x = loop { continue 'a };
+    }
+    println!("{:?}", x);
+}
+
+fn test3() {
+    let x: i32;
+    // Similarly, the use of variable `x` is unreachable.
+    'a: loop {
+        x = loop { return };
+    }
+    println!("{:?}", x);
+}
+
+fn main() {
+}
diff --git a/tests/ui/loops/loop-proper-liveness.stderr b/tests/ui/loops/loop-proper-liveness.stderr
new file mode 100644
index 00000000000..f9d94b6810c
--- /dev/null
+++ b/tests/ui/loops/loop-proper-liveness.stderr
@@ -0,0 +1,18 @@
+error[E0381]: used binding `x` isn't initialized
+  --> $DIR/loop-proper-liveness.rs:9:22
+   |
+LL |     let x: i32;
+   |         - binding declared here but left uninitialized
+...
+LL |     println!("{:?}", x);
+   |                      ^ `x` used here but it isn't initialized
+   |
+   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: consider assigning a value
+   |
+LL |     let x: i32 = 0;
+   |                +++
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0381`.
diff --git a/tests/ui/loops/loop-properly-diverging-2.rs b/tests/ui/loops/loop-properly-diverging-2.rs
new file mode 100644
index 00000000000..97b3972c135
--- /dev/null
+++ b/tests/ui/loops/loop-properly-diverging-2.rs
@@ -0,0 +1,6 @@
+fn forever2() -> i32 {
+  let x: i32 = loop { break }; //~ ERROR mismatched types
+  x
+}
+
+fn main() {}
diff --git a/tests/ui/loops/loop-properly-diverging-2.stderr b/tests/ui/loops/loop-properly-diverging-2.stderr
new file mode 100644
index 00000000000..5030a2935b9
--- /dev/null
+++ b/tests/ui/loops/loop-properly-diverging-2.stderr
@@ -0,0 +1,12 @@
+error[E0308]: mismatched types
+  --> $DIR/loop-properly-diverging-2.rs:2:23
+   |
+LL |   let x: i32 = loop { break };
+   |                       ^^^^^
+   |                       |
+   |                       expected `i32`, found `()`
+   |                       help: give it a value of the expected type: `break 42`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.