Description
From nightly-2020-02-28 (nightly-2020-02-27 was fine), I get link errors if I compile a cdylib with a thread local that isn't used, then compile again after referencing the thread local.
To reproduce, create a.rs:
pub struct Foo {}
impl Drop for Foo {
fn drop(&mut self) {
println!("Dropping Foo");
}
}
#[no_mangle]
pub extern "C" fn run() {
thread_local! { pub static FOO : Foo = Foo { } ; }
//FOO.with(|_f| ())
}
Then run:
rustc a.rs --crate-type cdylib --emit=dep-info,link -C opt-level=2 --out-dir target -C incremental=target/incremental
Uncomment the commented line and rerun the above command.
This produces the following link error:
1k5ednsjh2ku99ix:(.text._ZN3std6thread5local4fast12Key$LT$T$GT$14try_initialize17h998faaa5ce3b2835E.llvm.11804660923702021477+0x3a): undefined reference to `anon.043848776374366c7b657cba74aa2e83.1.llvm.9876575881142733961'
/usr/bin/ld: target/lib.1k5ednsjh2ku99ix.rcgu.o: relocation R_X86_64_PC32 against undefined hidden symbol `anon.043848776374366c7b657cba74aa2e83.1.llvm.9876575881142733961' can not be used when making a shared object
It's possible however that the underlying bug is actually older though, since at opt-level=1, I get link errors from 1.32.0 onwards (1.31.0 was fine). In that case, the Drop implementation isn't needed.
pub struct Foo {}
#[no_mangle]
pub extern "C" fn run() {
thread_local! { pub static FOO : Foo = Foo { } ; }
//FOO.with(|_f| ())
}
rustc a.rs --crate-type cdylib --emit=dep-info,link -C opt-level=1 --out-dir target -C incremental=target/incremental
Linking fails with: undefined reference to 'core::ptr::drop_in_place'
The exact symbol that's reported varies.
Versions:
rustc 1.43.0-nightly (2890b37 2020-03-06)
cc (Debian 9.2.1-16) 9.2.1 20191030
GNU ld (GNU Binutils for Debian) 2.33.1