File tree 7 files changed +97
-3
lines changed
7 files changed +97
-3
lines changed Original file line number Diff line number Diff line change @@ -2790,7 +2790,7 @@ impl<T: ?Sized> Unique<T> {
2790
2790
}
2791
2791
2792
2792
/// Acquires the underlying `*mut` pointer.
2793
- pub fn as_ptr ( self ) -> * mut T {
2793
+ pub const fn as_ptr ( self ) -> * mut T {
2794
2794
self . pointer as * mut T
2795
2795
}
2796
2796
@@ -2903,7 +2903,8 @@ impl<T: Sized> NonNull<T> {
2903
2903
/// some other means.
2904
2904
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
2905
2905
#[ inline]
2906
- pub fn dangling ( ) -> Self {
2906
+ #[ cfg_attr( not( stage0) , rustc_const_unstable( feature = "const_ptr_nonnull" ) ) ]
2907
+ pub const fn dangling ( ) -> Self {
2907
2908
unsafe {
2908
2909
let ptr = mem:: align_of :: < T > ( ) as * mut T ;
2909
2910
NonNull :: new_unchecked ( ptr)
@@ -2966,7 +2967,8 @@ impl<T: ?Sized> NonNull<T> {
2966
2967
/// Cast to a pointer of another type
2967
2968
#[ stable( feature = "nonnull_cast" , since = "1.27.0" ) ]
2968
2969
#[ 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 > {
2970
2972
unsafe {
2971
2973
NonNull :: new_unchecked ( self . as_ptr ( ) as * mut U )
2972
2974
}
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
+ 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
+ #![ 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