Repository: rust
Issue: Accessing a TLS variable in its own constructor results in a stack overflow
Labels: C-enhancement,A-thread-locals,T-libs

Consider the following program ([Playground](https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=b906422f1f99a3a48f4f8935573efb80)):

```rust
struct Foobar {}

thread_local! {
    static TLS: Foobar = {
        let _ = TLS.try_with( |_| {} );
        Foobar {}
    };
}

fn main() {
    TLS.with( |_| {} );
}
```

Currently this results in a stack overflow:

```
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted (core dumped)
```

Stack trace from GDB:

```
#71449 0x0000555555559813 in foobar::TLS::__init::ha2c35ff7b5b5b1c5 () at src/main.rs:11
#71450 0x0000555555558543 in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::init::hdc42f4a92fb0acaa (self=0x5555555822f8, slot=0x7ffff7b71760) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:249
#71451 0x00005555555589bb in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::try_with::h53a54e92c6f85798 (self=0x5555555822f8, f=...) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:298
#71452 0x0000555555559813 in foobar::TLS::__init::ha2c35ff7b5b5b1c5 () at src/main.rs:11
#71453 0x0000555555558543 in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::init::hdc42f4a92fb0acaa (self=0x5555555822f8, slot=0x7ffff7b71760) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:249
#71454 0x00005555555589bb in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::try_with::h53a54e92c6f85798 (self=0x5555555822f8, f=...) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:298
#71455 0x0000555555559813 in foobar::TLS::__init::ha2c35ff7b5b5b1c5 () at src/main.rs:11
#71456 0x0000555555558543 in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::init::hdc42f4a92fb0acaa (self=0x5555555822f8, slot=0x7ffff7b71760) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:249
#71457 0x00005555555589bb in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::try_with::h53a54e92c6f85798 (self=0x5555555822f8, f=...) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:298
#71458 0x0000555555559813 in foobar::TLS::__init::ha2c35ff7b5b5b1c5 () at src/main.rs:11
#71459 0x0000555555558543 in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::init::hdc42f4a92fb0acaa (self=0x5555555822f8, slot=0x7ffff7b71760) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:249
#71460 0x000055555555879b in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::try_with::h1ec5fdb93a6312a2 (self=0x5555555822f8, f=...) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:298
#71461 0x0000555555558623 in _$LT$std..thread..local..LocalKey$LT$T$GT$$GT$::with::ha3235fa6d0c897c9 (self=0x5555555822f8, f=...) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/thread/local.rs:242
#71462 0x00005555555597f0 in foobar::main::hc8c8fd2120b717f4 () at src/main.rs:17
#71463 0x0000555555558500 in std::rt::lang_start::_$u7b$$u7b$closure$u7d$$u7d$::h1a821f2568568e6c () at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/rt.rs:64
#71464 0x00005555555615a3 in {{closure}} () at src/libstd/rt.rs:49
#71465 do_call<closure,i32> () at src/libstd/panicking.rs:297
#71466 0x00005555555635ba in __rust_maybe_catch_panic () at src/libpanic_unwind/lib.rs:92
#71467 0x0000555555562086 in try<i32,closure> () at src/libstd/panicking.rs:276
#71468 catch_unwind<closure,i32> () at src/libstd/panic.rs:388
#71469 lang_start_internal () at src/libstd/rt.rs:48
#71470 0x00005555555584d9 in std::rt::lang_start::h8641b0ab47425e46 (main=0x5555555597e0 <foobar::main::hc8c8fd2120b717f4>, argc=1, argv=0x7fffffffe188) at /rustc/b139669f374eb5024a50eb13f116ff763b1c5935/src/libstd/rt.rs:64
#71471 0x000055555555986a in main ()
#71472 0x00007ffff7b99ce3 in __libc_start_main () from /usr/lib/libc.so.6
#71473 0x000055555555817e in _start ()
```

I'd expect the `try_with` to simply return `Err` instead of going into an infinite loop.

Platform: x86_64-unknown-linux-gnu
rustc 1.37.0-nightly (2019-06-03 6ffb8f53ee1cb0903f9d)