Skip to content

ICE when a static variable is declared of type for which the Drop trait is implemented #9243

Closed

Description

@MarkJr94

Minimal test case:

struct Test {
    mem: int,
}

pub static g_test: Test = Test {
    mem: 0,
};

impl Drop for Test {
    fn drop(&self) {
    }
}

fn main() {

}

Resulting Error after run with RUST_LOG=rustc=1 rustc test.rs:

task <unnamed> failed at 'assertion failed: `(left == right) && (right == left)` (left: `1u`, right: `2u`)', /build/buildd/rust-nightly-201309160805~3fbfab0~raring/src/librustc/middle/trans/adt.rs:545
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug
note: try running with RUST_LOG=rustc=1 to get further details and report the results to github.com/mozilla/rust/issues
task <unnamed> failed at 'explicit failure', /build/buildd/rust-nightly-201309160805~3fbfab0~raring/src/librustc/rustc.rs:377

Also, this:

struct Test;

pub static g_test: Test = Test;

impl Drop for Test {
    fn drop(&self) {
    }
}

fn main() {

}

compiles just fine, although I believe both cases should give a compilation error about having static variable implementing the Drop trait.

My rustc is acquired form the rust-nightly package in the Ubuntu repo.

Output of rustc --version:

rustc 0.8-pre
host: x86_64-unknown-linux-gnu

Activity

  1. alexcrichton commented on Sep 17, 2013

    @alexcrichton
    Member

    Nominating the decision of whether to allow this for the well-defined milestone

  2. luqmana commented on Sep 17, 2013

    @luqmana
    Contributor

    This also raises the question about whether to even allow putting types with destructors in a static? When would they run?

  3. catamorphism commented on Sep 19, 2013

    @catamorphism
    Contributor

    accepted for well-defined

  4. alchemy commented on Sep 25, 2013

    @alchemy

    Static items of a type implementing the Drop trait can have their distructors called at program termination, as in C++. Thus, both test cases above should compile without errors.

  5. brson commented on Sep 27, 2013

    @brson
    Contributor

    @alchemy it's not clear what the semantics of running destructors at program termination would be since at program termination there are no tasks in which to run the destructor.

    I'd say to just forbid types that implement Drop in statics, but that doesn't seem sufficient. What about mutable statics that contain objects (or closures) that might implement Drop? What about mutable types that contain allocations? Are we happy just letting them leak at termination?

  6. alexcrichton commented on Oct 16, 2013

    @alexcrichton
    Member

    We decided in the 10/15/13 meeting to disallow static items which have destructors (things like ~T or those with the Drop trait implemented).

  7. alchemy commented on Oct 17, 2013

    @alchemy

    Wise decision, i have been a little too hasty in my previous comment.
    As for the questions raised by @brson: as i see it, the only remaining issue is about managed allocations and potential memory leaks at program termination (as there is no task local gc). I'd say to leave things as they are. The compiler already forbids the @ operator in constant expressions, so the only way for allocations to end up in a (mutable) static item is through unsafe code, which seems reasonable.
    The alternative is to disallow static items containing managed pointers altogether.

  8. Aatch commented on Dec 13, 2013

    @Aatch
    Contributor

    With the new Pod kind incoming, would making the rule be that statics have to be Pod be acceptable? I think that it's technically a bit more limiting, but probably not enough to matter. It should also prevent any other future odd edge cases.

  9. nrc commented on Feb 14, 2014

    @nrc
    Member

    still ICEs, looking into this

  10. huonw commented on Feb 14, 2014

    @huonw
    Contributor

    @nick29581 this seems very likely to be covered by #11979.

  11. nrc commented on Feb 14, 2014

    @nrc
    Member

    Thanks huonw, I knew there was another issue similar to this because we discussed it at the meeting, but I couldn't find the number.

    @flaper87 - will this get fixed by #11979?

  12. nikomatsakis commented on Feb 14, 2014

    @nikomatsakis
    Contributor

    Closing as dup of #11979 -- under the rules we've discussed there, both of these programs would be in error (no static value may have a destructor).

  13. nikomatsakis commented on Feb 14, 2014

    @nikomatsakis
    Contributor

    Er, I see that #11979 is a PR, not an issue. Reopening :)

  14. 14 remaining items

  15. added a commit that references this issue on Mar 9, 2021
  16. added a commit that references this issue on Jul 28, 2022
  17. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priority

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions