]> git.proxmox.com Git - rustc.git/blame - src/test/ui/union-ub-fat-ptr.rs
New upstream version 1.30.0+dfsg1
[rustc.git] / src / test / ui / union-ub-fat-ptr.rs
CommitLineData
8faf50e0
XL
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
b7449926
XL
11#![allow(unused)]
12
13// normalize-stderr-test "alignment \d+" -> "alignment N"
14// normalize-stderr-test "offset \d+" -> "offset N"
15// normalize-stderr-test "allocation \d+" -> "allocation N"
16// normalize-stderr-test "size \d+" -> "size N"
17
18union BoolTransmute {
19 val: u8,
20 bl: bool,
21}
22
8faf50e0
XL
23#[repr(C)]
24#[derive(Copy, Clone)]
25struct SliceRepr {
26 ptr: *const u8,
27 len: usize,
28}
29
30#[repr(C)]
31#[derive(Copy, Clone)]
32struct BadSliceRepr {
33 ptr: *const u8,
34 len: &'static u8,
35}
36
37union SliceTransmute {
38 repr: SliceRepr,
39 bad: BadSliceRepr,
40 slice: &'static [u8],
41 str: &'static str,
b7449926
XL
42 my_str: &'static MyStr,
43 my_slice: &'static MySliceBool,
8faf50e0
XL
44}
45
46#[repr(C)]
47#[derive(Copy, Clone)]
48struct DynRepr {
49 ptr: *const u8,
50 vtable: *const u8,
51}
52
53#[repr(C)]
54#[derive(Copy, Clone)]
55struct DynRepr2 {
56 ptr: *const u8,
57 vtable: *const u64,
58}
59
60#[repr(C)]
61#[derive(Copy, Clone)]
62struct BadDynRepr {
63 ptr: *const u8,
64 vtable: usize,
65}
66
67union DynTransmute {
68 repr: DynRepr,
69 repr2: DynRepr2,
70 bad: BadDynRepr,
71 rust: &'static Trait,
72}
73
74trait Trait {}
b7449926
XL
75impl Trait for bool {}
76
77// custom unsized type
78struct MyStr(str);
79
80// custom unsized type with sized fields
81struct MySlice<T: ?Sized>(bool, T);
82type MySliceBool = MySlice<[bool]>;
8faf50e0
XL
83
84// OK
85const A: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
b7449926 86// bad str
8faf50e0 87const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
b7449926
XL
88//~^ ERROR this constant likely exhibits undefined behavior
89// bad str
8faf50e0
XL
90const C: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
91//~^ ERROR this constant likely exhibits undefined behavior
b7449926
XL
92// bad str in user-defined unsized type
93const C2: &MyStr = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.my_str};
94//~^ ERROR this constant likely exhibits undefined behavior
8faf50e0
XL
95
96// OK
97const A2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
b7449926 98// bad slice
8faf50e0 99const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
b7449926
XL
100//~^ ERROR this constant likely exhibits undefined behavior
101// bad slice
102const C3: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
8faf50e0
XL
103//~^ ERROR this constant likely exhibits undefined behavior
104
b7449926 105// bad trait object
8faf50e0
XL
106const D: &Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
107//~^ ERROR this constant likely exhibits undefined behavior
b7449926 108// bad trait object
8faf50e0
XL
109const E: &Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
110//~^ ERROR this constant likely exhibits undefined behavior
b7449926 111// bad trait object
8faf50e0
XL
112const F: &Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
113//~^ ERROR this constant likely exhibits undefined behavior
114
b7449926
XL
115// bad data *inside* the trait object
116const G: &Trait = &unsafe { BoolTransmute { val: 3 }.bl };
117//~^ ERROR this constant likely exhibits undefined behavior
118// bad data *inside* the slice
119const H: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
120//~^ ERROR this constant likely exhibits undefined behavior
121
122// good MySliceBool
123const I1: &MySliceBool = &MySlice(true, [false]);
124// bad: sized field is not okay
125const I2: &MySliceBool = &MySlice(unsafe { BoolTransmute { val: 3 }.bl }, [false]);
126//~^ ERROR this constant likely exhibits undefined behavior
127// bad: unsized part is not okay
128const I3: &MySliceBool = &MySlice(true, [unsafe { BoolTransmute { val: 3 }.bl }]);
129//~^ ERROR this constant likely exhibits undefined behavior
130
131// invalid UTF-8
132const J1: &str = unsafe { SliceTransmute { slice: &[0xFF] }.str };
133//~^ ERROR this constant likely exhibits undefined behavior
134// invalid UTF-8 in user-defined str-like
135const J2: &MyStr = unsafe { SliceTransmute { slice: &[0xFF] }.my_str };
136//~^ ERROR this constant likely exhibits undefined behavior
137
8faf50e0
XL
138fn main() {
139}