File tree Expand file tree Collapse file tree 9 files changed +136
-3
lines changed Expand file tree Collapse file tree 9 files changed +136
-3
lines changed Original file line number Diff line number Diff 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ error[E0716]: temporary value dropped while borrowed
2+ --> $DIR/const-ptr-nonnull.rs:4:37
3+ |
4+ LL | let x: &'static NonNull<u32> = &(NonNull::dangling());
5+ | --------------------- ^^^^^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
6+ | |
7+ | type annotation requires that borrow lasts for `'static`
8+ ...
9+ LL | }
10+ | - temporary value is freed at the end of this statement
11+
12+ error[E0716]: temporary value dropped while borrowed
13+ --> $DIR/const-ptr-nonnull.rs:9:37
14+ |
15+ LL | let x: &'static NonNull<u32> = &(non_null.cast());
16+ | --------------------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
17+ | |
18+ | type annotation requires that borrow lasts for `'static`
19+ LL | //~^ ERROR borrowed value does not live long enough
20+ LL | }
21+ | - temporary value is freed at the end of this statement
22+
23+ error: aborting due to 2 previous errors
24+
25+ For more information about this error, try `rustc --explain E0716`.
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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`.
Original file line number Diff line number Diff line change 1+ error[E0716]: temporary value dropped while borrowed
2+ --> $DIR/const-ptr-unique.rs:8:33
3+ |
4+ LL | let x: &'static *mut u32 = &(unique.as_ptr());
5+ | ----------------- ^^^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
6+ | |
7+ | type annotation requires that borrow lasts for `'static`
8+ LL | //~^ ERROR borrowed value does not live long enough
9+ LL | }
10+ | - temporary value is freed at the end of this statement
11+
12+ error: aborting due to previous error
13+
14+ For more information about this error, try `rustc --explain E0716`.
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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`.
You can’t perform that action at this time.
0 commit comments