Skip to content

Commit af6b494

Browse files
authored
Rollup merge of rust-lang#58750 - TimDiekmann:master, r=oli-obk
Make `Unique::as_ptr`, `NonNull::dangling` and `NonNull::cast` const
2 parents 17f2f1e + c5f3830 commit af6b494

File tree

7 files changed

+97
-3
lines changed

7 files changed

+97
-3
lines changed

src/libcore/ptr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,7 @@ impl<T: ?Sized> Unique<T> {
27902790
}
27912791

27922792
/// Acquires the underlying `*mut` pointer.
2793-
pub fn as_ptr(self) -> *mut T {
2793+
pub const fn as_ptr(self) -> *mut T {
27942794
self.pointer as *mut T
27952795
}
27962796

@@ -2903,7 +2903,8 @@ impl<T: Sized> NonNull<T> {
29032903
/// some other means.
29042904
#[stable(feature = "nonnull", since = "1.25.0")]
29052905
#[inline]
2906-
pub fn dangling() -> Self {
2906+
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_nonnull"))]
2907+
pub const fn dangling() -> Self {
29072908
unsafe {
29082909
let ptr = mem::align_of::<T>() as *mut T;
29092910
NonNull::new_unchecked(ptr)
@@ -2966,7 +2967,8 @@ impl<T: ?Sized> NonNull<T> {
29662967
/// Cast to a pointer of another type
29672968
#[stable(feature = "nonnull_cast", since = "1.27.0")]
29682969
#[inline]
2969-
pub fn cast<U>(self) -> NonNull<U> {
2970+
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_nonnull"))]
2971+
pub const fn cast<U>(self) -> NonNull<U> {
29702972
unsafe {
29712973
NonNull::new_unchecked(self.as_ptr() as *mut U)
29722974
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
3+
#![feature(const_ptr_nonnull)]
4+
5+
use std::ptr::NonNull;
6+
7+
const DANGLING: NonNull<u32> = NonNull::dangling();
8+
const CASTED: NonNull<u32> = NonNull::cast(NonNull::<i32>::dangling());
9+
10+
fn ident<T>(ident: T) -> T {
11+
ident
12+
}
13+
14+
pub fn main() {
15+
assert_eq!(DANGLING, ident(NonNull::dangling()));
16+
assert_eq!(CASTED, ident(NonNull::dangling()));
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
3+
#![feature(ptr_internals)]
4+
5+
use std::ptr::Unique;
6+
7+
const PTR: *mut u32 = Unique::empty().as_ptr();
8+
9+
fn ident<T>(ident: T) -> T {
10+
ident
11+
}
12+
13+
pub fn main() {
14+
assert_eq!(PTR, ident(Unique::<u32>::empty().as_ptr()));
15+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::ptr::NonNull;
2+
3+
fn main() {
4+
let x: &'static NonNull<u32> = &(NonNull::dangling());
5+
//~^ ERROR borrowed value does not live long enough
6+
7+
let mut i: i32 = 10;
8+
let non_null = NonNull::new(&mut i).unwrap();
9+
let x: &'static NonNull<u32> = &(non_null.cast());
10+
//~^ ERROR borrowed value does not live long enough
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/const-ptr-nonnull.rs:4:37
3+
|
4+
LL | let x: &'static NonNull<u32> = &(NonNull::dangling());
5+
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
...
7+
LL | }
8+
| - temporary value only lives until here
9+
|
10+
= note: borrowed value must be valid for the static lifetime...
11+
12+
error[E0597]: borrowed value does not live long enough
13+
--> $DIR/const-ptr-nonnull.rs:9:37
14+
|
15+
LL | let x: &'static NonNull<u32> = &(non_null.cast());
16+
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
17+
LL | //~^ ERROR borrowed value does not live long enough
18+
LL | }
19+
| - temporary value only lives until here
20+
|
21+
= note: borrowed value must be valid for the static lifetime...
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0597`.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(ptr_internals)]
2+
3+
use std::ptr::Unique;
4+
5+
fn main() {
6+
let mut i: u32 = 10;
7+
let unique = Unique::new(&mut i).unwrap();
8+
let x: &'static *mut u32 = &(unique.as_ptr());
9+
//~^ ERROR borrowed value does not live long enough
10+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/const-ptr-unique.rs:8:33
3+
|
4+
LL | let x: &'static *mut u32 = &(unique.as_ptr());
5+
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
LL | //~^ ERROR borrowed value does not live long enough
7+
LL | }
8+
| - temporary value only lives until here
9+
|
10+
= note: borrowed value must be valid for the static lifetime...
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)