]> git.lizzy.rs Git - rust.git/blob - src/test/ui/union-ub-fat-ptr.rs
Rollup merge of #53317 - estebank:abolish-ice, r=oli-obk
[rust.git] / src / test / ui / union-ub-fat-ptr.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // normalize-stderr-test "alignment \d+" -> "alignment N"
12 // normalize-stderr-test "offset \d+" -> "offset N"
13 // normalize-stderr-test "allocation \d+" -> "allocation N"
14 // normalize-stderr-test "size \d+" -> "size N"
15
16 #[repr(C)]
17 #[derive(Copy, Clone)]
18 struct SliceRepr {
19     ptr: *const u8,
20     len: usize,
21 }
22
23 #[repr(C)]
24 #[derive(Copy, Clone)]
25 struct BadSliceRepr {
26     ptr: *const u8,
27     len: &'static u8,
28 }
29
30 union SliceTransmute {
31     repr: SliceRepr,
32     bad: BadSliceRepr,
33     slice: &'static [u8],
34     str: &'static str,
35 }
36
37 #[repr(C)]
38 #[derive(Copy, Clone)]
39 struct DynRepr {
40     ptr: *const u8,
41     vtable: *const u8,
42 }
43
44 #[repr(C)]
45 #[derive(Copy, Clone)]
46 struct DynRepr2 {
47     ptr: *const u8,
48     vtable: *const u64,
49 }
50
51 #[repr(C)]
52 #[derive(Copy, Clone)]
53 struct BadDynRepr {
54     ptr: *const u8,
55     vtable: usize,
56 }
57
58 union DynTransmute {
59     repr: DynRepr,
60     repr2: DynRepr2,
61     bad: BadDynRepr,
62     rust: &'static Trait,
63 }
64
65 trait Trait {}
66
67 // OK
68 const A: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
69 // should lint
70 const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
71 // bad
72 const C: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
73 //~^ ERROR this constant likely exhibits undefined behavior
74
75 // OK
76 const A2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
77 // should lint
78 const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
79 // bad
80 const C2: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
81 //~^ ERROR this constant likely exhibits undefined behavior
82
83 // bad
84 const D: &Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
85 //~^ ERROR this constant likely exhibits undefined behavior
86 // bad
87 const E: &Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
88 //~^ ERROR this constant likely exhibits undefined behavior
89 // bad
90 const F: &Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
91 //~^ ERROR this constant likely exhibits undefined behavior
92
93 fn main() {
94 }