Skip to content

Commit abea1b8

Browse files
Extend const-loop and const-if to handle more cases
This makes sure that our HIR visitor is visiting as many const-items as possible.
1 parent 8904faa commit abea1b8

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
const _X: i32 = if true { 5 } else { 6 };
2-
//~^ ERROR constant contains unimplemented expression type
3-
//~| ERROR constant contains unimplemented expression type
1+
const _: i32 = if true { //~ ERROR if expression is not allowed in a const
2+
5
3+
} else {
4+
6
5+
};
6+
7+
const _: i32 = match 1 { //~ ERROR match expression is not allowed in a const
8+
2 => 3,
9+
4 => 5,
10+
_ => 0,
11+
};
12+
13+
const fn foo() -> i32 {
14+
if true { 5 } else { 6 } //~ ERROR if expression is not allowed in a const fn
15+
}
16+
17+
const fn bar() -> i32 {
18+
match 0 { 1 => 2, _ => 0 } //~ ERROR match expression is not allowed in a const fn
19+
}
420

521
fn main() {}

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

+52-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
1+
const _: () = loop {}; //~ ERROR loop is not allowed in a const
2+
3+
static FOO: i32 = loop { break 4; }; //~ ERROR loop is not allowed in a static
4+
5+
const fn foo() {
6+
loop {} //~ ERROR loop is not allowed in a const fn
7+
}
8+
9+
pub trait Foo {
10+
const BAR: i32 = loop { break 4; }; //~ ERROR loop is not allowed in a const
11+
}
12+
13+
impl Foo for () {
14+
const BAR: i32 = loop { break 4; }; //~ ERROR loop is not allowed in a const
15+
}
16+
17+
fn non_const_outside() {
18+
const fn const_inside() {
19+
loop {} //~ ERROR `loop` is not allowed in a `const fn`
20+
}
21+
}
22+
23+
const fn const_outside() {
24+
fn non_const_inside() {
25+
loop {}
26+
}
27+
}
28+
29+
fn main() {
30+
let x = [0; {
31+
while false {}
32+
//~^ ERROR `while` is not allowed in a `const`
33+
//~| ERROR constant contains unimplemented expression type
34+
//~| ERROR constant contains unimplemented expression type
35+
4
36+
}];
37+
}
38+
139
const _: i32 = {
240
let mut x = 0;
341

4-
while x < 4 {
5-
//~^ ERROR constant contains unimplemented expression type
6-
//~| ERROR constant contains unimplemented expression type
42+
while x < 4 { //~ ERROR while loop is not allowed in a const
743
x += 1;
844
}
945

10-
while x < 8 {
46+
while x < 8 { //~ ERROR while loop is not allowed in a const
1147
x += 1;
1248
}
1349

@@ -17,16 +53,11 @@ const _: i32 = {
1753
const _: i32 = {
1854
let mut x = 0;
1955

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
56+
for i in 0..4 { //~ ERROR for loop is not allowed in a const
2657
x += i;
2758
}
2859

29-
for i in 0..4 {
60+
for i in 0..4 { //~ ERROR for loop is not allowed in a const
3061
x += i;
3162
}
3263

@@ -36,23 +67,26 @@ const _: i32 = {
3667
const _: i32 = {
3768
let mut x = 0;
3869

39-
loop {
70+
loop { //~ ERROR loop is not allowed in a const
4071
x += 1;
41-
if x == 4 {
42-
//~^ ERROR constant contains unimplemented expression type
43-
//~| ERROR constant contains unimplemented expression type
72+
if x == 4 { //~ ERROR if expression is not allowed in a const
4473
break;
4574
}
4675
}
4776

48-
loop {
77+
loop { //~ ERROR loop is not allowed in a const
4978
x += 1;
50-
if x == 8 {
79+
if x == 8 { //~ ERROR if expression is not allowed in a const
5180
break;
5281
}
5382
}
5483

5584
x
5685
};
5786

58-
fn main() {}
87+
const _: i32 = {
88+
let mut x = 0;
89+
while let None = Some(x) { } //~ ERROR while loop is not allowed in a const
90+
while let None = Some(x) { } //~ ERROR while loop is not allowed in a const
91+
x
92+
};

0 commit comments

Comments
 (0)