Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const-stablilize NonNull::as_ref #102198

Merged
merged 2 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,14 @@ impl<T: ?Sized> NonNull<T> {
///
/// [the module documentation]: crate::ptr#safety
#[stable(feature = "nonnull", since = "1.25.0")]
#[rustc_const_unstable(feature = "const_ptr_as_ref", issue = "91822")]
#[rustc_const_stable(feature = "const_nonnull_as_ref", since = "CURRENT_RUSTC_VERSION")]
#[must_use]
#[inline(always)]
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
// SAFETY: the caller must guarantee that `self` meets all the
// requirements for a reference.
unsafe { &*self.as_ptr() }
// `cast_const` avoids a mutable raw pointer deref.
unsafe { &*self.as_ptr().cast_const() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the cast_const needed now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the cast it will fail to compile with error[E0658]: dereferencing raw mutable pointers in constant functions is unstable (playground)

lukas-code marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns a unique reference to the value. If the value may be uninitialized, [`as_uninit_mut`]
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/consts/const-eval/nonnull_as_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass

use std::ptr::NonNull;

const NON_NULL: NonNull<u8> = unsafe { NonNull::new_unchecked((&42u8 as *const u8).cast_mut()) };
const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });

fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/consts/const-eval/nonnull_as_ref_ub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::ptr::NonNull;

const NON_NULL: NonNull<u8> = unsafe { NonNull::dangling() };
const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/consts/const-eval/nonnull_as_ref_ub.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
|
= note: dereferencing pointer failed: 0x1[noalloc] is a dangling pointer (it has no provenance)
|
note: inside `NonNull::<u8>::as_ref::<'_>`
--> $SRC_DIR/core/src/ptr/non_null.rs:LL:COL
note: inside `_`
--> $DIR/nonnull_as_ref_ub.rs:4:39
|
LL | const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });
| ^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0080`.