Skip to content

Commit b07419b

Browse files
authored
Rollup merge of rust-lang#121806 - RalfJung:const-metadata, r=oli-obk
add const test for ptr::metadata rust-lang#121199 uncovered this as a gap in our test suite. r? `@oli-obk`
2 parents 50e738b + 3ed175c commit b07419b

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

Diff for: tests/ui/consts/const-eval/ub-wide-ptr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// ignore-tidy-linelength
22
#![allow(unused)]
3+
#![feature(ptr_metadata)]
34

4-
use std::mem;
5+
use std::{ptr, mem};
56

67
// Strip out raw byte dumps to make comparison platform-independent:
78
//@ normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
@@ -145,6 +146,8 @@ const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92
145146
const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
146147
//~^ ERROR it is undefined behavior to use this value
147148
const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
149+
// Officially blessed way to get the vtable
150+
const DYN_METADATA: ptr::DynMetadata<dyn Send> = ptr::metadata::<dyn Send>(ptr::null::<i32>());
148151

149152
// Const eval fails for these, so they need to be statics to error.
150153
static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {

Diff for: tests/ui/consts/const-eval/ub-wide-ptr.stderr

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/ub-wide-ptr.rs:38:1
2+
--> $DIR/ub-wide-ptr.rs:39:1
33
|
44
LL | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation)
@@ -10,7 +10,7 @@ LL | const STR_TOO_LONG: &str = unsafe { mem::transmute((&42u8, 999usize)) };
1010
}
1111

1212
error[E0080]: it is undefined behavior to use this value
13-
--> $DIR/ub-wide-ptr.rs:40:1
13+
--> $DIR/ub-wide-ptr.rs:41:1
1414
|
1515
LL | const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, usize::MAX)) },);
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered invalid reference metadata: slice is bigger than largest supported object
@@ -21,7 +21,7 @@ LL | const NESTED_STR_MUCH_TOO_LONG: (&str,) = (unsafe { mem::transmute((&42, us
2121
}
2222

2323
error[E0080]: evaluation of constant value failed
24-
--> $DIR/ub-wide-ptr.rs:43:1
24+
--> $DIR/ub-wide-ptr.rs:44:1
2525
|
2626
LL | const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
2727
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -30,7 +30,7 @@ LL | const STR_LENGTH_PTR: &str = unsafe { mem::transmute((&42u8, &3)) };
3030
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
3131

3232
error[E0080]: evaluation of constant value failed
33-
--> $DIR/ub-wide-ptr.rs:46:1
33+
--> $DIR/ub-wide-ptr.rs:47:1
3434
|
3535
LL | const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
3636
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -39,7 +39,7 @@ LL | const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
3939
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
4040

4141
error[E0080]: it is undefined behavior to use this value
42-
--> $DIR/ub-wide-ptr.rs:48:1
42+
--> $DIR/ub-wide-ptr.rs:49:1
4343
|
4444
LL | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
4545
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object
@@ -50,7 +50,7 @@ LL | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize:
5050
}
5151

5252
error[E0080]: it is undefined behavior to use this value
53-
--> $DIR/ub-wide-ptr.rs:52:1
53+
--> $DIR/ub-wide-ptr.rs:53:1
5454
|
5555
LL | const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
5656
| ^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>: encountered uninitialized memory, but expected a string
@@ -61,7 +61,7 @@ LL | const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit:
6161
}
6262

6363
error[E0080]: it is undefined behavior to use this value
64-
--> $DIR/ub-wide-ptr.rs:55:1
64+
--> $DIR/ub-wide-ptr.rs:56:1
6565
|
6666
LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>.0: encountered uninitialized memory, but expected a string
@@ -72,13 +72,13 @@ LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUni
7272
}
7373

7474
error[E0080]: evaluation of constant value failed
75-
--> $DIR/ub-wide-ptr.rs:62:1
75+
--> $DIR/ub-wide-ptr.rs:63:1
7676
|
7777
LL | const SLICE_LENGTH_UNINIT: &[u8] = unsafe {
7878
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
7979

8080
error[E0080]: it is undefined behavior to use this value
81-
--> $DIR/ub-wide-ptr.rs:69:1
81+
--> $DIR/ub-wide-ptr.rs:70:1
8282
|
8383
LL | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
8484
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation)
@@ -89,7 +89,7 @@ LL | const SLICE_TOO_LONG: &[u8] = unsafe { mem::transmute((&42u8, 999usize)) };
8989
}
9090

