]> git.lizzy.rs Git - rust.git/blob - src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs
Rollup merge of #98640 - cuviper:stable-rust-analyzer, r=Mark-Simulacrum
[rust.git] / src / test / ui / consts / miri_unleashed / const_refers_to_static_cross_crate.rs
1 // compile-flags: -Zunleash-the-miri-inside-of-you
2 // aux-build:static_cross_crate.rs
3 // stderr-per-bitwidth
4 #![allow(const_err)]
5
6 #![feature(exclusive_range_pattern, half_open_range_patterns)]
7
8 extern crate static_cross_crate;
9
10 // Sneaky: reference to a mutable static.
11 // Allowing this would be a disaster for pattern matching, we could violate exhaustiveness checking!
12 const SLICE_MUT: &[u8; 1] = { //~ ERROR undefined behavior to use this value
13 //~| encountered a reference pointing to a static variable
14     unsafe { &static_cross_crate::ZERO }
15 };
16
17 const U8_MUT: &u8 = { //~ ERROR undefined behavior to use this value
18 //~| encountered a reference pointing to a static variable
19     unsafe { &static_cross_crate::ZERO[0] }
20 };
21
22 // Also test indirection that reads from other static. This causes a const_err.
23 #[warn(const_err)]
24 const U8_MUT2: &u8 = {
25     unsafe { &(*static_cross_crate::ZERO_REF)[0] }
26     //~^ WARN [const_err]
27     //~| constant accesses static
28     //~| WARN this was previously accepted by the compiler but is being phased out
29 };
30 #[warn(const_err)]
31 const U8_MUT3: &u8 = {
32     unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
33     //~^ WARN [const_err]
34     //~| constant accesses static
35     //~| WARN this was previously accepted by the compiler but is being phased out
36 };
37
38 pub fn test(x: &[u8; 1]) -> bool {
39     match x {
40         SLICE_MUT => true,
41         //~^ ERROR could not evaluate constant pattern
42         //~| ERROR could not evaluate constant pattern
43         &[1..] => false,
44     }
45 }
46
47 pub fn test2(x: &u8) -> bool {
48     match x {
49         U8_MUT => true,
50         //~^ ERROR could not evaluate constant pattern
51         //~| ERROR could not evaluate constant pattern
52         &(1..) => false,
53     }
54 }
55
56 // We need to use these *in a pattern* to trigger the failure... likely because
57 // the errors above otherwise stop compilation too early?
58 pub fn test3(x: &u8) -> bool {
59     match x {
60         U8_MUT2 => true,
61         //~^ ERROR could not evaluate constant pattern
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         //~| ERROR could not evaluate constant pattern
71         &(1..) => false,
72     }
73 }
74
75 fn main() {
76     unsafe {
77         static_cross_crate::ZERO[0] = 1;
78     }
79     // Now the pattern is not exhaustive any more!
80     test(&[0]);
81     test2(&0);
82 }