-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Program that uses the const VOID: ! =panic!()
associated constant compiles and runs into illegal intruction.
#66975
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
cc @rust-lang/wg-const-eval |
Ok, there's three questionable things going on.
#![feature(const_panic)]
#![allow(const_err)]
const VOID: ! = panic!();
fn main() {
let _ = VOID;
}
#![allow(const_err)]
const VOID: u32 = 0 - 1;
fn main() {
let x = VOID; // still an error
} |
Ok... so I did a full MIR dump of the example code and even the very first MIR directly after mir building does not contain the assignment (although it contains the variable of |
We throw the constant on the floor: rust/src/librustc_mir/build/expr/into.rs Line 68 in e862c01
This needs to be as_temp to ensure that anything ends up in the MIR.
|
Wouldn't |
|
FWIW I don't think this is unsound; this just runs into the safety net that we have when embedding consts/promoteds with errors into the code -- we emit a trap. So this is certainly wrong codegen but I don't think there is any UB. |
It is unsound, because we don't run into the promoted safety net. The constant never even shows up in MIR, instead all it is is a basic block with an unreachable terminator. @matthewjasper my worry was not about this specific code miscompiling, but other code miscompiling. But you're right, the call you found only is run for never types. Can you open a PR with the change and a mir opt test showing that it doesn't get optimized away? This may additionally require #67134 |
Oh. Yeah okay that's pretty bad indeed. |
Running this with
|
Also if you remove the #![feature(const_panic)]
struct PrintName;
impl PrintName {
const VOID: ! = panic!();
}
fn main() {
PrintName::VOID;
}
|
This unsound code compiles in the playground (on Rust nightly 2019-12-01):
And runs into an illegal instruction:
I expected this to fail to compile because panicking in constants should cause compilation errors.
The text was updated successfully, but these errors were encountered: