]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs
New upstream version 1.51.0+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 //~| WARN this was previously accepted by the compiler but is being phased out
30 //~| NOTE
31 };
32 #[warn(const_err)] //~ NOTE
33 const U8_MUT3: &u8 = { //~ NOTE
34 unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } }
35 //~^ WARN [const_err]
36 //~| NOTE constant accesses static
37 //~| NOTE in this expansion of panic!
38 //~| WARN this was previously accepted by the compiler but is being phased out
39 //~| NOTE
40 };
41
42 pub fn test(x: &[u8; 1]) -> bool {
43 match x {
44 SLICE_MUT => true,
45 //~^ ERROR could not evaluate constant pattern
46 //~| ERROR could not evaluate constant pattern
47 &[1..] => false,
48 }
49 }
50
51 pub fn test2(x: &u8) -> bool {
52 match x {
53 U8_MUT => true,
54 //~^ ERROR could not evaluate constant pattern
55 //~| ERROR could not evaluate constant pattern
56 &(1..) => false,
57 }
58 }
59
60 // We need to use these *in a pattern* to trigger the failure... likely because
61 // the errors above otherwise stop compilation too early?
62 pub fn test3(x: &u8) -> bool {
63 match x {
64 U8_MUT2 => true,
65 //~^ ERROR could not evaluate constant pattern
66 //~| ERROR could not evaluate constant pattern
67 &(1..) => false,
68 }
69 }
70 pub fn test4(x: &u8) -> bool {
71 match x {
72 U8_MUT3 => true,
73 //~^ ERROR could not evaluate constant pattern
74 //~| ERROR could not evaluate constant pattern
75 &(1..) => false,
76 }
77 }
78
79 fn main() {
80 unsafe {
81 static_cross_crate::ZERO[0] = 1;
82 }
83 // Now the pattern is not exhaustive any more!
84 test(&[0]);
85 test2(&0);
86 }