Skip to content

Commit 855b8b8

Browse files
author
Lukas Markeffsky
committed
add tests for const NonNull::as_ref
1 parent 3e07965 commit 855b8b8

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-pass
2+
3+
use std::ptr::NonNull;
4+
5+
const NON_NULL: NonNull<u8> = unsafe { NonNull::new_unchecked((&42u8 as *const u8).cast_mut()) };
6+
const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });
7+
8+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::ptr::NonNull;
2+
3+
const NON_NULL: NonNull<u8> = unsafe { NonNull::dangling() };
4+
const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
3+
|
4+
= note: dereferencing pointer failed: 0x1[noalloc] is a dangling pointer (it has no provenance)
5+
|
6+
note: inside `NonNull::<u8>::as_ref::<'_>`
7+
--> $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
8+
note: inside `_`
9+
--> $DIR/nonnull_as_ref_ub.rs:4:39
10+
|
11+
LL | const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });
12+
| ^^^^^^^^^^^^^^^^^
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)