Skip to content

Commit 9de9786

Browse files
authored
Rollup merge of #100487 - tmiasko:assert-safe, r=petrochenkov
`assert_{inhabited,zero_valid,uninit_valid}` intrinsics are safe Those intrinsics either panic or do nothing. They are safe.
2 parents 38bc937 + 6b19a48 commit 9de9786

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

compiler/rustc_typeck/src/check/intrinsic.rs

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety {
6969
// to note that it's safe to call, since
7070
// safe extern fns are otherwise unprecedented.
7171
sym::abort
72+
| sym::assert_inhabited
73+
| sym::assert_zero_valid
74+
| sym::assert_uninit_valid
7275
| sym::size_of
7376
| sym::min_align_of
7477
| sym::needs_drop

src/test/ui/consts/assert-type-intrinsics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ fn main() {
1313
const _BAD1: () = unsafe {
1414
MaybeUninit::<!>::uninit().assume_init();
1515
};
16-
const _BAD2: () = unsafe {
16+
const _BAD2: () = {
1717
intrinsics::assert_uninit_valid::<bool>();
1818
};
19-
const _BAD3: () = unsafe {
19+
const _BAD3: () = {
2020
intrinsics::assert_zero_valid::<&'static i32>();
2121
};
2222
}

src/test/ui/consts/assert-type-intrinsics.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | MaybeUninit::<!>::uninit().assume_init();
1313
error: any use of this value will cause an error
1414
--> $DIR/assert-type-intrinsics.rs:17:9
1515
|
16-
LL | const _BAD2: () = unsafe {
16+
LL | const _BAD2: () = {
1717
| ---------------
1818
LL | intrinsics::assert_uninit_valid::<bool>();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `bool` uninitialized, which is invalid
@@ -24,7 +24,7 @@ LL | intrinsics::assert_uninit_valid::<bool>();
2424
error: any use of this value will cause an error
2525
--> $DIR/assert-type-intrinsics.rs:20:9
2626
|
27-
LL | const _BAD3: () = unsafe {
27+
LL | const _BAD3: () = {
2828
| ---------------
2929
LL | intrinsics::assert_zero_valid::<&'static i32>();
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `&i32`, which is invalid
@@ -51,7 +51,7 @@ Future breakage diagnostic:
5151
error: any use of this value will cause an error
5252
--> $DIR/assert-type-intrinsics.rs:17:9
5353
|
54-
LL | const _BAD2: () = unsafe {
54+
LL | const _BAD2: () = {
5555
| ---------------
5656
LL | intrinsics::assert_uninit_valid::<bool>();
5757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to leave type `bool` uninitialized, which is invalid
@@ -64,7 +64,7 @@ Future breakage diagnostic:
6464
error: any use of this value will cause an error
6565
--> $DIR/assert-type-intrinsics.rs:20:9
6666
|
67-
LL | const _BAD3: () = unsafe {
67+
LL | const _BAD3: () = {
6868
| ---------------
6969
LL | intrinsics::assert_zero_valid::<&'static i32>();
7070
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to zero-initialize type `&i32`, which is invalid

0 commit comments

Comments
 (0)