]> git.proxmox.com Git - rustc.git/blob - src/test/ui/consts/const-eval/ub-wide-ptr.rs
New upstream version 1.46.0~beta.2+dfsg1
[rustc.git] / src / test / ui / consts / const-eval / ub-wide-ptr.rs
1 // ignore-tidy-linelength
2 #![allow(unused)]
3 #![allow(const_err)] // make sure we cannot allow away the errors tested here
4
5 use std::mem;
6
7 // normalize-stderr-test "offset \d+" -> "offset N"
8 // normalize-stderr-test "alloc\d+" -> "allocN"
9 // normalize-stderr-test "size \d+" -> "size N"
10
11 #[repr(C)]
12 union MaybeUninit<T: Copy> {
13 uninit: (),
14 init: T,
15 }
16
17 trait Trait {}
18 impl Trait for bool {}
19
20 // custom unsized type
21 struct MyStr(str);
22
23 // custom unsized type with sized fields
24 struct MySlice<T: ?Sized>(bool, T);
25 type MySliceBool = MySlice<[bool]>;
26
27 // # str
28 // OK
29 const STR_VALID: &str = unsafe { mem::transmute((&42u8, 1usize)) };
30 // bad str
31 const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
32 //~^ ERROR it is undefined behavior to use this value
33 const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },);
34 //~^ ERROR it is undefined behavior to use this value
35 // bad str
36 const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
37 //~^ ERROR it is undefined behavior to use this value
38 // bad str in user-defined unsized type
39 const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
40 //~^ ERROR it is undefined behavior to use this value
41 const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
42 //~^ ERROR it is undefined behavior to use this value
43
44 // uninitialized byte
45 const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
46 //~^ ERROR it is undefined behavior to use this value
47 // uninitialized byte in user-defined str-like
48 const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
49 //~^ ERROR it is undefined behavior to use this value
50
51 // # slice
52 // OK
53 const SLICE_VALID: &[u8] = unsafe { mem::transmute((&42u8, 1usize)) };
54 // bad slice: length uninit
55 const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
56 //~^ ERROR it is undefined behavior to use this value
57 let uninit_len = MaybeUninit::<usize> { uninit: () };
58 mem::transmute((42, uninit_len))
59 };
60 // bad slice: length too big
61 const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
62 //~^ ERROR it is undefined behavior to use this value
63 // bad slice: length not an int
64 const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
65 //~^ ERROR it is undefined behavior to use this value
66 // bad slice box: length too big
67 const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
68 //~^ ERROR it is undefined behavior to use this value
69 // bad slice box: length not an int
70 const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
71 //~^ ERROR it is undefined behavior to use this value
72
73 // bad data *inside* the slice
74 const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
75 //~^ ERROR it is undefined behavior to use this value
76
77 // good MySliceBool
78 const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
79 // bad: sized field is not okay
80 const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
81 //~^ ERROR it is undefined behavior to use this value
82 // bad: unsized part is not okay
83 const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
84 //~^ ERROR it is undefined behavior to use this value
85
86 // # raw slice
87 const RAW_SLICE_VALID: *const [u8] = unsafe { mem::transmute((&42u8, 1usize)) }; // ok
88 const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, 999usize)) }; // ok because raw
89 const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { mem::transmute((&42u8, usize::MAX)) }; // ok because raw
90 const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
91 //~^ ERROR it is undefined behavior to use this value
92 let uninit_len = MaybeUninit::<usize> { uninit: () };
93 mem::transmute((42, uninit_len))
94 };
95
96 // # trait object
97 // bad trait object
98 const TRAIT_OBJ_SHORT_VTABLE_1: &dyn Trait = unsafe { mem::transmute((&92u8, &3u8)) };
99 //~^ ERROR it is undefined behavior to use this value
100 // bad trait object
101 const TRAIT_OBJ_SHORT_VTABLE_2: &dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
102 //~^ ERROR it is undefined behavior to use this value
103 // bad trait object
104 const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4usize)) };
105 //~^ ERROR it is undefined behavior to use this value
106 const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
107 //~^ ERROR it is undefined behavior to use this value
108 const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
109 //~^ ERROR it is undefined behavior to use this value
110 const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
111 //~^ ERROR it is undefined behavior to use this value
112 const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: &dyn Trait = unsafe { mem::transmute((&92u8, &[&42u8; 8])) };
113 //~^ ERROR it is undefined behavior to use this value
114
115 // bad data *inside* the trait object
116 const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
117 //~^ ERROR it is undefined behavior to use this value
118
119 // # raw trait object
120 const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
121 //~^ ERROR it is undefined behavior to use this value
122 const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
123 //~^ ERROR it is undefined behavior to use this value
124 const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
125
126 // Const eval fails for these, so they need to be statics to error.
127 static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
128 mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
129 //~^ ERROR could not evaluate static initializer
130 };
131 static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
132 mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
133 //~^ ERROR could not evaluate static initializer
134 };
135
136 fn main() {}