-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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 #51990 - oli-obk:unstable_union, r=nikomatsakis
Place unions, pointer casts and pointer derefs behind extra feature gates To ensure we don't stabilize these things together with const fn stabilization (or any other stabilization) This PR moves union field accesses inside `const fn` behind a feature gate. It was possible without a feature gate before, but since `const fn` was behind a feature gate we can do this change. While "dereferencing raw pointers" and "casting raw pointers to usize" were hard errors before this PR, one could work around them by abusing unions: ```rust // deref union Foo<T> { x: &'static T, y: *const T, } const FOO: u32 = unsafe { *Foo { y: 42 as *const T }.x }; // as usize cast union Bar<T> { x: usize, y: *const T, } const BAR: usize = unsafe { Bar { y: &1u8 }.x }; ``` r? @eddyb cc @nikomatsakis
- Loading branch information
Showing
35 changed files
with
317 additions
and
200 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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
error[E0396]: raw pointers cannot be dereferenced in statics | ||
error[E0658]: dereferencing raw pointers in statics is unstable (see issue #51911) | ||
--> $DIR/const-deref-ptr.rs:14:29 | ||
| | ||
LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)}; //~ ERROR E0396 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ dereference of raw pointer in constant | ||
LL | static C: u64 = unsafe {*(0xdeadbeef as *const u64)}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0396`. | ||
For more information about this error, try `rustc --explain E0658`. |
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,27 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(const_raw_ptr_to_usize_cast, const_compare_raw_pointers, const_raw_ptr_deref)] | ||
|
||
fn main() {} | ||
|
||
// unconst and bad, will thus error in miri | ||
const X: bool = &1 as *const i32 == &2 as *const i32; //~ ERROR cannot be used | ||
// unconst and fine | ||
const X2: bool = 42 as *const i32 == 43 as *const i32; | ||
// unconst and fine | ||
const Y: usize = 42usize as *const i32 as usize + 1; | ||
// unconst and bad, will thus error in miri | ||
const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used | ||
// unconst and fine | ||
const Z: i32 = unsafe { *(&1 as *const i32) }; | ||
// unconst and bad, will thus error in miri | ||
const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used | ||
const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used |
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,36 @@ | ||
error: this constant cannot be used | ||
--> $DIR/const_raw_ptr_ops.rs:16:1 | ||
| | ||
LL | const X: bool = &1 as *const i32 == &2 as *const i32; //~ ERROR cannot be used | ||
| ^^^^^^^^^^^^^^^^------------------------------------^ | ||
| | | ||
| "pointer arithmetic or comparison" needs an rfc before being allowed inside constants | ||
| | ||
= note: #[deny(const_err)] on by default | ||
|
||
error: this constant cannot be used | ||
--> $DIR/const_raw_ptr_ops.rs:22:1 | ||
| | ||
LL | const Y2: usize = &1 as *const i32 as usize + 1; //~ ERROR cannot be used | ||
| ^^^^^^^^^^^^^^^^^^-----------------------------^ | ||
| | | ||
| "pointer arithmetic or comparison" needs an rfc before being allowed inside constants | ||
|
||
error: this constant cannot be used | ||
--> $DIR/const_raw_ptr_ops.rs:26:1 | ||
| | ||
LL | const Z2: i32 = unsafe { *(42 as *const i32) }; //~ ERROR cannot be used | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^^ | ||
| | | ||
| tried to access memory with alignment 2, but alignment 4 is required | ||
|
||
error: this constant cannot be used | ||
--> $DIR/const_raw_ptr_ops.rs:27:1 | ||
| | ||
LL | const Z3: i32 = unsafe { *(44 as *const i32) }; //~ ERROR cannot be used | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^-------------------^^^ | ||
| | | ||
| a memory access tried to interpret some bytes as a pointer | ||
|
||
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
Oops, something went wrong.