From b23fd439642a0d161d4a76d373736e3cad182339 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Tue, 26 Feb 2019 10:11:41 +0100 Subject: [PATCH 1/6] Make `Unique::as_ptr`, `NonNull::dangling` and `NonNull::cast` const --- src/libcore/ptr.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 866c8d0896b3c..6186173812cb2 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2790,7 +2790,7 @@ impl Unique { } /// 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 } @@ -2903,7 +2903,7 @@ impl NonNull { /// some other means. #[stable(feature = "nonnull", since = "1.25.0")] #[inline] - pub fn dangling() -> Self { + pub const fn dangling() -> Self { unsafe { let ptr = mem::align_of::() as *mut T; NonNull::new_unchecked(ptr) @@ -2966,7 +2966,7 @@ impl NonNull { /// Cast to a pointer of another type #[stable(feature = "nonnull_cast", since = "1.27.0")] #[inline] - pub fn cast(self) -> NonNull { + pub const fn cast(self) -> NonNull { unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) } From 2d6261904663522ee1e9ec6c9f4079e4e56ba057 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Tue, 26 Feb 2019 11:58:30 +0100 Subject: [PATCH 2/6] Add `rustc_const_unstable` attribute --- src/libcore/ptr.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 6186173812cb2..d00665d778fb6 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2892,6 +2892,17 @@ impl !Send for NonNull { } impl !Sync for NonNull { } impl NonNull { + /// No docs here + #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] + #[cfg(stage0)] + pub fn dangling() -> Self { + unsafe { + let ptr = mem::align_of::() as *mut T; + NonNull::new_unchecked(ptr) + } + } + /// Creates a new `NonNull` that is dangling, but well-aligned. /// /// This is useful for initializing types which lazily allocate, like @@ -2903,6 +2914,8 @@ impl NonNull { /// some other means. #[stable(feature = "nonnull", since = "1.25.0")] #[inline] + #[cfg(not(stage0))] + #[rustc_const_unstable(feature = "const_ptr_nonnull")] pub const fn dangling() -> Self { unsafe { let ptr = mem::align_of::() as *mut T; @@ -2963,9 +2976,21 @@ impl NonNull { &mut *self.as_ptr() } + /// No docs here + #[stable(feature = "nonnull_cast", since = "1.27.0")] + #[inline] + #[cfg(stage0)] + pub fn cast(self) -> NonNull { + unsafe { + NonNull::new_unchecked(self.as_ptr() as *mut U) + } + } + /// Cast to a pointer of another type #[stable(feature = "nonnull_cast", since = "1.27.0")] #[inline] + #[cfg(not(stage0))] + #[rustc_const_unstable(feature = "const_ptr_nonnull")] pub const fn cast(self) -> NonNull { unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) From 4839d8c544232df8fa1cb55d306a936fc098ce9c Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Tue, 26 Feb 2019 11:58:44 +0100 Subject: [PATCH 3/6] Add tests --- src/test/run-pass/consts/const-ptr-nonnull.rs | 22 +++++++++++++++++ src/test/run-pass/consts/const-ptr-unique.rs | 15 ++++++++++++ src/test/ui/consts/const-ptr-nonnull.rs | 9 +++++++ src/test/ui/consts/const-ptr-nonnull.stderr | 24 +++++++++++++++++++ src/test/ui/consts/const-ptr-unique.rs | 9 +++++++ src/test/ui/consts/const-ptr-unique.stderr | 13 ++++++++++ 6 files changed, 92 insertions(+) create mode 100644 src/test/run-pass/consts/const-ptr-nonnull.rs create mode 100644 src/test/run-pass/consts/const-ptr-unique.rs create mode 100644 src/test/ui/consts/const-ptr-nonnull.rs create mode 100644 src/test/ui/consts/const-ptr-nonnull.stderr create mode 100644 src/test/ui/consts/const-ptr-unique.rs create mode 100644 src/test/ui/consts/const-ptr-unique.stderr diff --git a/src/test/run-pass/consts/const-ptr-nonnull.rs b/src/test/run-pass/consts/const-ptr-nonnull.rs new file mode 100644 index 0000000000000..62f10fb484177 --- /dev/null +++ b/src/test/run-pass/consts/const-ptr-nonnull.rs @@ -0,0 +1,22 @@ +// run-pass + +#![feature(const_ptr_nonnull)] + +use std::ptr::NonNull; + +const fn dangling() -> NonNull { + NonNull::dangling() +} + +const fn cast(non_null: NonNull) -> NonNull { + non_null.cast() +} + +pub fn main() { + assert_eq!(dangling(), NonNull::dangling()); + + let mut i: i32 = 10; + let non_null_t = NonNull::new(&mut i).unwrap(); + let non_null_u: NonNull = cast(non_null_t); + assert_eq!(non_null_t.as_ptr(), non_null_u.as_ptr() as *mut i32); +} diff --git a/src/test/run-pass/consts/const-ptr-unique.rs b/src/test/run-pass/consts/const-ptr-unique.rs new file mode 100644 index 0000000000000..4374bb81b4ee8 --- /dev/null +++ b/src/test/run-pass/consts/const-ptr-unique.rs @@ -0,0 +1,15 @@ +// run-pass + +#![feature(ptr_internals)] + +use std::ptr::Unique; + +const fn as_ptr(ptr: Unique) -> *mut T { + ptr.as_ptr() +} + +pub fn main() { + let mut i: i32 = 10; + let unique = Unique::new(&mut i).unwrap(); + assert_eq!(unique.as_ptr(), as_ptr(unique)); +} diff --git a/src/test/ui/consts/const-ptr-nonnull.rs b/src/test/ui/consts/const-ptr-nonnull.rs new file mode 100644 index 0000000000000..9502b619b6613 --- /dev/null +++ b/src/test/ui/consts/const-ptr-nonnull.rs @@ -0,0 +1,9 @@ +use std::ptr::NonNull; + +fn main() { + let x: &'static NonNull = &(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 = &(non_null.cast()); //~ ERROR borrowed value does not live long enough +} diff --git a/src/test/ui/consts/const-ptr-nonnull.stderr b/src/test/ui/consts/const-ptr-nonnull.stderr new file mode 100644 index 0000000000000..e670528210849 --- /dev/null +++ b/src/test/ui/consts/const-ptr-nonnull.stderr @@ -0,0 +1,24 @@ +error[E0597]: borrowed value does not live long enough + --> $DIR/const-ptr-nonnull.rs:4:37 + | +LL | let x: &'static NonNull = &(NonNull::dangling()); //~ ERROR borrowed value does not live long enough + | ^^^^^^^^^^^^^^^^^^^^^ 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:8:37 + | +LL | let x: &'static NonNull = &(non_null.cast()); //~ ERROR borrowed value does not live long enough + | ^^^^^^^^^^^^^^^^^ 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: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0597`. diff --git a/src/test/ui/consts/const-ptr-unique.rs b/src/test/ui/consts/const-ptr-unique.rs new file mode 100644 index 0000000000000..792d1854b7899 --- /dev/null +++ b/src/test/ui/consts/const-ptr-unique.rs @@ -0,0 +1,9 @@ +#![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 +} diff --git a/src/test/ui/consts/const-ptr-unique.stderr b/src/test/ui/consts/const-ptr-unique.stderr new file mode 100644 index 0000000000000..32cb862b3c3e8 --- /dev/null +++ b/src/test/ui/consts/const-ptr-unique.stderr @@ -0,0 +1,13 @@ +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()); //~ ERROR borrowed value does not live long enough + | ^^^^^^^^^^^^^^^^^ 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: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. From 278cb91346dcd043734ec250fbcc4b87ae93aef1 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Tue, 26 Feb 2019 13:03:40 +0100 Subject: [PATCH 4/6] Satisfy tidy --- src/libcore/ptr.rs | 2 +- src/test/run-pass/consts/const-ptr-nonnull.rs | 2 +- src/test/ui/consts/const-ptr-nonnull.rs | 8 +++++--- src/test/ui/consts/const-ptr-nonnull.stderr | 7 ++++--- src/test/ui/consts/const-ptr-unique.rs | 3 ++- src/test/ui/consts/const-ptr-unique.stderr | 3 ++- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index d00665d778fb6..d817dacadca9c 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2902,7 +2902,7 @@ impl NonNull { NonNull::new_unchecked(ptr) } } - + /// Creates a new `NonNull` that is dangling, but well-aligned. /// /// This is useful for initializing types which lazily allocate, like diff --git a/src/test/run-pass/consts/const-ptr-nonnull.rs b/src/test/run-pass/consts/const-ptr-nonnull.rs index 62f10fb484177..70d22c5e7147f 100644 --- a/src/test/run-pass/consts/const-ptr-nonnull.rs +++ b/src/test/run-pass/consts/const-ptr-nonnull.rs @@ -14,7 +14,7 @@ const fn cast(non_null: NonNull) -> NonNull { pub fn main() { assert_eq!(dangling(), NonNull::dangling()); - + let mut i: i32 = 10; let non_null_t = NonNull::new(&mut i).unwrap(); let non_null_u: NonNull = cast(non_null_t); diff --git a/src/test/ui/consts/const-ptr-nonnull.rs b/src/test/ui/consts/const-ptr-nonnull.rs index 9502b619b6613..54e743aa32e23 100644 --- a/src/test/ui/consts/const-ptr-nonnull.rs +++ b/src/test/ui/consts/const-ptr-nonnull.rs @@ -1,9 +1,11 @@ use std::ptr::NonNull; fn main() { - let x: &'static NonNull = &(NonNull::dangling()); //~ ERROR borrowed value does not live long enough - + let x: &'static NonNull = &(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 = &(non_null.cast()); //~ ERROR borrowed value does not live long enough + let x: &'static NonNull = &(non_null.cast()); + //~^ ERROR borrowed value does not live long enough } diff --git a/src/test/ui/consts/const-ptr-nonnull.stderr b/src/test/ui/consts/const-ptr-nonnull.stderr index e670528210849..a9476dda6d320 100644 --- a/src/test/ui/consts/const-ptr-nonnull.stderr +++ b/src/test/ui/consts/const-ptr-nonnull.stderr @@ -1,7 +1,7 @@ error[E0597]: borrowed value does not live long enough --> $DIR/const-ptr-nonnull.rs:4:37 | -LL | let x: &'static NonNull = &(NonNull::dangling()); //~ ERROR borrowed value does not live long enough +LL | let x: &'static NonNull = &(NonNull::dangling()); | ^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough ... LL | } @@ -10,10 +10,11 @@ LL | } = note: borrowed value must be valid for the static lifetime... error[E0597]: borrowed value does not live long enough - --> $DIR/const-ptr-nonnull.rs:8:37 + --> $DIR/const-ptr-nonnull.rs:9:37 | -LL | let x: &'static NonNull = &(non_null.cast()); //~ ERROR borrowed value does not live long enough +LL | let x: &'static NonNull = &(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 | diff --git a/src/test/ui/consts/const-ptr-unique.rs b/src/test/ui/consts/const-ptr-unique.rs index 792d1854b7899..be44a24181606 100644 --- a/src/test/ui/consts/const-ptr-unique.rs +++ b/src/test/ui/consts/const-ptr-unique.rs @@ -5,5 +5,6 @@ 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 + let x: &'static *mut u32 = &(unique.as_ptr()); + //~^ ERROR borrowed value does not live long enough } diff --git a/src/test/ui/consts/const-ptr-unique.stderr b/src/test/ui/consts/const-ptr-unique.stderr index 32cb862b3c3e8..141465bf184d0 100644 --- a/src/test/ui/consts/const-ptr-unique.stderr +++ b/src/test/ui/consts/const-ptr-unique.stderr @@ -1,8 +1,9 @@ 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()); //~ ERROR borrowed value does not live long enough +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 | From baea3c7ae9588adb6500acfbf418e58c8edb0a7e Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Tue, 26 Feb 2019 14:18:43 +0100 Subject: [PATCH 5/6] Use `cfg_attr` to prevent duplication --- src/libcore/ptr.rs | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index d817dacadca9c..3e1773ff9d25c 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2892,17 +2892,6 @@ impl !Send for NonNull { } impl !Sync for NonNull { } impl NonNull { - /// No docs here - #[stable(feature = "nonnull", since = "1.25.0")] - #[inline] - #[cfg(stage0)] - pub fn dangling() -> Self { - unsafe { - let ptr = mem::align_of::() as *mut T; - NonNull::new_unchecked(ptr) - } - } - /// Creates a new `NonNull` that is dangling, but well-aligned. /// /// This is useful for initializing types which lazily allocate, like @@ -2914,8 +2903,7 @@ impl NonNull { /// some other means. #[stable(feature = "nonnull", since = "1.25.0")] #[inline] - #[cfg(not(stage0))] - #[rustc_const_unstable(feature = "const_ptr_nonnull")] + #[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_nonnull"))] pub const fn dangling() -> Self { unsafe { let ptr = mem::align_of::() as *mut T; @@ -2976,21 +2964,10 @@ impl NonNull { &mut *self.as_ptr() } - /// No docs here - #[stable(feature = "nonnull_cast", since = "1.27.0")] - #[inline] - #[cfg(stage0)] - pub fn cast(self) -> NonNull { - unsafe { - NonNull::new_unchecked(self.as_ptr() as *mut U) - } - } - /// Cast to a pointer of another type #[stable(feature = "nonnull_cast", since = "1.27.0")] #[inline] - #[cfg(not(stage0))] - #[rustc_const_unstable(feature = "const_ptr_nonnull")] + #[cfg_attr(not(stage0), rustc_const_unstable(feature = "const_ptr_nonnull"))] pub const fn cast(self) -> NonNull { unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) From c5f3830e63e1a222f07234ac9ea012dee96220bb Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Tue, 26 Feb 2019 14:19:07 +0100 Subject: [PATCH 6/6] Use const eval instead of const fn --- src/test/run-pass/consts/const-ptr-nonnull.rs | 17 ++++++----------- src/test/run-pass/consts/const-ptr-unique.rs | 10 +++++----- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/test/run-pass/consts/const-ptr-nonnull.rs b/src/test/run-pass/consts/const-ptr-nonnull.rs index 70d22c5e7147f..91624e92fbe75 100644 --- a/src/test/run-pass/consts/const-ptr-nonnull.rs +++ b/src/test/run-pass/consts/const-ptr-nonnull.rs @@ -4,19 +4,14 @@ use std::ptr::NonNull; -const fn dangling() -> NonNull { - NonNull::dangling() -} +const DANGLING: NonNull = NonNull::dangling(); +const CASTED: NonNull = NonNull::cast(NonNull::::dangling()); -const fn cast(non_null: NonNull) -> NonNull { - non_null.cast() +fn ident(ident: T) -> T { + ident } pub fn main() { - assert_eq!(dangling(), NonNull::dangling()); - - let mut i: i32 = 10; - let non_null_t = NonNull::new(&mut i).unwrap(); - let non_null_u: NonNull = cast(non_null_t); - assert_eq!(non_null_t.as_ptr(), non_null_u.as_ptr() as *mut i32); + assert_eq!(DANGLING, ident(NonNull::dangling())); + assert_eq!(CASTED, ident(NonNull::dangling())); } diff --git a/src/test/run-pass/consts/const-ptr-unique.rs b/src/test/run-pass/consts/const-ptr-unique.rs index 4374bb81b4ee8..eb371ab184166 100644 --- a/src/test/run-pass/consts/const-ptr-unique.rs +++ b/src/test/run-pass/consts/const-ptr-unique.rs @@ -4,12 +4,12 @@ use std::ptr::Unique; -const fn as_ptr(ptr: Unique) -> *mut T { - ptr.as_ptr() +const PTR: *mut u32 = Unique::empty().as_ptr(); + +fn ident(ident: T) -> T { + ident } pub fn main() { - let mut i: i32 = 10; - let unique = Unique::new(&mut i).unwrap(); - assert_eq!(unique.as_ptr(), as_ptr(unique)); + assert_eq!(PTR, ident(Unique::::empty().as_ptr())); }