@@ -218,32 +218,9 @@ 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) )
242- {
243- if let Some ( name) = attrs. link_name {
244- return name. to_string ( ) ;
245- }
246- return tcx. item_name ( def_id) . to_string ( ) ;
221+ if let Some ( name) = attrs. link_name {
222+ // Use provided name
223+ return name. to_string ( ) ;
247224 }
248225
249226 if let Some ( name) = attrs. export_name {
@@ -252,8 +229,30 @@ fn compute_symbol_name<'tcx>(
252229 }
253230
254231 if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE ) {
255- // Don't mangle
256- return tcx. item_name ( def_id) . to_string ( ) ;
232+ if tcx. is_foreign_item ( def_id)
233+ && tcx. sess . target . is_like_wasm
234+ && tcx. wasm_import_module_map ( LOCAL_CRATE ) . contains_key ( & def_id. into ( ) )
235+ {
236+ // * On the wasm32 targets there is a bug (or feature) in LLD [1] where the
237+ // same-named symbol when imported from different wasm modules will get
238+ // hooked up incorrectly. As a result foreign symbols, on the wasm target,
239+ // with a wasm import module, get mangled. Additionally our codegen will
240+ // deduplicate symbols based purely on the symbol name, but for wasm this
241+ // isn't quite right because the same-named symbol on wasm can come from
242+ // different modules. For these reasons if `#[link(wasm_import_module)]`
243+ // is present we mangle everything on wasm because the demangled form will
244+ // show up in the `wasm-import-name` custom attribute in LLVM IR.
245+ //
246+ // [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
247+ //
248+ // So, on wasm if a foreign item loses its `#[no_mangle]`, it might *still*
249+ // be mangled if we're forced to. Note: I don't like this.
250+ // These kinds of exceptions should be added during the `codegen_attrs` query.
251+ // However, we don't have the wasm import module map there yet.
252+ } else {
253+ // Don't mangle
254+ return tcx. item_name ( def_id) . to_string ( ) ;
255+ }
257256 }
258257
259258 // If we're dealing with an instance of a function that's inlined from
0 commit comments