Skip to content
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

Closed
arielb1 opened this issue Feb 26, 2015 · 12 comments
Closed

Allow constants with destructors (but don't have them run) #913

arielb1 opened this issue Feb 26, 2015 · 12 comments

Comments

@arielb1
Copy link
Contributor

arielb1 commented Feb 26, 2015

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.

@Kimundi
Copy link
Member

Kimundi commented Feb 26, 2015

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 statics contain types with destructors that will never be run is not memory unsafe, and should be allowed.

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
}

@Ericson2314
Copy link
Contributor

Why was this restriction added in the first place?

@thepowersgang
Copy link
Contributor

With the introduction of const fn and the conversion of forget to be safe, this is restriction is very likely not needed any more. What would be required to remove it? A new RFC, or just a compiler change?

@thepowersgang
Copy link
Contributor

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

@arielb1
Copy link
Contributor Author

arielb1 commented Jan 1, 2016

@thepowersgang

This restriction was never needed for safety - you could always call exit(3)/_exit(2) and not call any destructor.

@thepowersgang
Copy link
Contributor

@arielb1 ... good point... so why did this restriction exist? To avoid surprises?

@arielb1
Copy link
Contributor Author

arielb1 commented Jan 11, 2016

@thepowersgang

I don't have a ****** idea.

@bluss
Copy link
Member

bluss commented Mar 8, 2016

I think the restriction existed because rust was adamant to not support code outside of main ("global constructors and destructors").

@SimonSapin
Copy link
Contributor

This can be closed in favor of #1111 which has more details.

@nagisa
Copy link
Member

nagisa commented Mar 8, 2016

I think the restriction existed because rust was adamant to not support code outside of main ("global constructors and destructors").

We also were disillusioned about our capability to guarantee execution of destructors.

@Kimundi
Copy link
Member

Kimundi commented Nov 16, 2016

With #1440 merged this can probably be closed now.

@Kimundi
Copy link
Member

Kimundi commented Nov 19, 2016

Closing after having verified that everything works as expected.

@Kimundi Kimundi closed this as completed Nov 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants