Skip to content

Commit 5c377f3

Browse files
Reorganize, bless and add tests for const control flow
This creates a new test directory, `ui/consts/control-flow` to hold tests related to control flow in a const context. It also blesses all existing tests with the new error messages, and adds new tests for the `const_if_match` feature.
1 parent 26d93f3 commit 5c377f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1180
-308
lines changed
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
error[E0744]: `match` is not allowed in a `static`
1+
error[E0658]: `match` is not allowed in a `static`
22
--> $DIR/issue-64453.rs:4:31
33
|
44
LL | static settings_dir: String = format!("");
55
| ^^^^^^^^^^^
66
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
8+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
79
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
810

911
error: aborting due to previous error
1012

11-
For more information about this error, try `rustc --explain E0744`.
13+
For more information about this error, try `rustc --explain E0658`.

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ LL | |
99
LL | | }
1010
| |_________^
1111

12-
error[E0744]: `if` is not allowed in a `const`
12+
error[E0658]: `if` is not allowed in a `const`
1313
--> $DIR/infinite_loop.rs:9:17
1414
|
1515
LL | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
|
18+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
19+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
1720

1821
warning: Constant evaluating a complex constant, this might take some time
1922
--> $DIR/infinite_loop.rs:4:18
@@ -36,5 +39,5 @@ LL | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
3639

3740
error: aborting due to 3 previous errors
3841

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

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0744]: `match` is not allowed in a `const`
1+
error[E0658]: `match` is not allowed in a `const`
22
--> $DIR/match-test-ptr-null.rs:6:9
33
|
44
LL | / match &1 as *const i32 as usize {
@@ -9,6 +9,9 @@ LL | | 0 => 42,
99
LL | | n => n,
1010
LL | | }
1111
| |_________^
12+
|
13+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
14+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
1215

1316
error[E0658]: casting pointers to integers in constants is unstable
1417
--> $DIR/match-test-ptr-null.rs:6:15
@@ -27,5 +30,5 @@ LL | match &1 as *const i32 as usize {
2730

2831
error: aborting due to 3 previous errors
2932

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

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

-21
This file was deleted.

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

-37
This file was deleted.

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

-90
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0744]: `match` is not allowed in a `const`
1+
error[E0658]: `match` is not allowed in a `const`
22
--> $DIR/const-match-pattern-arm.rs:3:17
33
|
44
LL | const x: bool = match Some(true) {
@@ -7,16 +7,22 @@ LL | | Some(value) => true,
77
LL | | _ => false
88
LL | | };
99
| |_^
10+
|
11+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
12+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
1013

11-
error[E0744]: `match` is not allowed in a `const`
14+
error[E0658]: `match` is not allowed in a `const`
1215
--> $DIR/const-match-pattern-arm.rs:9:5
1316
|
1417
LL | / match Some(true) {
1518
LL | | Some(value) => true,
1619
LL | | _ => false
1720
LL | | }
1821
| |_____^
22+
|
23+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
24+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
1925

2026
error: aborting due to 2 previous errors
2127

22-
For more information about this error, try `rustc --explain E0744`.
28+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/assert.rs:12:15
3+
|
4+
LL | const _: () = assert!(false);
5+
| --------------^^^^^^^^^^^^^^-
6+
| |
7+
| the evaluated program panicked at 'assertion failed: false', $DIR/assert.rs:12:15
8+
|
9+
= note: `#[deny(const_err)]` on by default
10+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
11+
12+
error: aborting due to previous error
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0658]: panicking in constants is unstable
2+
--> $DIR/assert.rs:8:15
3+
|
4+
LL | const _: () = assert!(true);
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
8+
= help: add `#![feature(const_panic)]` to the crate attributes to enable
9+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
10+
11+
error[E0658]: panicking in constants is unstable
12+
--> $DIR/assert.rs:12:15
13+
|
14+
LL | const _: () = assert!(false);
15+
| ^^^^^^^^^^^^^^
16+
|
17+
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
18+
= help: add `#![feature(const_panic)]` to the crate attributes to enable
19+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: `if` is not allowed in a `const`
2+
--> $DIR/assert.rs:8:15
3+
|
4+
LL | const _: () = assert!(true);
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
8+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
9+
10+
error[E0658]: `if` is not allowed in a `const`
11+
--> $DIR/assert.rs:12:15
12+
|
13+
LL | const _: () = assert!(false);
14+
| ^^^^^^^^^^^^^^
15+
|
16+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
17+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Test that `assert` works only when both `const_if_match` and `const_panic` are enabled.
2+
3+
// revisions: stock if_match panic both
4+
5+
#![cfg_attr(any(both, if_match), feature(const_if_match))]
6+
#![cfg_attr(any(both, panic), feature(const_panic))]
7+
8+
const _: () = assert!(true);
9+
//[stock,panic]~^ ERROR `if` is not allowed in a `const`
10+
//[if_match]~^^ ERROR panicking in constants is unstable
11+
12+
const _: () = assert!(false);
13+
//[stock,panic]~^ ERROR `if` is not allowed in a `const`
14+
//[if_match]~^^ ERROR panicking in constants is unstable
15+
//[both]~^^^ ERROR any use of this value will cause an error
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: `if` is not allowed in a `const`
2+
--> $DIR/assert.rs:8:15
3+
|
4+
LL | const _: () = assert!(true);
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
8+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
9+
10+
error[E0658]: `if` is not allowed in a `const`
11+
--> $DIR/assert.rs:12:15
12+
|
13+
LL | const _: () = assert!(false);
14+
| ^^^^^^^^^^^^^^
15+
|
16+
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
17+
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Test basic functionality of `if` and `match` in a const context.
2+
3+
// run-pass
4+
5+
#![feature(const_panic)]
6+
#![feature(const_if_match)]
7+
8+
const X: u32 = 4;
9+
const Y: u32 = 5;
10+
11+
const ABS_DIFF: u32 = if X < Y {
12+
Y - X
13+
} else {
14+
X - Y
15+
};
16+
17+
const fn abs_diff(a: u32, b: u32) -> u32 {
18+
match (a, b) {
19+
(big, little) if big > little => big - little,
20+
(little, big) => big - little,
21+
}
22+
}
23+
24+
const fn gcd(a: u32, b: u32) -> u32 {
25+
if b == 0 {
26+
return a;
27+
}
28+
29+
gcd(b, a % b)
30+
}
31+
32+
fn main() {
33+
const _: () = assert!(abs_diff(4, 5) == abs_diff(5, 4));
34+
assert_eq!(abs_diff(4, 5), abs_diff(5, 4));
35+
36+
const _: () = assert!(ABS_DIFF == abs_diff(5, 4));
37+
assert_eq!(ABS_DIFF, abs_diff(5, 4));
38+
39+
const _: () = assert!(gcd(48, 18) == 6);
40+
const _: () = assert!(gcd(18, 48) == 6);
41+
assert_eq!(gcd(48, 18), 6);
42+
assert_eq!(gcd(18, 48), 6);
43+
}

0 commit comments

Comments
 (0)