Skip to content

Write to #[thread_local] is not respected #54901

Description

@dtolnay
#![feature(thread_local)]

#[thread_local]
static S: &str = "before";

fn set_s() {
    S = "after";
}

fn main() {
    println!("{}", S);
    set_s();
    println!("{}", S);
}

On rustc 1.31.0-nightly (b2d6ea9 2018-10-07) and x86_64-unknown-linux-gnu, we see the following surprising behavior:

$ cargo run  # as expected
before
after

$ cargo run --release
before
before

In release mode it seems the write has not happened in the place that it should.

Mentioning thread_local tracking issue: rust-lang/rust#29594.

Activity

  1. dtolnay commented on Oct 8, 2018

    @dtolnay
    MemberAuthor

    Ah, apparently this is missing a mut. With mut the behavior is correct. We need to make sure mutating a thread_local static that does not have mut does not compile.

    #![feature(thread_local)]
    
    #[thread_local]
    static mut S: &str = "before";
    
    fn set_s() {
        unsafe { S = "after" };
    }
    
    fn main() {
        println!("{}", unsafe { S });
        set_s();
        println!("{}", unsafe { S });
    }
    $ cargo run --release
    before
    after
  2. eddyb commented on Nov 4, 2018

    @eddyb
    Contributor

    cc @pnkfelix @nikomatsakis Did #55150 fix this by any chance?

  3. pnkfelix commented on Nov 5, 2018

    @pnkfelix
    Contributor

    It looks like this is fixed on nightly; I'm guessing #55150 is the reason. We probably should add some regression tests for the cases listed here.

  4. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Nov 5, 2018
  5. added 6 commits that reference this issue on Jan 15, 2019
    259b0bd
    c77dfc5
    612aa1f
    adf7163
    2b8d02f
    a1ef22a
  6. added a commit that references this issue on Jan 18, 2019
    23ad496
  7. added a commit that references this issue on Jan 18, 2019
    b9cb5db
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

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.fixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions