Skip to content

Commit

Permalink
Rollup merge of rust-lang#58750 - TimDiekmann:master, r=oli-obk
Browse files Browse the repository at this point in the history
Make `Unique::as_ptr`, `NonNull::dangling` and `NonNull::cast` const
  • Loading branch information
Centril authored Mar 9, 2019
2 parents bdf6263 + 60a649e commit 70c1977
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2790,7 +2790,7 @@ impl<T: ?Sized> Unique<T> {
}

/// Acquires the underlying `*mut` pointer.
pub fn as_ptr(self) -> *mut T {
pub const fn as_ptr(self) -> *mut T {
self.pointer as *mut T
}

Expand Down Expand Up @@ -2903,7 +2903,8 @@ impl<T: Sized> NonNull<T> {
/// some other means.
#[stable(feature = "nonnull", since = "1.25.0")]
#[inline]
pub fn dangling() -> Self {
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_nonnull"))]
pub const fn dangling() -> Self {
unsafe {
let ptr = mem::align_of::<T>() as *mut T;
NonNull::new_unchecked(ptr)
Expand Down Expand Up @@ -2966,7 +2967,8 @@ impl<T: ?Sized> NonNull<T> {
/// Cast to a pointer of another type
#[stable(feature = "nonnull_cast", since = "1.27.0")]
#[inline]
pub fn cast<U>(self) -> NonNull<U> {
#[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_nonnull"))]
pub const fn cast<U>(self) -> NonNull<U> {
unsafe {
NonNull::new_unchecked(self.as_ptr() as *mut U)
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/run-pass/consts/const-ptr-nonnull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// run-pass

#![feature(const_ptr_nonnull)]

use std::ptr::NonNull;

const DANGLING: NonNull<u32> = NonNull::dangling();
const CASTED: NonNull<u32> = NonNull::cast(NonNull::<i32>::dangling());

fn ident<T>(ident: T) -> T {
ident
}

pub fn main() {
assert_eq!(DANGLING, ident(NonNull::dangling()));
assert_eq!(CASTED, ident(NonNull::dangling()));
}
15 changes: 15 additions & 0 deletions src/test/run-pass/consts/const-ptr-unique.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-pass

#![feature(ptr_internals)]

use std::ptr::Unique;

const PTR: *mut u32 = Unique::empty().as_ptr();

fn ident<T>(ident: T) -> T {
ident
}

pub fn main() {
assert_eq!(PTR, ident(Unique::<u32>::empty().as_ptr()));
}
25 changes: 25 additions & 0 deletions src/test/ui/consts/const-ptr-nonnull.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/const-ptr-nonnull.rs:4:37
|
LL | let x: &'static NonNull<u32> = &(NonNull::dangling());
| --------------------- ^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
...
LL | }
| - temporary value is freed at the end of this statement

error[E0716]: temporary value dropped while borrowed
--> $DIR/const-ptr-nonnull.rs:9:37
|
LL | let x: &'static NonNull<u32> = &(non_null.cast());
| --------------------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
LL | //~^ ERROR borrowed value does not live long enough
LL | }
| - temporary value is freed at the end of this statement

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0716`.
11 changes: 11 additions & 0 deletions src/test/ui/consts/const-ptr-nonnull.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::ptr::NonNull;

fn main() {
let x: &'static NonNull<u32> = &(NonNull::dangling());
//~^ ERROR borrowed value does not live long enough

let mut i: i32 = 10;
let non_null = NonNull::new(&mut i).unwrap();
let x: &'static NonNull<u32> = &(non_null.cast());
//~^ ERROR borrowed value does not live long enough
}
25 changes: 25 additions & 0 deletions src/test/ui/consts/const-ptr-nonnull.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-ptr-nonnull.rs:4:37
|
LL | let x: &'static NonNull<u32> = &(NonNull::dangling());
| ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

error[E0597]: borrowed value does not live long enough
--> $DIR/const-ptr-nonnull.rs:9:37
|
LL | let x: &'static NonNull<u32> = &(non_null.cast());
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | //~^ ERROR borrowed value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0597`.
14 changes: 14 additions & 0 deletions src/test/ui/consts/const-ptr-unique.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/const-ptr-unique.rs:8:33
|
LL | let x: &'static *mut u32 = &(unique.as_ptr());
| ----------------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
| |
| type annotation requires that borrow lasts for `'static`
LL | //~^ ERROR borrowed value does not live long enough
LL | }
| - temporary value is freed at the end of this statement

error: aborting due to previous error

For more information about this error, try `rustc --explain E0716`.
10 changes: 10 additions & 0 deletions src/test/ui/consts/const-ptr-unique.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(ptr_internals)]

use std::ptr::Unique;

fn main() {
let mut i: u32 = 10;
let unique = Unique::new(&mut i).unwrap();
let x: &'static *mut u32 = &(unique.as_ptr());
//~^ ERROR borrowed value does not live long enough
}
14 changes: 14 additions & 0 deletions src/test/ui/consts/const-ptr-unique.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/const-ptr-unique.rs:8:33
|
LL | let x: &'static *mut u32 = &(unique.as_ptr());
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
LL | //~^ ERROR borrowed value does not live long enough
LL | }
| - temporary value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

error: aborting due to previous error

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

0 comments on commit 70c1977

Please sign in to comment.