about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/underscore_pattern.rs
blob: 2f203bdc01e45842b1ea84d9c1fa1845d40695e2 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Various tests ensuring that underscore patterns really just construct the place, but don't check its contents.
#![feature(never_type)]

use std::ptr;

fn main() {
    dangling_match();
    invalid_match();
    dangling_let();
    invalid_let();
    dangling_let_type_annotation();
    invalid_let_type_annotation();
    never();
}

fn dangling_match() {
    let p = {
        let b = Box::new(42);
        &*b as *const i32
    };
    unsafe {
        match *p {
            _ => {}
        }
    }
}

fn invalid_match() {
    union Uninit<T: Copy> {
        value: T,
        uninit: (),
    }
    unsafe {
        let x: Uninit<bool> = Uninit { uninit: () };
        match x.value {
            _ => {}
        }
    }

    unsafe {
        let x: Uninit<!> = Uninit { uninit: () };
        match x.value {
            _ => {}
        }
    }
}

fn dangling_let() {
    unsafe {
        let ptr = ptr::without_provenance::<bool>(0x40);
        let _ = *ptr;
    }

    unsafe {
        let ptr = ptr::without_provenance::<!>(0x40);
        let _ = *ptr;
    }
}

fn invalid_let() {
    unsafe {
        let val = 3u8;
        let ptr = ptr::addr_of!(val).cast::<bool>();
        let _ = *ptr;
    }

    unsafe {
        let val = 3u8;
        let ptr = ptr::addr_of!(val).cast::<!>();
        let _ = *ptr;
    }
}

// Adding a type annotation used to change how MIR is generated, make sure we cover both cases.
fn dangling_let_type_annotation() {
    unsafe {
        let ptr = ptr::without_provenance::<bool>(0x40);
        let _: bool = *ptr;
    }

    unsafe {
        let ptr = ptr::without_provenance::<!>(0x40);
        let _: ! = *ptr;
    }
}

fn invalid_let_type_annotation() {
    unsafe {
        let val = 3u8;
        let ptr = ptr::addr_of!(val).cast::<bool>();
        let _: bool = *ptr;
    }

    unsafe {
        let val = 3u8;
        let ptr = ptr::addr_of!(val).cast::<!>();
        let _: ! = *ptr;
    }
}

// Regression test from <https://github.com/rust-lang/rust/issues/117288>.
fn never() {
    unsafe {
        let x = 3u8;
        let x: *const ! = &x as *const u8 as *const _;
        let _: ! = *x;
    }

    // Without a type annotation, make sure we don't implicitly coerce `!` to `()`
    // when we do the noop `*x` (as that would require a `!` *value*, creating
    // which is UB).
    unsafe {
        let x = 3u8;
        let x: *const ! = &x as *const u8 as *const _;
        let _ = *x;
    }
}