-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Values that need to be dropped are promoted in const / static initializers #91009
Comments
This is very strange. There is only one place in promotion where we treat a
and the only difference is whether we check for the presence of the @oli-obk any idea what is going on? Could this be due to some difference in how MIR is generated for consts vs regular functions? |
I did remove the explicit difference where storage markers were often not generated at all. What we still have is the fact that We'll need to look at the actual MIR generated for these |
The reason I described the issue as applying to const / static initializers, is that there are no existing To be effective It was quite a bit of time since I looked at this, but as far as I remember the root problem was complete omission of check for live drops in MIR that construct call operands. |
MIR for this code, looking at the For the constant:
For main:
Looks... pretty similar? In fact a diff after removing comments says they are entirely identical. |
|
Okay, so it is not just a const-context bug then. That code in Looks like something is wrong with the handling of nested promotion, where we then fail to properly check for destructors. I never understood what happens with nested promotion... |
So this is the relevant code that bans rust/compiler/rustc_const_eval/src/transform/promote_consts.rs Lines 219 to 257 in dd12cd6
The problem with that approach is that the check is once per candidate. The safest thing would be to move that |
Good point. That kind of code leads to even simpler reproducer: struct NoisyDrop(&'static str);
impl Drop for NoisyDrop {
fn drop(&mut self) {
println!("{}", self.0);
}
}
fn main() {
let _: &'static _ = &&(NoisyDrop("drop!"), 0).1;
// NosiyDrop is not dropped ...
} |
…s, r=RalfJung Stop promoting all the things fixes rust-lang#91009 r? `@RalfJung`
For example, promotion succeeds in the example below, although compilation nevertheless fails due to checks for live drops:
With
const_precise_live_drops
, checking for live drops is delayed until after the promotion, so the following example compiles successfully:The check for live drops can also be passed by providing a const drop implementation. The following example compiles successfully as well:
@rustbot label +A-const-eval
The text was updated successfully, but these errors were encountered: