Skip to content

Commit 50d1271

Browse files
Auto merge of #144678 - jdonszelmann:no-mangle-extern, r=<try>
Make no_mangle on foreign items explicit instead of implicit try-job: test-various
2 parents da19b9d + 9003a3b commit 50d1271

File tree

6 files changed

+72
-54
lines changed

6 files changed

+72
-54
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,35 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
443443
if tcx.should_inherit_track_caller(did) {
444444
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
445445
}
446+
447+
// Foreign items by default use no mangling for their symbol name.
448+
if tcx.is_foreign_item(did) {
449+
// There's a few exceptions to this rule though:
450+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
451+
// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
452+
// both for exports and imports through foreign items. This is handled further,
453+
// during symbol mangling logic/
454+
} else if codegen_fn_attrs.link_name.is_some() {
455+
// * This can be overridden with the `#[link_name]` attribute
456+
} else if tcx.sess.target.is_like_wasm
457+
&& tcx.wasm_import_module_map(LOCAL_CRATE).contains_key(&did.into())
458+
{
459+
// * On the wasm32 targets there is a bug (or feature) in LLD [1] where the
460+
// same-named symbol when imported from different wasm modules will get
461+
// hooked up incorrectly. As a result foreign symbols, on the wasm target,
462+
// with a wasm import module, get mangled. Additionally our codegen will
463+
// deduplicate symbols based purely on the symbol name, but for wasm this
464+
// isn't quite right because the same-named symbol on wasm can come from
465+
// different modules. For these reasons if `#[link(wasm_import_module)]`
466+
// is present we mangle everything on wasm because the demangled form will
467+
// show up in the `wasm-import-name` custom attribute in LLVM IR.
468+
//
469+
// [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
470+
} else {
471+
// if none of the exceptions apply; apply no_mangle
472+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
473+
}
474+
}
446475
}
447476

448477
fn check_result(

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ impl CodegenFnAttrs {
192192
/// * `#[export_name(...)]` is present
193193
/// * `#[linkage]` is present
194194
///
195+
/// Note that this returns true for foreign items.
196+
/// However, in some places that care about `contains_extern_indicator`, foreign items
197+
/// (in an `extern` block) should explicitly be ignored.
198+
///
195199
/// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
196200
pub fn contains_extern_indicator(&self) -> bool {
197201
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)

compiler/rustc_passes/src/check_attr.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,24 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
563563
match target {
564564
Target::Fn
565565
| Target::Closure
566-
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {}
566+
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
567+
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
568+
if let Some(did) = hir_id.as_owner()
569+
&& self.tcx.def_kind(did).has_codegen_attrs()
570+
&& kind != &InlineAttr::Never
571+
{
572+
let attrs = self.tcx.codegen_fn_attrs(did);
573+
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
574+
if attrs.contains_extern_indicator() {
575+
self.tcx.emit_node_span_lint(
576+
UNUSED_ATTRIBUTES,
577+
hir_id,
578+
attr_span,
579+
errors::InlineIgnoredForExported {},
580+
);
581+
}
582+
}
583+
}
567584
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
568585
self.tcx.emit_node_span_lint(
569586
UNUSED_ATTRIBUTES,
@@ -590,23 +607,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
590607
self.dcx().emit_err(errors::InlineNotFnOrClosure { attr_span, defn_span });
591608
}
592609
}
593-
594-
// `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
595-
if let Some(did) = hir_id.as_owner()
596-
&& self.tcx.def_kind(did).has_codegen_attrs()
597-
&& kind != &InlineAttr::Never
598-
{
599-
let attrs = self.tcx.codegen_fn_attrs(did);
600-
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
601-
if attrs.contains_extern_indicator() {
602-
self.tcx.emit_node_span_lint(
603-
UNUSED_ATTRIBUTES,
604-
hir_id,
605-
attr_span,
606-
errors::InlineIgnoredForExported {},
607-
);
608-
}
609-
}
610610
}
611611

612612
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,

compiler/rustc_passes/src/dead.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,17 @@ fn has_allow_dead_code_or_lang_attr(
694694
}
695695

696696
fn has_used_like_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
697-
tcx.def_kind(def_id).has_codegen_attrs() && {
698-
let cg_attrs = tcx.codegen_fn_attrs(def_id);
699-
700-
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
701-
// forcefully, e.g., for placing it in a specific section.
702-
cg_attrs.contains_extern_indicator()
703-
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
704-
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
705-
}
697+
tcx.def_kind(def_id).has_codegen_attrs()
698+
&& {
699+
let cg_attrs = tcx.codegen_fn_attrs(def_id);
700+
701+
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
702+
// forcefully, e.g., for placing it in a specific section.
703+
cg_attrs.contains_extern_indicator()
704+
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
705+
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
706+
}
707+
&& !tcx.is_foreign_item(def_id)
706708
}
707709

708710
if has_allow_expect_dead_code(tcx, def_id) {

compiler/rustc_passes/src/reachable.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ impl<'tcx> ReachableContext<'tcx> {
183183
} else {
184184
CodegenFnAttrs::EMPTY
185185
};
186-
let is_extern = codegen_attrs.contains_extern_indicator();
186+
let is_extern =
187+
codegen_attrs.contains_extern_indicator() && !self.tcx.is_foreign_item(search_item);
187188
if is_extern {
188189
self.reachable_symbols.insert(search_item);
189190
}
@@ -417,12 +418,17 @@ fn check_item<'tcx>(
417418
}
418419

419420
fn has_custom_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
421+
if tcx.is_foreign_item(def_id) {
422+
return false;
423+
}
424+
420425
// Anything which has custom linkage gets thrown on the worklist no
421426
// matter where it is in the crate, along with "special std symbols"
422427
// which are currently akin to allocator symbols.
423428
if !tcx.def_kind(def_id).has_codegen_attrs() {
424429
return false;
425430
}
431+
426432
let codegen_attrs = tcx.codegen_fn_attrs(def_id);
427433
codegen_attrs.contains_extern_indicator()
428434
// FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

0 commit comments

Comments
 (0)