-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Place unions, pointer casts and pointer derefs behind extra feature gates #51990
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ef863b
Place unions, pointer casts and pointer derefs behind extra feature g…
oli-obk fee0753
Add feature gate checks
oli-obk c0aedc0
Use the correct feature gate name
oli-obk 07e2dd7
Don't accidentally promote union access in MIR
oli-obk 1fc7580
Rebase fallout: new tests need updated ui output
oli-obk 36907fc
Also put comparing raw pointers behind a feature gate
oli-obk aa0884e
Add feature gate test
oli-obk a091a65
Make sure the feature gate actually works and never allows promoting …
oli-obk 4b731a9
Fix tidy
oli-obk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change is wrong. All derefs are
NOT_CONST
, because we can never prove anything strong enough about the pointer/reference.this.add(Qualif::NOT_CONST);
was not withinif let ty::TyRawPtr(_) = base_ty.sty {
.The practical effects are that this compiles on nightly:
Thankfully, NLL is still unstable, so we didn't regress in any stable-visible way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How that, safe references should be fine?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh your comment in #54224 indicates this is about promotion. Then never mind, I anyway have no idea what happens there.^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type is safe, but the value isn't guaranteed to be valid / not point to a
static
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't parse the end of your sentence, do you mean "isn't guaranteed to point to a static" or "isn't guaranteed to NOT point to a static"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"isn't guaranteed to (be valid | not point to a
static
)"(as in, pointing to a
static
is as invalid as a random unsafe pointer, for promotion - since the value may be different at runtime)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, what? I would have thoughts statics are the least problematic to point at in a promoted (aka another static)...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe in a constant, yeah, but in the general case, a static could've changed at runtime.