From 6b19a48e708609b456d3a6e90cafd63a6c348e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 13 Aug 2022 00:00:00 +0000 Subject: [PATCH] `assert_{inhabited,zero_valid,uninit_valid}` intrinsics are safe Those intrinsics either panic or do nothing. They are safe. --- compiler/rustc_typeck/src/check/intrinsic.rs | 3 +++ src/test/ui/consts/assert-type-intrinsics.rs | 4 ++-- src/test/ui/consts/assert-type-intrinsics.stderr | 8 ++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_typeck/src/check/intrinsic.rs b/compiler/rustc_typeck/src/check/intrinsic.rs index 3f2a0da8d6515..05686be5d4b3d 100644 --- a/compiler/rustc_typeck/src/check/intrinsic.rs +++ b/compiler/rustc_typeck/src/check/intrinsic.rs @@ -69,6 +69,9 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety { // to note that it's safe to call, since // safe extern fns are otherwise unprecedented. sym::abort + | sym::assert_inhabited + | sym::assert_zero_valid + | sym::assert_uninit_valid | sym::size_of | sym::min_align_of | sym::needs_drop diff --git a/src/test/ui/consts/assert-type-intrinsics.rs b/src/test/ui/consts/assert-type-intrinsics.rs index 38e5c454edf75..3ce3e1bdbac0f 100644 --- a/src/test/ui/consts/assert-type-intrinsics.rs +++ b/src/test/ui/consts/assert-type-intrinsics.rs @@ -13,10 +13,10 @@ fn main() { const _BAD1: () = unsafe { MaybeUninit::::uninit().assume_init(); }; - const _BAD2: () = unsafe { + const _BAD2: () = { intrinsics::assert_uninit_valid::(); }; - const _BAD3: () = unsafe { + const _BAD3: () = { intrinsics::assert_zero_valid::<&'static i32>(); }; } diff --git a/src/test/ui/consts/assert-type-intrinsics.stderr b/src/test/ui/consts/assert-type-intrinsics.stderr index f3b9170d428af..6eab10197b855 100644 --- a/src/test/ui/consts/assert-type-intrinsics.stderr +++ b/src/test/ui/consts/assert-type-intrinsics.stderr @@ -13,7 +13,7 @@ LL | MaybeUninit::::uninit().assume_init(); error: any use of this value will cause an error --> $DIR/assert-type-intrinsics.rs:17:9 | -LL | const _BAD2: () = unsafe { +LL | const _BAD2: () = { | --------------- LL | intrinsics::assert_uninit_valid::(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `bool` uninitialized, which is invalid @@ -24,7 +24,7 @@ LL | intrinsics::assert_uninit_valid::(); error: any use of this value will cause an error --> $DIR/assert-type-intrinsics.rs:20:9 | -LL | const _BAD3: () = unsafe { +LL | const _BAD3: () = { | --------------- LL | intrinsics::assert_zero_valid::<&'static i32>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `&i32`, which is invalid @@ -51,7 +51,7 @@ Future breakage diagnostic: error: any use of this value will cause an error --> $DIR/assert-type-intrinsics.rs:17:9 | -LL | const _BAD2: () = unsafe { +LL | const _BAD2: () = { | --------------- LL | intrinsics::assert_uninit_valid::(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `bool` uninitialized, which is invalid @@ -64,7 +64,7 @@ Future breakage diagnostic: error: any use of this value will cause an error --> $DIR/assert-type-intrinsics.rs:20:9 | -LL | const _BAD3: () = unsafe { +LL | const _BAD3: () = { | --------------- LL | intrinsics::assert_zero_valid::<&'static i32>(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `&i32`, which is invalid