File: const_refers_to_static_cross_crate.rs

package info (click to toggle)
rustc 1.89.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 906,624 kB
  • sloc: xml: 158,148; python: 34,888; javascript: 19,595; sh: 19,221; ansic: 13,046; cpp: 7,144; asm: 4,376; makefile: 692; lisp: 174; sql: 15
file content (74 lines) | stat: -rw-r--r-- 2,112 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//@ compile-flags: -Zunleash-the-miri-inside-of-you
//@ aux-build:static_cross_crate.rs
//@ normalize-stderr: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
//@ normalize-stderr: "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"

#![feature(half_open_range_patterns_in_slices)]
#![allow(static_mut_refs)]

extern crate static_cross_crate;

// Sneaky: reference to a mutable static.
// Allowing this would be a disaster for pattern matching, we could violate exhaustiveness checking!
const SLICE_MUT: &[u8; 1] = {
    //~^ ERROR encountered reference to mutable memory
    unsafe { &static_cross_crate::ZERO }
};

const U8_MUT: &u8 = {
    //~^ ERROR encountered reference to mutable memory
    unsafe { &static_cross_crate::ZERO[0] }
};

// Also test indirection that reads from other static.
const U8_MUT2: &u8 = {
    //~^ ERROR encountered reference to mutable memory
    unsafe { &(*static_cross_crate::ZERO_REF)[0] }
};
const U8_MUT3: &u8 = {
    unsafe {
        match static_cross_crate::OPT_ZERO {
            //~^ ERROR constant accesses mutable global memory
            Some(ref u) => u,
            None => panic!(),
        }
    }
};

pub fn test(x: &[u8; 1]) -> bool {
    match x {
        SLICE_MUT => true, // ok, `const` error already emitted
        &[1..] => false,
    }
}

pub fn test2(x: &u8) -> bool {
    match x {
        U8_MUT => true, // ok, `const` error already emitted
        &(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, // ok, `const` error already emitted
        &(1..) => false,
    }
}
pub fn test4(x: &u8) -> bool {
    match x {
        U8_MUT3 => true, // ok, `const` error already emitted
        &(1..) => false,
    }
}

fn main() {
    unsafe {
        static_cross_crate::ZERO[0] = 1;
    }
    // Now the pattern is not exhaustive any more!
    test(&[0]);
    test2(&0);
}