diff --git a/example/mini_core.rs b/example/mini_core.rs index 78b493e0515..a271cb6e62e 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -529,3 +529,5 @@ pub macro line() { /* compiler built-in */ } #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] pub macro cfg() { /* compiler built-in */ } + +pub static A_STATIC: u8 = 42; diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 56a799be2fb..955530c7f91 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -261,4 +261,7 @@ fn main() { let f2 = -1000.0; assert_eq!(f2 as i8, -128); assert_eq!(f2 as u8, 0); + + static ANOTHER_STATIC: &u8 = &A_STATIC; + assert_eq!(*ANOTHER_STATIC, 42); } diff --git a/src/constant.rs b/src/constant.rs index 465ca06a2b6..f035d8b9e06 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -297,6 +297,10 @@ fn define_all_allocs( read_target_uint(endianness, bytes).unwrap() }; + // Don't inline `reloc_target_alloc` into the match. That would cause `tcx.alloc_map` + // to be locked for the duration of the match. `data_id_for_static` however may try + // to lock `tcx.alloc_map` itself while calculating the layout of the target static. + // This would cause a panic in single threaded rustc and a deadlock for parallel rustc. let reloc_target_alloc = tcx.alloc_map.lock().get(reloc).unwrap(); let data_id = match reloc_target_alloc { GlobalAlloc::Function(instance) => { @@ -311,7 +315,9 @@ fn define_all_allocs( data_id_for_alloc_id(module, reloc, alloc.align) } GlobalAlloc::Static(def_id) => { - cx.todo.insert(TodoItem::Static(def_id)); + // Don't push a `TodoItem::Static` here, as it will cause statics used by + // multiple crates to be duplicated between them. It isn't necessary anyway, + // as it will get pushed by `codegen_static` when necessary. let linkage = crate::linkage::get_static_ref_linkage(tcx, def_id); data_id_for_static(tcx, module, def_id, linkage) } diff --git a/test.sh b/test.sh index 997c4da7538..b961069e541 100755 --- a/test.sh +++ b/test.sh @@ -26,7 +26,7 @@ rm -r target/out || true mkdir -p target/out/clif echo "[BUILD] mini_core" -$RUSTC example/mini_core.rs --crate-name mini_core --crate-type dylib +$RUSTC example/mini_core.rs --crate-name mini_core --crate-type lib,dylib echo "[BUILD] example" $RUSTC example/example.rs --crate-type lib