-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Make unreachable_unchecked a const fn #74459
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4fefa2c
Make unreachable_unchecked a const fn
canova 2f28d59
Add a passing test for const unsafe_unreachable
canova c45e9c8
Add a test for const unsafe_unreachable that triggers UB
canova 6cd164f
Update UB test to fail during build with contant errors
canova 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// run-pass | ||
|
||
#![feature(const_fn)] | ||
#![feature(const_unreachable_unchecked)] | ||
|
||
const unsafe fn foo(x: bool) -> bool { | ||
match x { | ||
true => true, | ||
false => std::hint::unreachable_unchecked(), | ||
} | ||
} | ||
|
||
const BAR: bool = unsafe { foo(true) }; | ||
|
||
fn main() { | ||
assert_eq!(BAR, true); | ||
} |
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,20 @@ | ||
// build-fail | ||
|
||
#![feature(const_fn)] | ||
#![feature(const_unreachable_unchecked)] | ||
|
||
const unsafe fn foo(x: bool) -> bool { | ||
match x { | ||
true => true, | ||
false => std::hint::unreachable_unchecked(), | ||
} | ||
} | ||
|
||
#[warn(const_err)] | ||
const BAR: bool = unsafe { foo(false) }; | ||
|
||
fn main() { | ||
assert_eq!(BAR, true); | ||
//~^ ERROR E0080 | ||
//~| ERROR erroneous constant | ||
} |
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,44 @@ | ||
warning: any use of this value will cause an error | ||
--> $SRC_DIR/libcore/hint.rs:LL:COL | ||
| | ||
LL | unsafe { intrinsics::unreachable() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error is showing the internal code instead of the |
||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| entering unreachable code | ||
| inside `std::hint::unreachable_unchecked` at $SRC_DIR/libcore/hint.rs:LL:COL | ||
| inside `foo` at $DIR/const_unsafe_unreachable_ub.rs:9:18 | ||
| inside `BAR` at $DIR/const_unsafe_unreachable_ub.rs:14:28 | ||
| | ||
::: $DIR/const_unsafe_unreachable_ub.rs:14:1 | ||
| | ||
LL | const BAR: bool = unsafe { foo(false) }; | ||
| ---------------------------------------- | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/const_unsafe_unreachable_ub.rs:13:8 | ||
| | ||
LL | #[warn(const_err)] | ||
| ^^^^^^^^^ | ||
|
||
error[E0080]: evaluation of constant expression failed | ||
--> $DIR/const_unsafe_unreachable_ub.rs:17:3 | ||
| | ||
LL | assert_eq!(BAR, true); | ||
| ^^^^^^^^^^^---^^^^^^^^ | ||
| | | ||
| referenced constant has errors | ||
| | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: erroneous constant used | ||
--> $DIR/const_unsafe_unreachable_ub.rs:17:3 | ||
| | ||
LL | assert_eq!(BAR, true); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 2 previous errors; 1 warning emitted | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
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.
If you make this test
#[warn(const_err)]
you'll get an additional error on theBAR
use site, which you can then mark with//~ ERROR erroneous constant
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 tried to add#[warn(const_err)]
on top of the BAR constant but I still don't get an error in the use site.Edit: Okay, I figured out. I was missing the
build-fail
annotation actually. Updating the codeThere 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.
@oli-obk Thanks for the recommendation! I figured out and updated the code. Could you take a look again?
(Added it as a new commit to make it easier to see new changes, but I can squash with the previous commit later if you prefer it that 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.
yea, that build-fail thing is really problematic around const prop and similar. I gotta figure something out for that.
The PR lgtm now, thanks!