Skip to content

Commit 067f1b7

Browse files
authored
Rollup merge of #80491 - RalfJung:dangling-of-val, r=oli-obk
Miri: make size/align_of_val work for dangling raw ptrs This is needed for #80365 (comment). r? `@oli-obk`
2 parents 3d93dfd + f76bae9 commit 067f1b7

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

compiler/rustc_mir/src/interpret/intrinsics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
141141
}
142142

143143
sym::min_align_of_val | sym::size_of_val => {
144-
let place = self.deref_operand(args[0])?;
144+
// Avoid `deref_operand` -- this is not a deref, the ptr does not have to be
145+
// dereferencable!
146+
let place = self.ref_to_mplace(self.read_immediate(args[0])?)?;
145147
let (size, align) = self
146-
.size_and_align_of(place.meta, place.layout)?
148+
.size_and_align_of_mplace(place)?
147149
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
148150

149151
let result = match intrinsic_name {

compiler/rustc_mir/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
391391
}
392392
// Make sure this is dereferenceable and all.
393393
let size_and_align = try_validation!(
394-
self.ecx.size_and_align_of(place.meta, place.layout),
394+
self.ecx.size_and_align_of_mplace(place),
395395
self.path,
396396
err_ub!(InvalidMeta(msg)) => { "invalid {} metadata: {}", kind, msg },
397397
);

library/core/src/mem/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
379379
/// ```
380380
#[inline]
381381
#[unstable(feature = "layout_for_ptr", issue = "69835")]
382-
pub unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
382+
#[rustc_const_unstable(feature = "const_size_of_val_raw", issue = "46571")]
383+
pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
383384
intrinsics::size_of_val(val)
384385
}
385386

@@ -510,7 +511,8 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
510511
/// ```
511512
#[inline]
512513
#[unstable(feature = "layout_for_ptr", issue = "69835")]
513-
pub unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
514+
#[rustc_const_unstable(feature = "const_align_of_val_raw", issue = "46571")]
515+
pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
514516
intrinsics::min_align_of_val(val)
515517
}
516518

src/test/ui/consts/const-size_of_val-align_of_val.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-pass
22

33
#![feature(const_size_of_val, const_align_of_val)]
4+
#![feature(const_size_of_val_raw, const_align_of_val_raw, layout_for_ptr)]
45

56
use std::mem;
67

@@ -32,6 +33,9 @@ const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH);
3233

3334
const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes());
3435

36+
const SIZE_OF_DANGLING: usize = unsafe { mem::size_of_val_raw(0x100 as *const i32) };
37+
const ALIGN_OF_DANGLING: usize = unsafe { mem::align_of_val_raw(0x100 as *const i16) };
38+
3539
fn main() {
3640
assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>());
3741
assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>());
@@ -41,5 +45,8 @@ fn main() {
4145
assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>());
4246
assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>());
4347

48+
assert_eq!(SIZE_OF_DANGLING, mem::size_of::<i32>());
49+
assert_eq!(ALIGN_OF_DANGLING, mem::align_of::<i16>());
50+
4451
assert_eq!(SIZE_OF_SLICE, "foobar".len());
4552
}

0 commit comments

Comments
 (0)