Skip to content

Commit c36ea63

Browse files
committed
Merge link_name and export_name
1 parent 69c0013 commit c36ea63

File tree

8 files changed

+33
-44
lines changed

8 files changed

+33
-44
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
513513
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", module));
514514

515515
let name =
516-
codegen_fn_attrs.link_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
516+
codegen_fn_attrs.symbol_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
517517
let name = name.as_str();
518518
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
519519
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ pub fn is_call_from_compiler_builtins_to_upstream_monomorphization<'tcx>(
857857
instance: Instance<'tcx>,
858858
) -> bool {
859859
fn is_llvm_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
860-
if let Some(name) = tcx.codegen_fn_attrs(def_id).link_name {
860+
if let Some(name) = tcx.codegen_fn_attrs(def_id).symbol_name {
861861
name.as_str().starts_with("llvm.")
862862
} else {
863863
false

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,21 @@ fn process_builtin_attrs(
189189
match p {
190190
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
191191
AttributeKind::ExportName { name, .. } => {
192-
codegen_fn_attrs.export_name = Some(*name)
192+
codegen_fn_attrs.symbol_name = Some(*name)
193193
}
194194
AttributeKind::Inline(inline, span) => {
195195
codegen_fn_attrs.inline = *inline;
196196
interesting_spans.inline = Some(*span);
197197
}
198198
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
199199
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
200-
AttributeKind::LinkName { name, .. } => codegen_fn_attrs.link_name = Some(*name),
200+
AttributeKind::LinkName { name, .. } => {
201+
// FIXME Remove check for foreign functions once #[link_name] on non-foreign
202+
// functions is a hard error
203+
if tcx.is_foreign_item(did) {
204+
codegen_fn_attrs.symbol_name = Some(*name);
205+
}
206+
}
201207
AttributeKind::LinkOrdinal { ordinal, span } => {
202208
codegen_fn_attrs.link_ordinal = Some(*ordinal);
203209
interesting_spans.link_ordinal = Some(*span);
@@ -417,7 +423,7 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
417423
// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
418424
// both for exports and imports through foreign items. This is handled further,
419425
// during symbol mangling logic.
420-
} else if codegen_fn_attrs.link_name.is_some() {
426+
} else if codegen_fn_attrs.symbol_name.is_some() {
421427
// * This can be overridden with the `#[link_name]` attribute
422428
} else {
423429
// NOTE: there's one more exception that we cannot apply here. On wasm,
@@ -472,7 +478,7 @@ fn check_result(
472478
}
473479

474480
// error when specifying link_name together with link_ordinal
475-
if let Some(_) = codegen_fn_attrs.link_name
481+
if let Some(_) = codegen_fn_attrs.symbol_name
476482
&& let Some(_) = codegen_fn_attrs.link_ordinal
477483
{
478484
let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
@@ -523,8 +529,7 @@ fn handle_lang_items(
523529
&& let Some(link_name) = lang_item.link_name()
524530
{
525531
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
526-
codegen_fn_attrs.export_name = Some(link_name);
527-
codegen_fn_attrs.link_name = Some(link_name);
532+
codegen_fn_attrs.symbol_name = Some(link_name);
528533
}
529534

530535
// error when using no_mangle on a lang item item

compiler/rustc_lint/src/foreign_modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl ClashingExternDeclarations {
179179
/// symbol's name.
180180
fn name_of_extern_decl(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> SymbolName {
181181
if let Some((overridden_link_name, overridden_link_name_span)) =
182-
tcx.codegen_fn_attrs(fi).link_name.map(|overridden_link_name| {
182+
tcx.codegen_fn_attrs(fi).symbol_name.map(|overridden_link_name| {
183183
// FIXME: Instead of searching through the attributes again to get span
184184
// information, we could have codegen_fn_attrs also give span information back for
185185
// where the attribute was defined. However, until this is found to be a

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ impl<'tcx> Collector<'tcx> {
701701
.link_ordinal
702702
.map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));
703703

704-
let name = codegen_fn_attrs.link_name.unwrap_or_else(|| self.tcx.item_name(item));
704+
let name = codegen_fn_attrs.symbol_name.unwrap_or_else(|| self.tcx.item_name(item));
705705

706706
if self.tcx.sess.target.binary_format == BinaryFormat::Elf {
707707
let name = name.as_str();

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,10 @@ pub struct CodegenFnAttrs {
3636
pub inline: InlineAttr,
3737
/// Parsed representation of the `#[optimize]` attribute
3838
pub optimize: OptimizeAttr,
39-
/// The `#[export_name = "..."]` attribute, indicating a custom symbol a
40-
/// function should be exported under
41-
pub export_name: Option<Symbol>,
42-
/// The `#[link_name = "..."]` attribute, indicating a custom symbol an
43-
/// imported function should be imported as. Note that `export_name`
44-
/// probably isn't set when this is set, this is for foreign items while
45-
/// `#[export_name]` is for Rust-defined functions.
46-
pub link_name: Option<Symbol>,
39+
/// The name this function will be imported/exported under. This can be set
40+
/// using the `#[export_name = "..."]` or `#[link_name = "..."]` attribute
41+
/// depending on if this is a function definition or foreign function.
42+
pub symbol_name: Option<Symbol>,
4743
/// The `#[link_ordinal = "..."]` attribute, indicating an ordinal an
4844
/// imported function has in the dynamic library. Note that this must not
4945
/// be set when `link_name` is set. This is for foreign items with the
@@ -170,8 +166,7 @@ impl CodegenFnAttrs {
170166
flags: CodegenFnAttrFlags::empty(),
171167
inline: InlineAttr::None,
172168
optimize: OptimizeAttr::Default,
173-
export_name: None,
174-
link_name: None,
169+
symbol_name: None,
175170
link_ordinal: None,
176171
target_features: vec![],
177172
safe_target_features: false,
@@ -200,7 +195,7 @@ impl CodegenFnAttrs {
200195

201196
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
202197
|| self.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
203-
|| self.export_name.is_some()
198+
|| self.symbol_name.is_some()
204199
|| match self.linkage {
205200
// These are private, so make sure we don't try to consider
206201
// them external.

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,9 @@ fn compute_symbol_name<'tcx>(
204204
// mangling scheme can't be used. For example both the GCC backend and
205205
// Rust-for-Linux don't support some of the characters used by the
206206
// legacy symbol mangling scheme.
207-
let name = if tcx.is_foreign_item(def_id) {
208-
if let Some(name) = attrs.link_name { name } else { tcx.item_name(def_id) }
209-
} else {
210-
if let Some(name) = attrs.export_name { name } else { tcx.item_name(def_id) }
211-
};
207+
let name = if let Some(name) = attrs.symbol_name { name } else { tcx.item_name(def_id) };
212208

213-
return v0::mangle_internal_symbol(tcx, name.as_str());
209+
return v0::mangle_internal_symbol(tcx, name.as_str());
214210
}
215211

216212
let wasm_import_module_exception_force_mangling = {
@@ -235,23 +231,16 @@ fn compute_symbol_name<'tcx>(
235231
&& tcx.wasm_import_module_map(LOCAL_CRATE).contains_key(&def_id.into())
236232
};
237233

238-
if let Some(name) = attrs.link_name
239-
&& !wasm_import_module_exception_force_mangling
240-
{
241-
// Use provided name
242-
return name.to_string();
243-
}
244-
245-
if let Some(name) = attrs.export_name {
246-
// Use provided name
247-
return name.to_string();
248-
}
234+
if !wasm_import_module_exception_force_mangling {
235+
if let Some(name) = attrs.symbol_name {
236+
// Use provided name
237+
return name.to_string();
238+
}
249239

250-
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
251-
&& !wasm_import_module_exception_force_mangling
252-
{
253-
// Don't mangle
254-
return tcx.item_name(def_id).to_string();
240+
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
241+
// Don't mangle
242+
return tcx.item_name(def_id).to_string();
243+
}
255244
}
256245

257246
// If we're dealing with an instance of a function that's inlined from

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
146146
return interp_ok(());
147147
}
148148
// Skip over items without an explicitly defined symbol name.
149-
if !(attrs.export_name.is_some()
149+
if !(attrs.symbol_name.is_some()
150150
|| attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
151151
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL))
152152
{

0 commit comments

Comments
 (0)