diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 559ce227454de..389a218535bb8 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1186,6 +1186,13 @@ struct RootCollector<'a, 'tcx> { impl<'v> RootCollector<'_, 'v> { fn process_item(&mut self, id: hir::ItemId) { + let def_id = id.owner_id.def_id; + let (live_symbols, _) = self.tcx.live_symbols_and_ignored_derived_traits(()); + if !live_symbols.contains(&def_id) && !self.tcx.sess.link_dead_code() { + // This is dead code; ignore it. + return; + } + match self.tcx.def_kind(id.owner_id) { DefKind::Enum | DefKind::Struct | DefKind::Union => { let item = self.tcx.hir().item(id); diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 5d0224c35f364..68c49ab064799 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -623,6 +623,11 @@ fn create_and_seed_worklist<'tcx>( check_foreign_item(tcx, &mut worklist, id); } + if let Some(static_) = tcx.proc_macro_decls_static(()) { + // We assume this is always used if present. + worklist.push(static_); + } + (worklist, struct_constructors) }