Skip to content

Commit d66ca53

Browse files
committed
add a flag to codegen fn attrs for foreign items
1 parent b779120 commit d66ca53

File tree

11 files changed

+16
-14
lines changed

11 files changed

+16
-14
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel
570570
// core/std/allocators/etc. For example symbols used to hook up allocation
571571
// are not considered for export
572572
let codegen_fn_attrs = tcx.codegen_fn_attrs(sym_def_id);
573-
let is_extern = codegen_fn_attrs.contains_extern_indicator(tcx, sym_def_id);
573+
let is_extern = codegen_fn_attrs.contains_extern_indicator();
574574
let std_internal =
575575
codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
576576

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
385385

386386
// Foreign items by default use no mangling for their symbol name.
387387
if tcx.is_foreign_item(did) {
388+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
389+
388390
// There's a few exceptions to this rule though:
389391
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
390392
// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22

33
use rustc_abi::Align;
44
use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, Linkage, OptimizeAttr};
5-
use rustc_hir::def_id::DefId;
65
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
76
use rustc_span::Symbol;
87
use rustc_target::spec::SanitizerSet;
@@ -161,6 +160,8 @@ bitflags::bitflags! {
161160
const ALLOCATOR_ZEROED = 1 << 14;
162161
/// `#[no_builtins]`: indicates that disable implicit builtin knowledge of functions for the function.
163162
const NO_BUILTINS = 1 << 15;
163+
/// Marks foreign items, to make `contains_extern_indicator` cheaper.
164+
const FOREIGN_ITEM = 1 << 16;
164165
}
165166
}
166167
rustc_data_structures::external_bitflags_debug! { CodegenFnAttrFlags }
@@ -194,8 +195,8 @@ impl CodegenFnAttrs {
194195
/// * `#[linkage]` is present
195196
///
196197
/// Keep this in sync with the logic for the unused_attributes for `#[inline]` lint.
197-
pub fn contains_extern_indicator(&self, tcx: TyCtxt<'_>, did: DefId) -> bool {
198-
if tcx.is_foreign_item(did) {
198+
pub fn contains_extern_indicator(&self) -> bool {
199+
if self.flags.contains(CodegenFnAttrFlags::FOREIGN_ITEM) {
199200
return false;
200201
}
201202

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'tcx> MonoItem<'tcx> {
149149
// instantiation:
150150
// We emit an unused_attributes lint for this case, which should be kept in sync if possible.
151151
let codegen_fn_attrs = tcx.codegen_instance_attrs(instance.def);
152-
if codegen_fn_attrs.contains_extern_indicator(tcx, instance.def.def_id())
152+
if codegen_fn_attrs.contains_extern_indicator()
153153
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED)
154154
{
155155
return InstantiationMode::GloballyShared { may_conflict: false };

compiler/rustc_mir_transform/src/cross_crate_inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
1818
let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
1919
// If this has an extern indicator, then this function is globally shared and thus will not
2020
// generate cgu-internal copies which would make it cross-crate inlinable.
21-
if codegen_fn_attrs.contains_extern_indicator(tcx, def_id.into()) {
21+
if codegen_fn_attrs.contains_extern_indicator() {
2222
return false;
2323
}
2424

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
495495
{
496496
let attrs = self.tcx.codegen_fn_attrs(did);
497497
// Not checking naked as `#[inline]` is forbidden for naked functions anyways.
498-
if attrs.contains_extern_indicator(self.tcx, did.into()) {
498+
if attrs.contains_extern_indicator() {
499499
self.tcx.emit_node_span_lint(
500500
UNUSED_ATTRIBUTES,
501501
hir_id,

compiler/rustc_passes/src/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ fn has_allow_dead_code_or_lang_attr(
706706

707707
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive
708708
// forcefully, e.g., for placing it in a specific section.
709-
cg_attrs.contains_extern_indicator(tcx, def_id.into())
709+
cg_attrs.contains_extern_indicator()
710710
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
711711
|| cg_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
712712
}

compiler/rustc_passes/src/reachable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'tcx> ReachableContext<'tcx> {
183183
} else {
184184
CodegenFnAttrs::EMPTY
185185
};
186-
let is_extern = codegen_attrs.contains_extern_indicator(self.tcx, search_item.into());
186+
let is_extern = codegen_attrs.contains_extern_indicator();
187187
if is_extern {
188188
self.reachable_symbols.insert(search_item);
189189
}
@@ -425,7 +425,7 @@ fn has_custom_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
425425
}
426426

427427
let codegen_attrs = tcx.codegen_fn_attrs(def_id);
428-
codegen_attrs.contains_extern_indicator(tcx, def_id.into())
428+
codegen_attrs.contains_extern_indicator()
429429
// FIXME(nbdd0121): `#[used]` are marked as reachable here so it's picked up by
430430
// `linked_symbols` in cg_ssa. They won't be exported in binary or cdylib due to their
431431
// `SymbolExportLevel::Rust` export level but may end up being exported in dylibs.

src/tools/clippy/clippy_lints/src/missing_inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,5 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
190190
/// and a rustc warning would be triggered, see #15301
191191
fn fn_is_externally_exported(cx: &LateContext<'_>, def_id: DefId) -> bool {
192192
let attrs = cx.tcx.codegen_fn_attrs(def_id);
193-
attrs.contains_extern_indicator(cx.tcx, def_id)
193+
attrs.contains_extern_indicator()
194194
}

src/tools/miri/src/bin/miri.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
279279
return None;
280280
}
281281
let codegen_fn_attrs = tcx.codegen_fn_attrs(local_def_id);
282-
if codegen_fn_attrs.contains_extern_indicator(tcx, local_def_id.into())
282+
if codegen_fn_attrs.contains_extern_indicator()
283283
|| codegen_fn_attrs
284284
.flags
285285
.contains(CodegenFnAttrFlags::USED_COMPILER)

0 commit comments

Comments
 (0)