Skip to content

Commit bf80513

Browse files
Bless const tests with improved diagnostics
1 parent abea1b8 commit bf80513

37 files changed

+442
-243
lines changed

src/test/compile-fail/consts/const-fn-error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const fn f(x: usize) -> usize {
1010
//~| ERROR E0019
1111
//~| ERROR E0019
1212
//~| ERROR E0080
13+
//~| ERROR E0744
1314
sum += i;
1415
}
1516
sum

src/test/compile-fail/issue-52443.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
fn main() {
22
[(); & { loop { continue } } ]; //~ ERROR mismatched types
3+
//~^ ERROR `loop` is not allowed in a `const`
34
[(); loop { break }]; //~ ERROR mismatched types
5+
//~^ ERROR `loop` is not allowed in a `const`
46
[(); {while true {break}; 0}];
57
//~^ ERROR constant contains unimplemented expression type
68
//~| ERROR constant contains unimplemented expression type
9+
//~| ERROR `while` is not allowed in a `const`
710
//~| WARN denote infinite loops with
811
[(); { for _ in 0usize.. {}; 0}];
912
//~^ ERROR calls in constants are limited to constant functions
13+
//~| ERROR `for` is not allowed in a `const`
1014
//~| ERROR references in constants may only refer to immutable values
1115
//~| ERROR constant contains unimplemented expression type
1216
//~| ERROR constant contains unimplemented expression type

src/test/ui/borrowck/issue-64453.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ struct Project;
22
struct Value;
33

44
static settings_dir: String = format!("");
5-
//~^ ERROR [E0019]
6-
//~| ERROR [E0015]
7-
//~| ERROR [E0015]
5+
//~^ ERROR `match` is not allowed in a `static`
86

97
fn from_string(_: String) -> Value {
108
Value
@@ -13,7 +11,6 @@ fn set_editor(_: Value) {}
1311

1412
fn main() {
1513
let settings_data = from_string(settings_dir);
16-
//~^ ERROR cannot move out of static item `settings_dir` [E0507]
1714
let args: i32 = 0;
1815

1916
match args {
+3-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,11 @@
1-
error[E0507]: cannot move out of static item `settings_dir`
2-
--> $DIR/issue-64453.rs:15:37
3-
|
4-
LL | let settings_data = from_string(settings_dir);
5-
| ^^^^^^^^^^^^ move occurs because `settings_dir` has type `std::string::String`, which does not implement the `Copy` trait
6-
7-
error[E0019]: static contains unimplemented expression type
8-
--> $DIR/issue-64453.rs:4:31
9-
|
10-
LL | static settings_dir: String = format!("");
11-
| ^^^^^^^^^^^
12-
|
13-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
14-
15-
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
16-
--> $DIR/issue-64453.rs:4:31
17-
|
18-
LL | static settings_dir: String = format!("");
19-
| ^^^^^^^^^^^
20-
|
21-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
22-
23-
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
1+
error[E0744]: `match` is not allowed in a `static`
242
--> $DIR/issue-64453.rs:4:31
253
|
264
LL | static settings_dir: String = format!("");
275
| ^^^^^^^^^^^
286
|
297
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
308

31-
error: aborting due to 4 previous errors
9+
error: aborting due to previous error
3210

33-
Some errors have detailed explanations: E0015, E0019, E0507.
34-
For more information about an error, try `rustc --explain E0015`.
11+
For more information about this error, try `rustc --explain E0744`.

src/test/ui/closures/issue-52437.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
fn main() {
22
[(); &(&'static: loop { |x| {}; }) as *const _ as usize]
33
//~^ ERROR: invalid label name `'static`
4+
//~| ERROR: `loop` is not allowed in a `const`
45
//~| ERROR: type annotations needed
56
}

src/test/ui/closures/issue-52437.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ error: invalid label name `'static`
44
LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize]
55
| ^^^^^^^
66

7+
error[E0744]: `loop` is not allowed in a `const`
8+
--> $DIR/issue-52437.rs:2:13
9+
|
10+
LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
713
error[E0282]: type annotations needed
814
--> $DIR/issue-52437.rs:2:30
915
|
1016
LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize]
1117
| ^ consider giving this closure parameter a type
1218

13-
error: aborting due to 2 previous errors
19+
error: aborting due to 3 previous errors
1420

15-
For more information about this error, try `rustc --explain E0282`.
21+
Some errors have detailed explanations: E0282, E0744.
22+
For more information about an error, try `rustc --explain E0282`.

src/test/ui/consts/const-eval/infinite_loop.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ fn main() {
77
while n != 0 {
88
//~^ ERROR constant contains unimplemented expression type
99
//~| ERROR constant contains unimplemented expression type
10+
//~| ERROR `while` is not allowed in a `const`
1011
n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
1112
//~^ ERROR evaluation of constant value failed
13+
//~| ERROR `if` is not allowed in a `const`
1214
}
1315
n
1416
}];

src/test/ui/consts/const-eval/infinite_loop.stderr

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
error[E0744]: `while` is not allowed in a `const`
2+
--> $DIR/infinite_loop.rs:7:9
3+
|
4+
LL | / while n != 0 {
5+
LL | |
6+
LL | |
7+
LL | |
8+
... |
9+
LL | |
10+
LL | | }
11+
| |_________^
12+
13+
error[E0744]: `if` is not allowed in a `const`
14+
--> $DIR/infinite_loop.rs:11:17
15+
|
16+
LL | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
119
error[E0019]: constant contains unimplemented expression type
220
--> $DIR/infinite_loop.rs:7:15
321
|
@@ -10,7 +28,8 @@ error[E0019]: constant contains unimplemented expression type
1028
LL | / while n != 0 {
1129
LL | |
1230
LL | |
13-
LL | | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
31+
LL | |
32+
... |
1433
LL | |
1534
LL | | }
1635
| |_________^
@@ -29,12 +48,12 @@ LL | | }];
2948
| |_____^
3049

3150
error[E0080]: evaluation of constant value failed
32-
--> $DIR/infinite_loop.rs:10:20
51+
--> $DIR/infinite_loop.rs:11:20
3352
|
3453
LL | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
3554
| ^^^^^^^^^^ duplicate interpreter state observed here, const evaluation will never terminate
3655

37-
error: aborting due to 3 previous errors
56+
error: aborting due to 5 previous errors
3857

39-
Some errors have detailed explanations: E0019, E0080.
58+
Some errors have detailed explanations: E0019, E0080, E0744.
4059
For more information about an error, try `rustc --explain E0019`.

src/test/ui/consts/const-eval/issue-52475.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ fn main() {
44
let mut x = &0;
55
let mut n = 0;
66
while n < 5 {
7-
//~^ ERROR constant contains unimplemented expression type
7+
//~^ ERROR `while` is not allowed in a `const`
8+
//~| ERROR constant contains unimplemented expression type
89
//~| ERROR constant contains unimplemented expression type
910
n = (n + 1) % 5; //~ ERROR evaluation of constant value failed
1011
x = &0; // Materialize a new AllocId

src/test/ui/consts/const-eval/issue-52475.stderr

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error[E0744]: `while` is not allowed in a `const`
2+
--> $DIR/issue-52475.rs:6:9
3+
|
4+
LL | / while n < 5 {
5+
LL | |
6+
LL | |
7+
LL | |
8+
LL | | n = (n + 1) % 5;
9+
LL | | x = &0; // Materialize a new AllocId
10+
LL | | }
11+
| |_________^
12+
113
error[E0019]: constant contains unimplemented expression type
214
--> $DIR/issue-52475.rs:6:15
315
|
@@ -10,6 +22,7 @@ error[E0019]: constant contains unimplemented expression type
1022
LL | / while n < 5 {
1123
LL | |
1224
LL | |
25+
LL | |
1326
LL | | n = (n + 1) % 5;
1427
LL | | x = &0; // Materialize a new AllocId
1528
LL | | }
@@ -29,12 +42,12 @@ LL | | }];
2942
| |_____^
3043

3144
error[E0080]: evaluation of constant value failed
32-
--> $DIR/issue-52475.rs:9:17
45+
--> $DIR/issue-52475.rs:10:17
3346
|
3447
LL | n = (n + 1) % 5;
3548
| ^^^^^^^^^^^ duplicate interpreter state observed here, const evaluation will never terminate
3649

37-
error: aborting due to 3 previous errors
50+
error: aborting due to 4 previous errors
3851

39-
Some errors have detailed explanations: E0019, E0080.
52+
Some errors have detailed explanations: E0019, E0080, E0744.
4053
For more information about an error, try `rustc --explain E0019`.

src/test/ui/consts/const-eval/match-test-ptr-null.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
let _: [u8; 0] = [4; {
66
match &1 as *const i32 as usize {
77
//~^ ERROR casting pointers to integers in constants
8+
//~| ERROR `match` is not allowed in a `const`
89
//~| ERROR constant contains unimplemented expression type
910
//~| ERROR evaluation of constant value failed
1011
0 => 42, //~ ERROR constant contains unimplemented expression type

src/test/ui/consts/const-eval/match-test-ptr-null.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error[E0744]: `match` is not allowed in a `const`
2+
--> $DIR/match-test-ptr-null.rs:6:9
3+
|
4+
LL | / match &1 as *const i32 as usize {
5+
LL | |
6+
LL | |
7+
LL | |
8+
... |
9+
LL | | n => n,
10+
LL | | }
11+
| |_________^
12+
113
error[E0658]: casting pointers to integers in constants is unstable
214
--> $DIR/match-test-ptr-null.rs:6:15
315
|
@@ -14,7 +26,7 @@ LL | match &1 as *const i32 as usize {
1426
| ^^^^^^^^^^^^^^^^^^^^^^^^^
1527

1628
error[E0019]: constant contains unimplemented expression type
17-
--> $DIR/match-test-ptr-null.rs:10:13
29+
--> $DIR/match-test-ptr-null.rs:11:13
1830
|
1931
LL | 0 => 42,
2032
| ^
@@ -25,7 +37,7 @@ error[E0080]: evaluation of constant value failed
2537
LL | match &1 as *const i32 as usize {
2638
| ^^^^^^^^^^^^^^^^^^^^^^^^^ "pointer-to-integer cast" needs an rfc before being allowed inside constants
2739

28-
error: aborting due to 4 previous errors
40+
error: aborting due to 5 previous errors
2941

30-
Some errors have detailed explanations: E0019, E0080, E0658.
42+
Some errors have detailed explanations: E0019, E0080, E0658, E0744.
3143
For more information about an error, try `rustc --explain E0019`.

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
const _: i32 = if true { //~ ERROR if expression is not allowed in a const
1+
const _: i32 = if true { //~ ERROR `if` is not allowed in a `const`
22
5
33
} else {
44
6
55
};
66

7-
const _: i32 = match 1 { //~ ERROR match expression is not allowed in a const
7+
const _: i32 = match 1 { //~ ERROR `match` is not allowed in a `const`
88
2 => 3,
99
4 => 5,
1010
_ => 0,
1111
};
1212

1313
const fn foo() -> i32 {
14-
if true { 5 } else { 6 } //~ ERROR if expression is not allowed in a const fn
14+
if true { 5 } else { 6 } //~ ERROR `if` is not allowed in a `const fn`
1515
}
1616

1717
const fn bar() -> i32 {
18-
match 0 { 1 => 2, _ => 0 } //~ ERROR match expression is not allowed in a const fn
18+
match 0 { 1 => 2, _ => 0 } //~ ERROR `match` is not allowed in a `const fn`
1919
}
2020

2121
fn main() {}

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

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
error[E0019]: constant contains unimplemented expression type
2-
--> $DIR/const-if.rs:1:20
1+
error[E0744]: `if` is not allowed in a `const`
2+
--> $DIR/const-if.rs:1:16
33
|
4-
LL | const _X: i32 = if true { 5 } else { 6 };
5-
| ^^^^
4+
LL | const _: i32 = if true {
5+
| ________________^
6+
LL | | 5
7+
LL | | } else {
8+
LL | | 6
9+
LL | | };
10+
| |_^
611

7-
error[E0019]: constant contains unimplemented expression type
8-
--> $DIR/const-if.rs:1:17
12+
error[E0744]: `match` is not allowed in a `const`
13+
--> $DIR/const-if.rs:7:16
914
|
10-
LL | const _X: i32 = if true { 5 } else { 6 };
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
15+
LL | const _: i32 = match 1 {
16+
| ________________^
17+
LL | | 2 => 3,
18+
LL | | 4 => 5,
19+
LL | | _ => 0,
20+
LL | | };
21+
| |_^
1222

13-
error: aborting due to 2 previous errors
23+
error[E0744]: `if` is not allowed in a `const fn`
24+
--> $DIR/const-if.rs:14:5
25+
|
26+
LL | if true { 5 } else { 6 }
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^
28+
29+
error[E0744]: `match` is not allowed in a `const fn`
30+
--> $DIR/const-if.rs:18:5
31+
|
32+
LL | match 0 { 1 => 2, _ => 0 }
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
35+
error: aborting due to 4 previous errors
1436

15-
For more information about this error, try `rustc --explain E0019`.
37+
For more information about this error, try `rustc --explain E0744`.

0 commit comments

Comments
 (0)