]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.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 #![allow(const_err)]
4
5 #![feature(exclusive_range_pattern, half_open_range_patterns)]
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 };
16
17 const U8_MUT: &u8 = { //~ ERROR undefined behavior to use this value
18 //~| NOTE encountered a reference pointing to a static variable
19 //~| NOTE
20 unsafe { &static_cross_crate::ZERO[0] }
21 };
22
23 // Also test indirection that reads from other static. This causes a const_err.
24 #[warn(const_err)] //~ NOTE
25 const U8_MUT2: &u8 = { //~ NOTE
26 unsafe { &(*static_cross_crate::ZERO_REF)[0] }
27 //~^ WARN [const_err]
28 //~| NOTE constant accesses static
29 };
30 #[warn(const_err)] //~ NOTE
31 const U8_MUT3: &u8 = { //~ NOTE
32 unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
33 //~^ WARN [const_err]
34 //~| NOTE constant accesses static
35 };
36
37 pub fn test(x: &[u8; 1]) -> bool {
38 match x {
39 SLICE_MUT => true,
40 //~^ ERROR could not evaluate constant pattern
41 //~| ERROR could not evaluate constant pattern
42 &[1..] => false,
43 }
44 }
45
46 pub fn test2(x: &u8) -> bool {
47 match x {
48 U8_MUT => true,
49 //~^ ERROR could not evaluate constant pattern
50 //~| ERROR could not evaluate constant pattern
51 &(1..) => false,
52 }
53 }
54
55 // We need to use these *in a pattern* to trigger the failure... likely because
56 // the errors above otherwise stop compilation too early?
57 pub fn test3(x: &u8) -> bool {
58 match x {
59 U8_MUT2 => true,
60 //~^ ERROR could not evaluate constant pattern
61 //~| ERROR could not evaluate constant pattern
62 &(1..) => false,
63 }
64 }
65 pub fn test4(x: &u8) -> bool {
66 match x {
67 U8_MUT3 => true,
68 //~^ ERROR could not evaluate constant pattern
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 }