diff options
| author | Ralf Jung <post@ralfj.de> | 2021-04-25 16:32:05 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2021-04-25 16:34:38 +0200 |
| commit | b1ad1fbc03383e6c7a320b7a18a1fd02ebc68e3b (patch) | |
| tree | fd8cdd7762fbe8c1a57019ddd20b4840dcd80535 /src/test | |
| parent | 43126f357339bd35e6cdc28201516422f6a2dac1 (diff) | |
| download | rust-b1ad1fbc03383e6c7a320b7a18a1fd02ebc68e3b.tar.gz rust-b1ad1fbc03383e6c7a320b7a18a1fd02ebc68e3b.zip | |
make sure raw ptr casts in 'const' context are unsafe
Diffstat (limited to 'src/test')
4 files changed, 76 insertions, 25 deletions
diff --git a/src/test/ui/cast/cast-ptr-to-int-const.rs b/src/test/ui/cast/cast-ptr-to-int-const.rs index ac153cb5742..aed099a53ea 100644 --- a/src/test/ui/cast/cast-ptr-to-int-const.rs +++ b/src/test/ui/cast/cast-ptr-to-int-const.rs @@ -1,11 +1,25 @@ // gate-test-const_raw_ptr_to_usize_cast +// revisions: with_feature without_feature + +#![cfg_attr(with_feature, feature(const_raw_ptr_to_usize_cast))] fn main() { - const X: u32 = unsafe { - main as u32 //~ ERROR casting pointers to integers in constants is unstable + const X: usize = unsafe { + main as usize //[without_feature]~ ERROR casting pointers to integers in constants is unstable }; const Y: u32 = 0; - const Z: u32 = unsafe { - &Y as *const u32 as u32 //~ ERROR is unstable + const Z: usize = unsafe { + &Y as *const u32 as usize //[without_feature]~ ERROR is unstable + }; + // Cast in `const` without `unsafe` block + const SAFE: usize = { + &Y as *const u32 as usize //[without_feature]~ ERROR is unstable + //[with_feature]~^ ERROR cast of pointer to int is unsafe and requires unsafe }; } + +// Cast in `const fn` without `unsafe` block +const fn test() -> usize { + &0 as *const i32 as usize //[without_feature]~ ERROR is unstable + //[with_feature]~^ ERROR cast of pointer to int is unsafe and requires unsafe +} diff --git a/src/test/ui/cast/cast-ptr-to-int-const.stderr b/src/test/ui/cast/cast-ptr-to-int-const.stderr deleted file mode 100644 index f523b14a98c..00000000000 --- a/src/test/ui/cast/cast-ptr-to-int-const.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error[E0658]: casting pointers to integers in constants is unstable - --> $DIR/cast-ptr-to-int-const.rs:5:9 - | -LL | main as u32 - | ^^^^^^^^^^^ - | - = note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information - = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable - -error[E0658]: casting pointers to integers in constants is unstable - --> $DIR/cast-ptr-to-int-const.rs:9:9 - | -LL | &Y as *const u32 as u32 - | ^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information - = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/cast/cast-ptr-to-int-const.with_feature.stderr b/src/test/ui/cast/cast-ptr-to-int-const.with_feature.stderr new file mode 100644 index 00000000000..8282bc3db05 --- /dev/null +++ b/src/test/ui/cast/cast-ptr-to-int-const.with_feature.stderr @@ -0,0 +1,19 @@ +error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block + --> $DIR/cast-ptr-to-int-const.rs:16:9 + | +LL | &Y as *const u32 as usize + | ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int + | + = note: casting pointers to integers in constants + +error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block + --> $DIR/cast-ptr-to-int-const.rs:23:5 + | +LL | &0 as *const i32 as usize + | ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int + | + = note: casting pointers to integers in constants + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0133`. diff --git a/src/test/ui/cast/cast-ptr-to-int-const.without_feature.stderr b/src/test/ui/cast/cast-ptr-to-int-const.without_feature.stderr new file mode 100644 index 00000000000..c87fa1a14a4 --- /dev/null +++ b/src/test/ui/cast/cast-ptr-to-int-const.without_feature.stderr @@ -0,0 +1,39 @@ +error[E0658]: casting pointers to integers in constants is unstable + --> $DIR/cast-ptr-to-int-const.rs:8:9 + | +LL | main as usize + | ^^^^^^^^^^^^^ + | + = note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information + = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable + +error[E0658]: casting pointers to integers in constants is unstable + --> $DIR/cast-ptr-to-int-const.rs:12:9 + | +LL | &Y as *const u32 as usize + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information + = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable + +error[E0658]: casting pointers to integers in constants is unstable + --> $DIR/cast-ptr-to-int-const.rs:16:9 + | +LL | &Y as *const u32 as usize + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information + = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable + +error[E0658]: casting pointers to integers in constant functions is unstable + --> $DIR/cast-ptr-to-int-const.rs:23:5 + | +LL | &0 as *const i32 as usize + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #51910 <https://github.com/rust-lang/rust/issues/51910> for more information + = help: add `#![feature(const_raw_ptr_to_usize_cast)]` to the crate attributes to enable + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0658`. |
