Skip to content

Commit 4839d8c

Browse files
committed
Add tests
1 parent 2d62619 commit 4839d8c

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run-pass
2+
3+
#![feature(const_ptr_nonnull)]
4+
5+
use std::ptr::NonNull;
6+
7+
const fn dangling() -> NonNull<u32> {
8+
NonNull::dangling()
9+
}
10+
11+
const fn cast<T, U>(non_null: NonNull<T>) -> NonNull<U> {
12+
non_null.cast()
13+
}
14+
15+
pub fn main() {
16+
assert_eq!(dangling(), NonNull::dangling());
17+
18+
let mut i: i32 = 10;
19+
let non_null_t = NonNull::new(&mut i).unwrap();
20+
let non_null_u: NonNull<u32> = cast(non_null_t);
21+
assert_eq!(non_null_t.as_ptr(), non_null_u.as_ptr() as *mut i32);
22+
}
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 fn as_ptr<T>(ptr: Unique<T>) -> *mut T {
8+
ptr.as_ptr()
9+
}
10+
11+
pub fn main() {
12+
let mut i: i32 = 10;
13+
let unique = Unique::new(&mut i).unwrap();
14+
assert_eq!(unique.as_ptr(), as_ptr(unique));
15+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::ptr::NonNull;
2+
3+
fn main() {
4+
let x: &'static NonNull<u32> = &(NonNull::dangling()); //~ ERROR borrowed value does not live long enough
5+
6+
let mut i: i32 = 10;
7+
let non_null = NonNull::new(&mut i).unwrap();
8+
let x: &'static NonNull<u32> = &(non_null.cast()); //~ ERROR borrowed value does not live long enough
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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()); //~ ERROR borrowed value does not live long enough
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:8:37
14+
|
15+
LL | let x: &'static NonNull<u32> = &(non_null.cast()); //~ ERROR borrowed value does not live long enough
16+
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
17+
LL | }
18+
| - temporary value only lives until here
19+
|
20+
= note: borrowed value must be valid for the static lifetime...
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0597`.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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()); //~ ERROR borrowed value does not live long enough
9+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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()); //~ ERROR borrowed value does not live long enough
5+
| ^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
LL | }
7+
| - temporary value only lives until here
8+
|
9+
= note: borrowed value must be valid for the static lifetime...
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)