-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Ensure that evaluating or validating a constant never reads from a static #67337
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
640e288
Panic on mutable allocs in constants
oli-obk ad6b9c7
Dynamically prevent constants from accessing statics
oli-obk 2022fac
Constants reading or referencing statics is illegal
oli-obk e56a861
Show `const_err` lints
oli-obk 89250b9
Update src/librustc_mir/interpret/intern.rs
oli-obk 75bdd95
Tidy
oli-obk 87fea04
Bless tests
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,12 @@ | ||
// compile-flags: -Zunleash-the-miri-inside-of-you | ||
|
||
#![allow(dead_code)] | ||
|
||
const TEST: &u8 = &MY_STATIC; | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ skipping const checks | ||
//~| it is undefined behavior to use this value | ||
|
||
static MY_STATIC: u8 = 4; | ||
|
||
fn main() { | ||
} |
This file contains hidden or 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,17 @@ | ||
warning: skipping const checks | ||
--> $DIR/const-points-to-static.rs:5:20 | ||
| | ||
LL | const TEST: &u8 = &MY_STATIC; | ||
| ^^^^^^^^^ | ||
|
||
error[E0080]: it is undefined behavior to use this value | ||
--> $DIR/const-points-to-static.rs:5:1 | ||
| | ||
LL | const TEST: &u8 = &MY_STATIC; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses static | ||
| | ||
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
This file contains hidden or 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 hidden or 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,6 +1,18 @@ | ||
warning: skipping const checks | ||
--> $DIR/const-prop-read-static-in-const.rs:6:18 | ||
--> $DIR/const-prop-read-static-in-const.rs:5:18 | ||
| | ||
LL | const TEST: u8 = MY_STATIC; | ||
| ^^^^^^^^^ | ||
|
||
error: any use of this value will cause an error | ||
--> $DIR/const-prop-read-static-in-const.rs:5:18 | ||
| | ||
LL | const TEST: u8 = MY_STATIC; | ||
| -----------------^^^^^^^^^- | ||
| | | ||
| constant accesses static | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error: aborting due to previous error | ||
|
38 changes: 38 additions & 0 deletions
38
src/test/ui/consts/miri_unleashed/const_refers_to_static.rs
This file contains hidden or 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,38 @@ | ||
// compile-flags: -Zunleash-the-miri-inside-of-you | ||
#![warn(const_err)] | ||
|
||
#![feature(const_raw_ptr_deref)] | ||
|
||
use std::sync::atomic::AtomicUsize; | ||
use std::sync::atomic::Ordering; | ||
|
||
const BOO: &usize = { //~ ERROR undefined behavior to use this value | ||
static FOO: AtomicUsize = AtomicUsize::new(0); | ||
unsafe { &*(&FOO as *const _ as *const usize) } | ||
//~^ WARN skipping const checks | ||
}; | ||
|
||
const FOO: usize = { | ||
static FOO: AtomicUsize = AtomicUsize::new(0); | ||
FOO.fetch_add(1, Ordering::Relaxed) //~ WARN any use of this value will cause an error | ||
//~^ WARN skipping const checks | ||
//~| WARN skipping const checks | ||
}; | ||
|
||
const BAR: usize = { | ||
static FOO: AtomicUsize = AtomicUsize::new(0); | ||
unsafe { *(&FOO as *const _ as *const usize) } //~ WARN any use of this value will cause an err | ||
//~^ WARN skipping const checks | ||
}; | ||
|
||
static mut MUTABLE: u32 = 0; | ||
const BAD: u32 = unsafe { MUTABLE }; //~ WARN any use of this value will cause an error | ||
//~^ WARN skipping const checks | ||
|
||
// ok some day perhaps | ||
const BOO_OK: &usize = { //~ ERROR it is undefined behavior to use this value | ||
static FOO: usize = 0; | ||
&FOO | ||
//~^ WARN skipping const checks | ||
}; | ||
fn main() {} |
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.
Uh oh!
There was an error while loading. Please reload this page.