9191
error[E0080]: it is undefined behavior to use this value
92-
--> $DIR/ub-wide-ptr.rs:72:1
92+
--> $DIR/ub-wide-ptr.rs:73:1
9393
|
9494
LL | const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, isize::MAX)) };
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object
@@ -100,7 +100,7 @@ LL | const SLICE_TOO_LONG_OVERFLOW: &[u32] = unsafe { mem::transmute((&42u32, is
100100
}
101101

102102
error[E0080]: evaluation of constant value failed
103-
--> $DIR/ub-wide-ptr.rs:75:1
103+
--> $DIR/ub-wide-ptr.rs:76:1
104104
|
105105
LL | const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
106106
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -109,7 +109,7 @@ LL | const SLICE_LENGTH_PTR: &[u8] = unsafe { mem::transmute((&42u8, &3)) };
109109
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
110110

111111
error[E0080]: it is undefined behavior to use this value
112-
--> $DIR/ub-wide-ptr.rs:78:1
112+
--> $DIR/ub-wide-ptr.rs:79:1
113113
|
114114
LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999usize)) };
115115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a dangling box (going beyond the bounds of its allocation)
@@ -120,7 +120,7 @@ LL | const SLICE_TOO_LONG_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, 999us
120120
}
121121

122122
error[E0080]: evaluation of constant value failed
123-
--> $DIR/ub-wide-ptr.rs:81:1
123+
--> $DIR/ub-wide-ptr.rs:82:1
124124
|
125125
LL | const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)) };
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into integer
@@ -129,7 +129,7 @@ LL | const SLICE_LENGTH_PTR_BOX: Box<[u8]> = unsafe { mem::transmute((&42u8, &3)
129129
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
130130

131131
error[E0080]: it is undefined behavior to use this value
132-
--> $DIR/ub-wide-ptr.rs:85:1
132+
--> $DIR/ub-wide-ptr.rs:86:1
133133
|
134134
LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
135135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x03, but expected a boolean
@@ -140,13 +140,13 @@ LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
140140
}
141141

142142
note: erroneous constant encountered
143-
--> $DIR/ub-wide-ptr.rs:85:40
143+
--> $DIR/ub-wide-ptr.rs:86:40
144144
|
145145
LL | const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { mem::transmute(3u8) }];
146146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147147

148148
error[E0080]: it is undefined behavior to use this value
149-
--> $DIR/ub-wide-ptr.rs:92:1
149+
--> $DIR/ub-wide-ptr.rs:93:1
150150
|
151151
LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
152152
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>.0: encountered 0x03, but expected a boolean
@@ -157,13 +157,13 @@ LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3
157157
}
158158

159159
note: erroneous constant encountered
160-
--> $DIR/ub-wide-ptr.rs:92:42
160+
--> $DIR/ub-wide-ptr.rs:93:42
161161
|
162162
LL | const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { mem::transmute(3u8) }, [false]);
163163
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164164

165165
error[E0080]: it is undefined behavior to use this value
166-
--> $DIR/ub-wide-ptr.rs:96:1
166+
--> $DIR/ub-wide-ptr.rs:97:1
167167
|
168168
LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
169169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>.1[0]: encountered 0x03, but expected a boolean
@@ -174,19 +174,19 @@ LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::tran
174174
}
175175

176176
note: erroneous constant encountered
177-
--> $DIR/ub-wide-ptr.rs:96:42
177+
--> $DIR/ub-wide-ptr.rs:97:42
178178
|
179179
LL | const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { mem::transmute(3u8) }]);
180180
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
181181

