@@ -135,7 +135,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
135135 let ty = self . get_type_of_global ( aliasee) ;
136136
137137 for ( alias, linkage, visibility) in aliases {
138- let symbol_name = self . tcx . symbol_name ( Instance :: mono ( self . tcx , * alias) ) ;
138+ let instance = Instance :: mono ( self . tcx , * alias) ;
139+ let symbol_name = self . tcx . symbol_name ( instance) ;
139140 tracing:: debug!( "STATIC ALIAS: {alias:?} {linkage:?} {visibility:?}" ) ;
140141
141142 let lldecl = llvm:: add_alias (
@@ -145,6 +146,13 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
145146 aliasee,
146147 & CString :: new ( symbol_name. name ) . unwrap ( ) ,
147148 ) ;
149+ // Add the alias name to the set of cached items, so there is no duplicate
150+ // instance added to it during the normal `external static` codegen
151+ let prev_entry = self . instances . borrow_mut ( ) . insert ( instance, lldecl) ;
152+
153+ // If there already was a previous entry, then `add_static_aliases` was called multiple times for the same `alias`
154+ // which would result in incorrect codegen
155+ assert ! ( prev_entry. is_none( ) , "An instance was already present for {instance:?}" ) ;
148156
149157 llvm:: set_visibility ( lldecl, base:: visibility_to_llvm ( * visibility) ) ;
150158 llvm:: set_linkage ( lldecl, base:: linkage_to_llvm ( * linkage) ) ;
0 commit comments