Skip to content

Commit

Permalink
Reorganize, bless and add tests for const control flow
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ecstatic-morse committed Nov 22, 2019
1 parent 26d93f3 commit 5c377f3
Show file tree
Hide file tree
Showing 49 changed files with 1,180 additions and 308 deletions.
6 changes: 4 additions & 2 deletions src/test/ui/borrowck/issue-64453.stderr
@@ -1,11 +1,13 @@
error[E0744]: `match` is not allowed in a `static`
error[E0658]: `match` is not allowed in a `static`
--> $DIR/issue-64453.rs:4:31
|
LL | static settings_dir: String = format!("");
| ^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0744`.
For more information about this error, try `rustc --explain E0658`.
7 changes: 5 additions & 2 deletions src/test/ui/consts/const-eval/infinite_loop.stderr
Expand Up @@ -9,11 +9,14 @@ LL | |
LL | | }
| |_________^

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

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

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0080, E0744.
Some errors have detailed explanations: E0080, E0658, E0744.
For more information about an error, try `rustc --explain E0080`.
7 changes: 5 additions & 2 deletions src/test/ui/consts/const-eval/match-test-ptr-null.stderr
@@ -1,4 +1,4 @@
error[E0744]: `match` is not allowed in a `const`
error[E0658]: `match` is not allowed in a `const`
--> $DIR/match-test-ptr-null.rs:6:9
|
LL | / match &1 as *const i32 as usize {
Expand All @@ -9,6 +9,9 @@ LL | | 0 => 42,
LL | | n => n,
LL | | }
| |_________^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable

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

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0080, E0658, E0744.
Some errors have detailed explanations: E0080, E0658.
For more information about an error, try `rustc --explain E0080`.
21 changes: 0 additions & 21 deletions src/test/ui/consts/const-if.rs

This file was deleted.

37 changes: 0 additions & 37 deletions src/test/ui/consts/const-if.stderr

This file was deleted.

90 changes: 0 additions & 90 deletions src/test/ui/consts/const-loop.rs

This file was deleted.

12 changes: 9 additions & 3 deletions src/test/ui/consts/const-match-pattern-arm.stderr
@@ -1,4 +1,4 @@
error[E0744]: `match` is not allowed in a `const`
error[E0658]: `match` is not allowed in a `const`
--> $DIR/const-match-pattern-arm.rs:3:17
|
LL | const x: bool = match Some(true) {
Expand All @@ -7,16 +7,22 @@ LL | | Some(value) => true,
LL | | _ => false
LL | | };
| |_^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable

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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0744`.
For more information about this error, try `rustc --explain E0658`.
13 changes: 13 additions & 0 deletions src/test/ui/consts/control-flow/assert.both.stderr
@@ -0,0 +1,13 @@
error: any use of this value will cause an error
--> $DIR/assert.rs:12:15
|
LL | const _: () = assert!(false);
| --------------^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'assertion failed: false', $DIR/assert.rs:12:15
|
= note: `#[deny(const_err)]` on by default
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

23 changes: 23 additions & 0 deletions src/test/ui/consts/control-flow/assert.if_match.stderr
@@ -0,0 +1,23 @@
error[E0658]: panicking in constants is unstable
--> $DIR/assert.rs:8:15
|
LL | const _: () = assert!(true);
| ^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
= help: add `#![feature(const_panic)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0658]: panicking in constants is unstable
--> $DIR/assert.rs:12:15
|
LL | const _: () = assert!(false);
| ^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51999
= help: add `#![feature(const_panic)]` to the crate attributes to enable
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
21 changes: 21 additions & 0 deletions src/test/ui/consts/control-flow/assert.panic.stderr
@@ -0,0 +1,21 @@
error[E0658]: `if` is not allowed in a `const`
--> $DIR/assert.rs:8:15
|
LL | const _: () = assert!(true);
| ^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable

error[E0658]: `if` is not allowed in a `const`
--> $DIR/assert.rs:12:15
|
LL | const _: () = assert!(false);
| ^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
17 changes: 17 additions & 0 deletions src/test/ui/consts/control-flow/assert.rs
@@ -0,0 +1,17 @@
// Test that `assert` works only when both `const_if_match` and `const_panic` are enabled.

// revisions: stock if_match panic both

#![cfg_attr(any(both, if_match), feature(const_if_match))]
#![cfg_attr(any(both, panic), feature(const_panic))]

const _: () = assert!(true);
//[stock,panic]~^ ERROR `if` is not allowed in a `const`
//[if_match]~^^ ERROR panicking in constants is unstable

const _: () = assert!(false);
//[stock,panic]~^ ERROR `if` is not allowed in a `const`
//[if_match]~^^ ERROR panicking in constants is unstable
//[both]~^^^ ERROR any use of this value will cause an error

fn main() {}
21 changes: 21 additions & 0 deletions src/test/ui/consts/control-flow/assert.stock.stderr
@@ -0,0 +1,21 @@
error[E0658]: `if` is not allowed in a `const`
--> $DIR/assert.rs:8:15
|
LL | const _: () = assert!(true);
| ^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable

error[E0658]: `if` is not allowed in a `const`
--> $DIR/assert.rs:12:15
|
LL | const _: () = assert!(false);
| ^^^^^^^^^^^^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/49146
= help: add `#![feature(const_if_match)]` to the crate attributes to enable

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0658`.
43 changes: 43 additions & 0 deletions src/test/ui/consts/control-flow/basics.rs
@@ -0,0 +1,43 @@
// Test basic functionality of `if` and `match` in a const context.

// run-pass

#![feature(const_panic)]
#![feature(const_if_match)]

const X: u32 = 4;
const Y: u32 = 5;

const ABS_DIFF: u32 = if X < Y {
Y - X
} else {
X - Y
};

const fn abs_diff(a: u32, b: u32) -> u32 {
match (a, b) {
(big, little) if big > little => big - little,
(little, big) => big - little,
}
}

const fn gcd(a: u32, b: u32) -> u32 {
if b == 0 {
return a;
}

gcd(b, a % b)
}

fn main() {
const _: () = assert!(abs_diff(4, 5) == abs_diff(5, 4));
assert_eq!(abs_diff(4, 5), abs_diff(5, 4));

const _: () = assert!(ABS_DIFF == abs_diff(5, 4));
assert_eq!(ABS_DIFF, abs_diff(5, 4));

const _: () = assert!(gcd(48, 18) == 6);
const _: () = assert!(gcd(18, 48) == 6);
assert_eq!(gcd(48, 18), 6);
assert_eq!(gcd(18, 48), 6);
}

0 comments on commit 5c377f3

Please sign in to comment.