Skip to content

Commit da305a2

Browse files
committed
Auto merge of rust-lang#80711 - camelid:intrinsic-of-val-safety, r=oli-obk
Make `size_of_val` and `min_align_of_val` intrinsics unsafe Fixes rust-lang#80668. r? `@oli-obk`
2 parents 3b63e16 + bbf175d commit da305a2

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

compiler/rustc_typeck/src/check/intrinsic.rs

-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety {
6363
| sym::min_align_of
6464
| sym::needs_drop
6565
| sym::caller_location
66-
| sym::size_of_val
67-
| sym::min_align_of_val
6866
| sym::add_with_overflow
6967
| sym::sub_with_overflow
7068
| sym::mul_with_overflow

library/core/src/mem/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! types, initializing and manipulating memory.
55
66
#![stable(feature = "rust1", since = "1.0.0")]
7+
#![cfg_attr(bootstrap, allow(unused_unsafe))]
78

89
use crate::clone;
910
use crate::cmp;
@@ -333,7 +334,8 @@ pub const fn size_of<T>() -> usize {
333334
#[stable(feature = "rust1", since = "1.0.0")]
334335
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
335336
pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
336-
intrinsics::size_of_val(val)
337+
// SAFETY: `val` is a reference, so it's a valid raw pointer
338+
unsafe { intrinsics::size_of_val(val) }
337339
}
338340

339341
/// Returns the size of the pointed-to value in bytes.
@@ -381,7 +383,8 @@ pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
381383
#[unstable(feature = "layout_for_ptr", issue = "69835")]
382384
#[rustc_const_unstable(feature = "const_size_of_val_raw", issue = "46571")]
383385
pub const unsafe fn size_of_val_raw<T: ?Sized>(val: *const T) -> usize {
384-
intrinsics::size_of_val(val)
386+
// SAFETY: the caller must provide a valid raw pointer
387+
unsafe { intrinsics::size_of_val(val) }
385388
}
386389

387390
/// Returns the [ABI]-required minimum alignment of a type.
@@ -425,7 +428,8 @@ pub fn min_align_of<T>() -> usize {
425428
#[stable(feature = "rust1", since = "1.0.0")]
426429
#[rustc_deprecated(reason = "use `align_of_val` instead", since = "1.2.0")]
427430
pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize {
428-
intrinsics::min_align_of_val(val)
431+
// SAFETY: val is a reference, so it's a valid raw pointer
432+
unsafe { intrinsics::min_align_of_val(val) }
429433
}
430434

431435
/// Returns the [ABI]-required minimum alignment of a type.
@@ -469,7 +473,8 @@ pub const fn align_of<T>() -> usize {
469473
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
470474
#[allow(deprecated)]
471475
pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
472-
intrinsics::min_align_of_val(val)
476+
// SAFETY: val is a reference, so it's a valid raw pointer
477+
unsafe { intrinsics::min_align_of_val(val) }
473478
}
474479

475480
/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.
@@ -513,7 +518,8 @@ pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
513518
#[unstable(feature = "layout_for_ptr", issue = "69835")]
514519
#[rustc_const_unstable(feature = "const_align_of_val_raw", issue = "46571")]
515520
pub const unsafe fn align_of_val_raw<T: ?Sized>(val: *const T) -> usize {
516-
intrinsics::min_align_of_val(val)
521+
// SAFETY: the caller must provide a valid raw pointer
522+
unsafe { intrinsics::min_align_of_val(val) }
517523
}
518524

519525
/// Returns `true` if dropping values of type `T` matters.

0 commit comments

Comments
 (0)