-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #74459 - canova:const-unreachable-unchecked, r=oli-obk
Make unreachable_unchecked a const fn This PR makes `std::hint::unreachable_unchecked` a const fn so we can use it inside a const function. r? @RalfJung Fixes #53188.
- Loading branch information
Showing
7 changed files
with
86 additions
and
1 deletion.
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() } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| 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`. |