@@ -218,40 +218,43 @@ fn compute_symbol_name<'tcx>(
218218 }
219219 }
220220
221- // Foreign items by default use no mangling for their symbol name. There's a
222- // few exceptions to this rule though:
223- //
224- // * This can be overridden with the `#[link_name]` attribute
225- //
226- // * On the wasm32 targets there is a bug (or feature) in LLD [1] where the
227- // same-named symbol when imported from different wasm modules will get
228- // hooked up incorrectly. As a result foreign symbols, on the wasm target,
229- // with a wasm import module, get mangled. Additionally our codegen will
230- // deduplicate symbols based purely on the symbol name, but for wasm this
231- // isn't quite right because the same-named symbol on wasm can come from
232- // different modules. For these reasons if `#[link(wasm_import_module)]`
233- // is present we mangle everything on wasm because the demangled form will
234- // show up in the `wasm-import-name` custom attribute in LLVM IR.
235- //
236- // * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
237- // both for exports and imports through foreign items. This is handled above.
238- // [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
239- if tcx. is_foreign_item ( def_id)
240- && ( !tcx. sess . target . is_like_wasm
241- || !tcx. wasm_import_module_map ( def_id. krate ) . contains_key ( & def_id) )
221+ let wasm_import_module_exception_force_mangling = {
222+ // * On the wasm32 targets there is a bug (or feature) in LLD [1] where the
223+ // same-named symbol when imported from different wasm modules will get
224+ // hooked up incorrectly. As a result foreign symbols, on the wasm target,
225+ // with a wasm import module, get mangled. Additionally our codegen will
226+ // deduplicate symbols based purely on the symbol name, but for wasm this
227+ // isn't quite right because the same-named symbol on wasm can come from
228+ // different modules. For these reasons if `#[link(wasm_import_module)]`
229+ // is present we mangle everything on wasm because the demangled form will
230+ // show up in the `wasm-import-name` custom attribute in LLVM IR.
231+ //
232+ // [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
233+ //
234+ // So, on wasm if a foreign item loses its `#[no_mangle]`, it might *still*
235+ // be mangled if we're forced to. Note: I don't like this.
236+ // These kinds of exceptions should be added during the `codegen_attrs` query.
237+ // However, we don't have the wasm import module map there yet.
238+ tcx. is_foreign_item ( def_id)
239+ && tcx. sess . target . is_like_wasm
240+ && tcx. wasm_import_module_map ( LOCAL_CRATE ) . contains_key ( & def_id. into ( ) )
241+ } ;
242+
243+ if let Some ( name) = attrs. link_name
244+ && !wasm_import_module_exception_force_mangling
242245 {
243- if let Some ( name) = attrs. link_name {
244- return name. to_string ( ) ;
245- }
246- return tcx. item_name ( def_id) . to_string ( ) ;
246+ // Use provided name
247+ return name. to_string ( ) ;
247248 }
248249
249250 if let Some ( name) = attrs. export_name {
250251 // Use provided name
251252 return name. to_string ( ) ;
252253 }
253254
254- if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE ) {
255+ if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE )
256+ && !wasm_import_module_exception_force_mangling
257+ {
255258 // Don't mangle
256259 return tcx. item_name ( def_id) . to_string ( ) ;
257260 }
0 commit comments