Skip to content

Commit 1ffa93e

Browse files
authored
Rollup merge of #66096 - ecstatic-morse:const-loop-test, r=Centril
Add a failing UI test for multiple loops of all kinds in a `const` This simply demonstrates the current behavior and ensures we don't allow anything by accident. The new const checker will be able to improve the diagnostics here. While working on it, I didn't see very many tests with non-`while` loops in a `const`, and there were no tests with multiple loops.
2 parents dc40c93 + c8ae281 commit 1ffa93e

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/test/ui/consts/const-loop.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const _: i32 = {
2+
let mut x = 0;
3+
4+
while x < 4 {
5+
//~^ ERROR constant contains unimplemented expression type
6+
//~| ERROR constant contains unimplemented expression type
7+
x += 1;
8+
}
9+
10+
while x < 8 {
11+
x += 1;
12+
}
13+
14+
x
15+
};
16+
17+
const _: i32 = {
18+
let mut x = 0;
19+
20+
for i in 0..4 {
21+
//~^ ERROR constant contains unimplemented expression type
22+
//~| ERROR constant contains unimplemented expression type
23+
//~| ERROR references in constants may only refer to immutable values
24+
//~| ERROR calls in constants are limited to constant functions, tuple
25+
// structs and tuple variants
26+
x += i;
27+
}
28+
29+
for i in 0..4 {
30+
x += i;
31+
}
32+
33+
x
34+
};
35+
36+
const _: i32 = {
37+
let mut x = 0;
38+
39+
loop {
40+
x += 1;
41+
if x == 4 {
42+
//~^ ERROR constant contains unimplemented expression type
43+
//~| ERROR constant contains unimplemented expression type
44+
break;
45+
}
46+
}
47+
48+
loop {
49+
x += 1;
50+
if x == 8 {
51+
break;
52+
}
53+
}
54+
55+
x
56+
};
57+
58+
fn main() {}

src/test/ui/consts/const-loop.stderr

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error[E0019]: constant contains unimplemented expression type
2+
--> $DIR/const-loop.rs:4:11
3+
|
4+
LL | while x < 4 {
5+
| ^^^^^
6+
7+
error[E0019]: constant contains unimplemented expression type
8+
--> $DIR/const-loop.rs:4:5
9+
|
10+
LL | / while x < 4 {
11+
LL | |
12+
LL | |
13+
LL | | x += 1;
14+
LL | | }
15+
| |_____^
16+
17+
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
18+
--> $DIR/const-loop.rs:20:14
19+
|
20+
LL | for i in 0..4 {
21+
| ^^^^
22+
23+
error[E0019]: constant contains unimplemented expression type
24+
--> $DIR/const-loop.rs:20:14
25+
|
26+
LL | for i in 0..4 {
27+
| ^^^^
28+
29+
error[E0017]: references in constants may only refer to immutable values
30+
--> $DIR/const-loop.rs:20:14
31+
|
32+
LL | for i in 0..4 {
33+
| ^^^^ constants require immutable values
34+
35+
error[E0019]: constant contains unimplemented expression type
36+
--> $DIR/const-loop.rs:20:9
37+
|
38+
LL | for i in 0..4 {
39+
| ^
40+
41+
error[E0019]: constant contains unimplemented expression type
42+
--> $DIR/const-loop.rs:41:12
43+
|
44+
LL | if x == 4 {
45+
| ^^^^^^
46+
47+
error[E0019]: constant contains unimplemented expression type
48+
--> $DIR/const-loop.rs:41:9
49+
|
50+
LL | / if x == 4 {
51+
LL | |
52+
LL | |
53+
LL | | break;
54+
LL | | }
55+
| |_________^
56+
57+
error: aborting due to 8 previous errors
58+
59+
Some errors have detailed explanations: E0015, E0017, E0019.
60+
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)