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.
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.
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"
24 #[derive(Copy, Clone)]
31 #[derive(Copy, Clone)]
37 union SliceTransmute {
42 my_str: &'static MyStr,
43 my_slice: &'static MySliceBool,
47 #[derive(Copy, Clone)]
54 #[derive(Copy, Clone)]
61 #[derive(Copy, Clone)]
75 impl Trait for bool {}
77 // custom unsized type
80 // custom unsized type with sized fields
81 struct MySlice<T: ?Sized>(bool, T);
82 type MySliceBool = MySlice<[bool]>;
85 const A: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
87 const B: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
88 //~^ ERROR this constant likely exhibits undefined behavior
90 const C: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
91 //~^ ERROR this constant likely exhibits undefined behavior
92 // bad str in user-defined unsized type
93 const C2: &MyStr = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.my_str};
94 //~^ ERROR this constant likely exhibits undefined behavior
97 const A2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
99 const B2: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
100 //~^ ERROR this constant likely exhibits undefined behavior
102 const C3: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
103 //~^ ERROR this constant likely exhibits undefined behavior
106 const D: &Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
107 //~^ ERROR this constant likely exhibits undefined behavior
109 const E: &Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
110 //~^ ERROR this constant likely exhibits undefined behavior
112 const F: &Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
113 //~^ ERROR this constant likely exhibits undefined behavior
115 // bad data *inside* the trait object
116 const G: &Trait = &unsafe { BoolTransmute { val: 3 }.bl };
117 //~^ ERROR this constant likely exhibits undefined behavior
118 // bad data *inside* the slice
119 const H: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
120 //~^ ERROR this constant likely exhibits undefined behavior
123 const I1: &MySliceBool = &MySlice(true, [false]);
124 // bad: sized field is not okay
125 const I2: &MySliceBool = &MySlice(unsafe { BoolTransmute { val: 3 }.bl }, [false]);
126 //~^ ERROR this constant likely exhibits undefined behavior
127 // bad: unsized part is not okay
128 const I3: &MySliceBool = &MySlice(true, [unsafe { BoolTransmute { val: 3 }.bl }]);
129 //~^ ERROR this constant likely exhibits undefined behavior
132 const J1: &str = unsafe { SliceTransmute { slice: &[0xFF] }.str };
133 //~^ ERROR this constant likely exhibits undefined behavior
134 // invalid UTF-8 in user-defined str-like
135 const J2: &MyStr = unsafe { SliceTransmute { slice: &[0xFF] }.my_str };
136 //~^ ERROR this constant likely exhibits undefined behavior