182182
error[E0080]: evaluation of constant value failed
183-
--> $DIR/ub-wide-ptr.rs:104:1
183+
--> $DIR/ub-wide-ptr.rs:105:1
184184
|
185185
LL | const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe {
186186
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
187187

188188
error[E0080]: it is undefined behavior to use this value
189-
--> $DIR/ub-wide-ptr.rs:113:1
189+
--> $DIR/ub-wide-ptr.rs:114:1
190190
|
191191
LL | const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u8))) };
192192
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered ALLOC12<imm>, but expected a vtable pointer
@@ -197,7 +197,7 @@ LL | const TRAIT_OBJ_SHORT_VTABLE_1: W<&dyn Trait> = unsafe { mem::transmute(W((
197197
}
198198

199199
error[E0080]: it is undefined behavior to use this value
200-
--> $DIR/ub-wide-ptr.rs:117:1
200+
--> $DIR/ub-wide-ptr.rs:118:1
201201
|
202202
LL | const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &3u64))) };
203203
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered ALLOC14<imm>, but expected a vtable pointer
@@ -208,7 +208,7 @@ LL | const TRAIT_OBJ_SHORT_VTABLE_2: W<&dyn Trait> = unsafe { mem::transmute(W((
208208
}
209209

210210
error[E0080]: it is undefined behavior to use this value
211-
--> $DIR/ub-wide-ptr.rs:121:1
211+
--> $DIR/ub-wide-ptr.rs:122:1
212212
|
213213
LL | const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, 4usize))) };
214214
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered 0x4[noalloc], but expected a vtable pointer
@@ -219,7 +219,7 @@ LL | const TRAIT_OBJ_INT_VTABLE: W<&dyn Trait> = unsafe { mem::transmute(W((&92u
219219
}
220220

221221
error[E0080]: it is undefined behavior to use this value
222-
--> $DIR/ub-wide-ptr.rs:124:1
222+
--> $DIR/ub-wide-ptr.rs:125:1
223223
|
224224
LL | const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
225225
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered ALLOC17<imm>, but expected a vtable pointer
@@ -230,7 +230,7 @@ LL | const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92
230230
}
231231

232232
error[E0080]: it is undefined behavior to use this value
233-
--> $DIR/ub-wide-ptr.rs:127:1
233+
--> $DIR/ub-wide-ptr.rs:128:1
234234
|
235235
LL | const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92u8, &[0usize; 8])) };
236236
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered ALLOC19<imm>, but expected a vtable pointer
@@ -241,7 +241,7 @@ LL | const TRAIT_OBJ_BAD_DROP_FN_NULL: &dyn Trait = unsafe { mem::transmute((&92
241241
}
242242

243243
error[E0080]: it is undefined behavior to use this value
244-
--> $DIR/ub-wide-ptr.rs:130:1
244+
--> $DIR/ub-wide-ptr.rs:131:1
245245
|
246246
LL | const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u8, &[1usize; 8])) };
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered ALLOC21<imm>, but expected a vtable pointer
@@ -252,7 +252,7 @@ LL | const TRAIT_OBJ_BAD_DROP_FN_INT: &dyn Trait = unsafe { mem::transmute((&92u
252252
}
253253

254254
error[E0080]: it is undefined behavior to use this value
255-
--> $DIR/ub-wide-ptr.rs:133:1
255+
--> $DIR/ub-wide-ptr.rs:134:1
256256
|
257257
LL | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::transmute(W((&92u8, &[&42u8; 8]))) };
258258
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .0: encountered ALLOC23<imm>, but expected a vtable pointer
@@ -263,7 +263,7 @@ LL | const TRAIT_OBJ_BAD_DROP_FN_NOT_FN_PTR: W<&dyn Trait> = unsafe { mem::trans
263263
}
264264

265265
error[E0080]: it is undefined behavior to use this value
266-
--> $DIR/ub-wide-ptr.rs:138:1
266+
--> $DIR/ub-wide-ptr.rs:139:1
267267
|
268268
LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
269269
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>.<dyn-downcast>: encountered 0x03, but expected a boolean
@@ -274,7 +274,7 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_,
274274
}
275275

276276
error[E0080]: it is undefined behavior to use this value
277-
--> $DIR/ub-wide-ptr.rs:143:1
277+
--> $DIR/ub-wide-ptr.rs:144:1
278278
|
279279
LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
280280
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer
@@ -285,7 +285,7 @@ LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute
285285
}
286286

287287
error[E0080]: it is undefined behavior to use this value
288-
--> $DIR/ub-wide-ptr.rs:145:1
288+
--> $DIR/ub-wide-ptr.rs:146:1
289289
|
290290
LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
291291
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered ALLOC28<imm>, but expected a vtable pointer
@@ -296,7 +296,7 @@ LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transm
296296
}
297297

298298
error[E0080]: it is undefined behavior to use this value
299-
--> $DIR/ub-wide-ptr.rs:150:1
299+
--> $DIR/ub-wide-ptr.rs:153:1
300300
|
301301
LL | static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe {
302302
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer
@@ -307,7 +307,7 @@ LL | static mut RAW_TRAIT_OBJ_VTABLE_NULL_THROUGH_REF: *const dyn Trait = unsafe
307307
}
308308

309309
error[E0080]: it is undefined behavior to use this value
310-
--> $DIR/ub-wide-ptr.rs:154:1
310+
--> $DIR/ub-wide-ptr.rs:157:1
311311
|
312312
LL | static mut RAW_TRAIT_OBJ_VTABLE_INVALID_THROUGH_REF: *const dyn Trait = unsafe {
313313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered ALLOC31<imm>, but expected a vtable pointer

0 commit comments

Comments
 (0)