|
| 1 | +// compile-flags: -Zunleash-the-miri-inside-of-you -Zdeduplicate-diagnostics |
| 2 | +// aux-build:static_cross_crate.rs |
| 3 | +#![allow(const_err)] |
| 4 | + |
| 5 | +#![feature(exclusive_range_pattern, half_open_range_patterns, const_if_match, const_panic)] |
| 6 | + |
| 7 | +extern crate static_cross_crate; |
| 8 | + |
| 9 | +// Sneaky: reference to a mutable static. |
| 10 | +// Allowing this would be a disaster for pattern matching, we could violate exhaustiveness checking! |
| 11 | +const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior to use this value |
| 12 | +//~| NOTE encountered a reference pointing to a static variable |
| 13 | +//~| NOTE |
| 14 | + unsafe { &static_cross_crate::ZERO } |
| 15 | + //~^ WARN skipping const checks |
| 16 | +}; |
| 17 | + |
| 18 | +const U8_MUT: &u8 = { //~ ERROR undefined behavior to use this value |
| 19 | +//~| NOTE encountered a reference pointing to a static variable |
| 20 | +//~| NOTE |
| 21 | + unsafe { &static_cross_crate::ZERO[0] } |
| 22 | + //~^ WARN skipping const checks |
| 23 | +}; |
| 24 | + |
| 25 | +// Also test indirection that reads from other static. This causes a const_err. |
| 26 | +#[warn(const_err)] //~ NOTE |
| 27 | +const U8_MUT2: &u8 = { //~ NOTE |
| 28 | + unsafe { &(*static_cross_crate::ZERO_REF)[0] } |
| 29 | + //~^ WARN skipping const checks |
| 30 | + //~| WARN [const_err] |
| 31 | + //~| NOTE constant accesses static |
| 32 | +}; |
| 33 | +#[warn(const_err)] //~ NOTE |
| 34 | +const U8_MUT3: &u8 = { //~ NOTE |
| 35 | + unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } } |
| 36 | + //~^ WARN skipping const checks |
| 37 | + //~| WARN [const_err] |
| 38 | + //~| NOTE constant accesses static |
| 39 | +}; |
| 40 | + |
| 41 | +pub fn test(x: &[u8; 1]) -> bool { |
| 42 | + match x { |
| 43 | + SLICE_MUT => true, |
| 44 | + //~^ ERROR could not evaluate constant pattern |
| 45 | + &[1..] => false, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub fn test2(x: &u8) -> bool { |
| 50 | + match x { |
| 51 | + U8_MUT => true, |
| 52 | + //~^ ERROR could not evaluate constant pattern |
| 53 | + &(1..) => false, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// We need to use these *in a pattern* to trigger the failure... likely because |
| 58 | +// the errors above otherwise stop compilation too early? |
| 59 | +pub fn test3(x: &u8) -> bool { |
| 60 | + match x { |
| 61 | + U8_MUT2 => true, |
| 62 | + //~^ ERROR could not evaluate constant pattern |
| 63 | + &(1..) => false, |
| 64 | + } |
| 65 | +} |
| 66 | +pub fn test4(x: &u8) -> bool { |
| 67 | + match x { |
| 68 | + U8_MUT3 => true, |
| 69 | + //~^ ERROR could not evaluate constant pattern |
| 70 | + &(1..) => false, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn main() { |
| 75 | + unsafe { |
| 76 | + static_cross_crate::ZERO[0] = 1; |
| 77 | + } |
| 78 | + // Now the pattern is not exhaustive any more! |
| 79 | + test(&[0]); |
| 80 | + test2(&0); |
| 81 | +} |
0 commit comments