-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #74940 - oli-obk:const_is_null, r=RalfJung
Make `<*const T>::is_null` const fn r? @RalfJung cc @rust-lang/wg-const-eval tracking issue: #74939
- Loading branch information
Showing
6 changed files
with
204 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// compile-flags: --crate-type=lib | ||
// normalize-stderr-32bit: "offset 8" -> "offset $$TWO_WORDS" | ||
// normalize-stderr-64bit: "offset 16" -> "offset $$TWO_WORDS" | ||
// normalize-stderr-32bit: "size 4" -> "size $$WORD" | ||
// normalize-stderr-64bit: "size 8" -> "size $$WORD" | ||
|
||
#![feature( | ||
const_panic, | ||
core_intrinsics, | ||
const_raw_ptr_comparison, | ||
const_ptr_offset, | ||
const_raw_ptr_deref, | ||
raw_ref_macros | ||
)] | ||
|
||
const FOO: &usize = &42; | ||
|
||
macro_rules! check { | ||
(eq, $a:expr, $b:expr) => { | ||
pub const _: () = | ||
assert!(std::intrinsics::ptr_guaranteed_eq($a as *const u8, $b as *const u8)); | ||
}; | ||
(ne, $a:expr, $b:expr) => { | ||
pub const _: () = | ||
assert!(std::intrinsics::ptr_guaranteed_ne($a as *const u8, $b as *const u8)); | ||
}; | ||
(!eq, $a:expr, $b:expr) => { | ||
pub const _: () = | ||
assert!(!std::intrinsics::ptr_guaranteed_eq($a as *const u8, $b as *const u8)); | ||
}; | ||
(!ne, $a:expr, $b:expr) => { | ||
pub const _: () = | ||
assert!(!std::intrinsics::ptr_guaranteed_ne($a as *const u8, $b as *const u8)); | ||
}; | ||
} | ||
|
||
check!(eq, 0, 0); | ||
check!(ne, 0, 1); | ||
check!(!eq, 0, 1); | ||
check!(!ne, 0, 0); | ||
check!(ne, FOO as *const _, 0); | ||
check!(!eq, FOO as *const _, 0); | ||
// We want pointers to be equal to themselves, but aren't checking this yet because | ||
// there are some open questions (e.g. whether function pointers to the same function | ||
// compare equal, they don't necessarily at runtime). | ||
// The case tested here should work eventually, but does not work yet. | ||
check!(!eq, FOO as *const _, FOO as *const _); | ||
check!(ne, unsafe { (FOO as *const usize).offset(1) }, 0); | ||
check!(!eq, unsafe { (FOO as *const usize).offset(1) }, 0); | ||
|
||
check!(ne, unsafe { (FOO as *const usize as *const u8).offset(3) }, 0); | ||
check!(!eq, unsafe { (FOO as *const usize as *const u8).offset(3) }, 0); | ||
|
||
/////////////////////////////////////////////////////////////////////////////// | ||
// If any of the below start compiling, make sure to add a `check` test for it. | ||
// These invocations exist as canaries so we don't forget to check that the | ||
// behaviour of `guaranteed_eq` and `guaranteed_ne` is still correct. | ||
// All of these try to obtain an out of bounds pointer in some manner. If we | ||
// can create out of bounds pointers, we can offset a pointer far enough that | ||
// at runtime it would be zero and at compile-time it would not be zero. | ||
|
||
const _: *const usize = unsafe { (FOO as *const usize).offset(2) }; | ||
//~^ NOTE | ||
|
||
const _: *const u8 = | ||
//~^ NOTE | ||
unsafe { std::ptr::raw_const!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; | ||
//~^ ERROR any use of this value will cause an error | ||
|
||
const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 }; | ||
//~^ ERROR any use of this value will cause an error | ||
//~| NOTE "pointer-to-integer cast" needs an rfc | ||
//~| NOTE | ||
|
||
const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 }; | ||
//~^ ERROR any use of this value will cause an error | ||
//~| NOTE "pointer-to-integer cast" needs an rfc | ||
//~| NOTE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error: any use of this value will cause an error | ||
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | ||
| | ||
LL | unsafe { intrinsics::offset(self, count) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| inbounds test failed: pointer must be in-bounds at offset $TWO_WORDS, but is outside bounds of alloc2 which has size $WORD | ||
| inside `std::ptr::const_ptr::<impl *const usize>::offset` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | ||
| inside `_` at $DIR/ptr_comparisons.rs:62:34 | ||
| | ||
::: $DIR/ptr_comparisons.rs:62:1 | ||
| | ||
LL | const _: *const usize = unsafe { (FOO as *const usize).offset(2) }; | ||
| ------------------------------------------------------------------- | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: any use of this value will cause an error | ||
--> $DIR/ptr_comparisons.rs:67:14 | ||
| | ||
LL | / const _: *const u8 = | ||
LL | | | ||
LL | | unsafe { std::ptr::raw_const!((*(FOO as *const usize as *const [u8; 1000]))[999]) }; | ||
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__- | ||
| | | ||
| memory access failed: pointer must be in-bounds at offset 1000, but is outside bounds of alloc2 which has size $WORD | ||
| | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: any use of this value will cause an error | ||
--> $DIR/ptr_comparisons.rs:70:27 | ||
| | ||
LL | const _: usize = unsafe { std::mem::transmute::<*const usize, usize>(FOO) + 4 }; | ||
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- | ||
| | | ||
| "pointer-to-integer cast" needs an rfc before being allowed inside constants | ||
|
||
error: any use of this value will cause an error | ||
--> $DIR/ptr_comparisons.rs:75:27 | ||
| | ||
LL | const _: usize = unsafe { *std::mem::transmute::<&&usize, &usize>(&FOO) + 4 }; | ||
| --------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- | ||
| | | ||
| "pointer-to-integer cast" needs an rfc before being allowed inside constants | ||
|
||
error: aborting due to 4 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// compile-flags: --crate-type=lib | ||
// check-pass | ||
|
||
#![feature(const_ptr_is_null, const_panic)] | ||
|
||
const FOO: &usize = &42; | ||
|
||
pub const _: () = assert!(!(FOO as *const usize).is_null()); | ||
|
||
pub const _: () = assert!(!(42 as *const usize).is_null()); | ||
|
||
pub const _: () = assert!((0 as *const usize).is_null()); | ||
|
||
pub const _: () = assert!(std::ptr::null::<usize>().is_null()); | ||
|
||
pub const _: () = assert!(!("foo" as *const str).is_null()); |