some more test cases

This commit is contained in:
Ralf Jung 2020-04-29 14:55:05 +02:00
parent 979bbf2ce1
commit a03355dea0
3 changed files with 109 additions and 14 deletions

View File

@ -1 +1,3 @@
pub static mut ZERO: [u8; 1] = [0];
pub static ZERO_REF: &[u8; 1] = unsafe { &ZERO };
pub static mut OPT_ZERO: Option<u8> = Some(0);

View File

@ -2,8 +2,7 @@
// aux-build:static_cross_crate.rs
#![allow(const_err)]
#![feature(exclusive_range_pattern)]
#![feature(half_open_range_patterns)]
#![feature(exclusive_range_pattern, half_open_range_patterns, const_if_match, const_panic)]
extern crate static_cross_crate;
@ -16,13 +15,29 @@ const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior to use this value
//~^ WARN skipping const checks
};
const SLICE_MUT2: &u8 = { //~ ERROR undefined behavior to use this value
const U8_MUT: &u8 = { //~ ERROR undefined behavior to use this value
//~| NOTE encountered a reference pointing to a static variable
//~| NOTE
unsafe { &static_cross_crate::ZERO[0] }
//~^ WARN skipping const checks
};
// Also test indirection that reads from other static. This causes a const_err.
#[warn(const_err)] //~ NOTE
const U8_MUT2: &u8 = { //~ NOTE
unsafe { &(*static_cross_crate::ZERO_REF)[0] }
//~^ WARN skipping const checks
//~| WARN [const_err]
//~| NOTE constant accesses static
};
#[warn(const_err)] //~ NOTE
const U8_MUT3: &u8 = { //~ NOTE
unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
//~^ WARN skipping const checks
//~| WARN [const_err]
//~| NOTE constant accesses static
};
pub fn test(x: &[u8; 1]) -> bool {
match x {
SLICE_MUT => true,
@ -33,7 +48,24 @@ pub fn test(x: &[u8; 1]) -> bool {
pub fn test2(x: &u8) -> bool {
match x {
SLICE_MUT2 => true,
U8_MUT => true,
//~^ ERROR could not evaluate constant pattern
&(1..) => false,
}
}
// We need to use these *in a pattern* to trigger the failure... likely because
// the errors above otherwise stop compilation too early?
pub fn test3(x: &u8) -> bool {
match x {
U8_MUT2 => true,
//~^ ERROR could not evaluate constant pattern
&(1..) => false,
}
}
pub fn test4(x: &u8) -> bool {
match x {
U8_MUT3 => true,
//~^ ERROR could not evaluate constant pattern
&(1..) => false,
}
@ -45,4 +77,5 @@ fn main() {
}
// Now the pattern is not exhaustive any more!
test(&[0]);
test2(&0);
}

View File

@ -1,11 +1,11 @@
warning: skipping const checks
--> $DIR/const_refers_to_static_cross_crate.rs:15:15
--> $DIR/const_refers_to_static_cross_crate.rs:14:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static_cross_crate.rs:12:1
--> $DIR/const_refers_to_static_cross_crate.rs:11:1
|
LL | / const SLICE_MUT: &[u8; 1] = {
LL | |
@ -18,21 +18,21 @@ LL | | };
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:28:9
--> $DIR/const_refers_to_static_cross_crate.rs:43:9
|
LL | SLICE_MUT => true,
| ^^^^^^^^^
warning: skipping const checks
--> $DIR/const_refers_to_static_cross_crate.rs:22:15
--> $DIR/const_refers_to_static_cross_crate.rs:21:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: it is undefined behavior to use this value
--> $DIR/const_refers_to_static_cross_crate.rs:19:1
--> $DIR/const_refers_to_static_cross_crate.rs:18:1
|
LL | / const SLICE_MUT2: &u8 = {
LL | / const U8_MUT: &u8 = {
LL | |
LL | |
LL | | unsafe { &static_cross_crate::ZERO[0] }
@ -43,11 +43,71 @@ LL | | };
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:36:9
--> $DIR/const_refers_to_static_cross_crate.rs:51:9
|
LL | SLICE_MUT2 => true,
| ^^^^^^^^^^
LL | U8_MUT => true,
| ^^^^^^
error: aborting due to 4 previous errors; 2 warnings emitted
warning: skipping const checks
--> $DIR/const_refers_to_static_cross_crate.rs:28:17
|
LL | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: any use of this value will cause an error
--> $DIR/const_refers_to_static_cross_crate.rs:28:14
|
LL | / const U8_MUT2: &u8 = {
LL | | unsafe { &(*static_cross_crate::ZERO_REF)[0] }
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static
LL | |
LL | |
LL | |
LL | | };
| |__-
|
note: the lint level is defined here
--> $DIR/const_refers_to_static_cross_crate.rs:26:8
|
LL | #[warn(const_err)]
| ^^^^^^^^^
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:61:9
|
LL | U8_MUT2 => true,
| ^^^^^^^
warning: skipping const checks
--> $DIR/const_refers_to_static_cross_crate.rs:35:20
|
LL | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: any use of this value will cause an error
--> $DIR/const_refers_to_static_cross_crate.rs:35:51
|
LL | / const U8_MUT3: &u8 = {
LL | | unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
| | ^^^^^^^^^^^ constant accesses static
LL | |
LL | |
LL | |
LL | | };
| |__-
|
note: the lint level is defined here
--> $DIR/const_refers_to_static_cross_crate.rs:33:8
|
LL | #[warn(const_err)]
| ^^^^^^^^^
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:68:9
|
LL | U8_MUT3 => true,
| ^^^^^^^
error: aborting due to 6 previous errors; 6 warnings emitted
For more information about this error, try `rustc --explain E0080`.