-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow constants with destructors (but don't have them run) #913
Comments
I need statics with destructors for https://github.com/Kimundi/lazy-static.rs to not depend on dynamical allocations. Since a program can always be terminated at arbitrary points without having its destructors run, having As a proof, these two programs would be semantically equivalent: fn main() {
let FOO = /* moving type */;
...
abort();
// abort,
// process ends,
// FOOs destructor does not run
} static FOO = /* moving type */;
fn main() {
...
// end of main,
// process ends,
// FOOs destructor does not run
} |
Why was this restriction added in the first place? |
With the introduction of |
I've started drafting a RFC to lift this restriction. https://github.com/thepowersgang/rust-lang_rfcs/blob/drop-types-in-const/text/0000-drop-types-in-const.md |
This restriction was never needed for safety - you could always call |
@arielb1 ... good point... so why did this restriction exist? To avoid surprises? |
I don't have a ****** idea. |
I think the restriction existed because rust was adamant to not support code outside of main ("global constructors and destructors"). |
This can be closed in favor of #1111 which has more details. |
We also were disillusioned about our capability to guarantee execution of destructors. |
With #1440 merged this can probably be closed now. |
Closing after having verified that everything works as expected. |
In today's Rust, constants and statics can't contain destructors, and this prevents them from containing things like
None : Option<Box<_>>
. However, destructors are entirely memory safe, even if they don't get run - they can only leak resources, and this is relatively harmless, as you can't allocate resources in statics.There's the problem of destructors being used to flush things, but I don't think this will be particularly evil.
The text was updated successfully, but these errors were encountered: