Skip to content

Commit 32b13ac

Browse files
committed
review
1 parent d371ebe commit 32b13ac

File tree

11 files changed

+73
-59
lines changed

11 files changed

+73
-59
lines changed

compiler/rustc_hir/src/def.rs

+37
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,43 @@ impl DefKind {
229229
_ => false,
230230
}
231231
}
232+
233+
/// Whether `query get_codegen_attrs` should be used with this definition.
234+
pub fn has_codegen_attrs(self) -> bool {
235+
match self {
236+
DefKind::Fn
237+
| DefKind::AssocFn
238+
| DefKind::Ctor(..)
239+
| DefKind::Closure
240+
| DefKind::Generator
241+
| DefKind::Static(_) => true,
242+
DefKind::Mod
243+
| DefKind::Struct
244+
| DefKind::Union
245+
| DefKind::Enum
246+
| DefKind::Variant
247+
| DefKind::Trait
248+
| DefKind::TyAlias
249+
| DefKind::ForeignTy
250+
| DefKind::TraitAlias
251+
| DefKind::AssocTy
252+
| DefKind::Const
253+
| DefKind::AssocConst
254+
| DefKind::Macro(..)
255+
| DefKind::Use
256+
| DefKind::ForeignMod
257+
| DefKind::OpaqueTy
258+
| DefKind::Impl
259+
| DefKind::Field
260+
| DefKind::TyParam
261+
| DefKind::ConstParam
262+
| DefKind::LifetimeParam
263+
| DefKind::AnonConst
264+
| DefKind::InlineConst
265+
| DefKind::GlobalAsm
266+
| DefKind::ExternCrate => false,
267+
}
268+
}
232269
}
233270

234271
/// The resolution of a path or export.

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10071007
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
10081008
self.encode_attrs(def_id);
10091009
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
1010-
if tcx.has_codegen_attrs(def_kind) {
1010+
if def_kind.has_codegen_attrs() {
10111011
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
10121012
}
10131013
if should_encode_visibility(def_kind) {

compiler/rustc_middle/src/ty/context.rs

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
55
use crate::hir::place::Place as HirPlace;
66
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
77
use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintLevelSource};
8+
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
89
use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath};
910
use crate::middle::stability;
1011
use crate::mir::interpret::{self, Allocation, ConstAllocation, ConstValue, Scalar};
@@ -1066,6 +1067,28 @@ pub struct GlobalCtxt<'tcx> {
10661067
}
10671068

10681069
impl<'tcx> TyCtxt<'tcx> {
1070+
/// Expects a body and returns its codegen attributes.
1071+
///
1072+
/// Unlike `codegen_fn_attrs`, this returns `CodegenFnAttrs::EMPTY` for
1073+
/// constants.
1074+
pub fn body_codegen_attrs(self, def_id: DefId) -> &'tcx CodegenFnAttrs {
1075+
let def_kind = self.def_kind(def_id);
1076+
if def_kind.has_codegen_attrs() {
1077+
self.codegen_fn_attrs(def_id)
1078+
} else if matches!(
1079+
def_kind,
1080+
DefKind::AnonConst | DefKind::AssocConst | DefKind::Const | DefKind::InlineConst
1081+
) {
1082+
CodegenFnAttrs::EMPTY
1083+
} else {
1084+
bug!(
1085+
"body_codegen_fn_attrs called on unexpected definition: {:?} {:?}",
1086+
def_id,
1087+
def_kind
1088+
)
1089+
}
1090+
}
1091+
10691092
pub fn typeck_opt_const_arg(
10701093
self,
10711094
def: ty::WithOptConstParam<LocalDefId>,

compiler/rustc_middle/src/ty/instance.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ impl<'tcx> InstanceDef<'tcx> {
246246
match *self {
247247
InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. })
248248
| InstanceDef::Virtual(def_id, _) => {
249-
tcx.has_codegen_attrs(tcx.def_kind(def_id))
250-
&& tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
249+
tcx.body_codegen_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
251250
}
252251
InstanceDef::ClosureOnceShim { call_once: _, track_caller } => track_caller,
253252
_ => false,

compiler/rustc_middle/src/ty/util.rs

-36
Original file line numberDiff line numberDiff line change
@@ -139,42 +139,6 @@ impl<'tcx> TyCtxt<'tcx> {
139139
hasher.finish()
140140
}
141141

142-
pub fn has_codegen_attrs(self, def_kind: DefKind) -> bool {
143-
match def_kind {
144-
DefKind::Fn
145-
| DefKind::AssocFn
146-
| DefKind::Ctor(..)
147-
| DefKind::Closure
148-
| DefKind::Generator
149-
| DefKind::Static(_) => true,
150-
DefKind::Mod
151-
| DefKind::Struct
152-
| DefKind::Union
153-
| DefKind::Enum
154-
| DefKind::Variant
155-
| DefKind::Trait
156-
| DefKind::TyAlias
157-
| DefKind::ForeignTy
158-
| DefKind::TraitAlias
159-
| DefKind::AssocTy
160-
| DefKind::Const
161-
| DefKind::AssocConst
162-
| DefKind::Macro(..)
163-
| DefKind::Use
164-
| DefKind::ForeignMod
165-
| DefKind::OpaqueTy
166-
| DefKind::Impl
167-
| DefKind::Field
168-
| DefKind::TyParam
169-
| DefKind::ConstParam
170-
| DefKind::LifetimeParam
171-
| DefKind::AnonConst
172-
| DefKind::InlineConst
173-
| DefKind::GlobalAsm
174-
| DefKind::ExternCrate => false,
175-
}
176-
}
177-
178142
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
179143
match res {
180144
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {

compiler/rustc_mir_build/src/check_unsafety.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,8 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
643643
return;
644644
}
645645

646-
let (thir, expr) = match tcx.thir_body(def) {
647-
Ok(body) => body,
648-
Err(_) => return,
646+
let Ok((thir, expr)) = tcx.thir_body(def) else {
647+
return
649648
};
650649
let thir = &thir.borrow();
651650
// If `thir` is empty, a type error occurred, skip this body.
@@ -661,11 +660,7 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
661660
BodyUnsafety::Safe
662661
}
663662
});
664-
let body_target_features: &[_] = if tcx.has_codegen_attrs(tcx.def_kind(def.did)) {
665-
&tcx.codegen_fn_attrs(def.did).target_features
666-
} else {
667-
&[]
668-
};
663+
let body_target_features = &tcx.body_codegen_attrs(def.did.to_def_id()).target_features;
669664
let safety_context =
670665
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
671666
let mut visitor = UnsafetyVisitor {

compiler/rustc_mir_transform/src/check_unsafety.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
375375
}
376376

377377
let callee_features = &self.tcx.codegen_fn_attrs(func_did).target_features;
378-
// Constants don't have codegen attributes, so the body might not have codegen attributes.
379-
let self_features: &[_] = if self.tcx.has_codegen_attrs(self.tcx.def_kind(self.body_did)) {
380-
&self.tcx.codegen_fn_attrs(self.body_did).target_features
381-
} else {
382-
&[]
383-
};
378+
// The body might be a constant, so it doesn't have codegen attributes.
379+
let self_features = &self.tcx.body_codegen_attrs(self.body_did.to_def_id()).target_features;
384380

385381
// Is `callee_features` a subset of `calling_features`?
386382
if !callee_features.iter().all(|feature| self_features.contains(feature)) {

compiler/rustc_passes/src/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
452452
}
453453

454454
let def_id = tcx.hir().local_def_id(id);
455-
if tcx.has_codegen_attrs(tcx.def_kind(def_id)) {
455+
if tcx.def_kind(def_id).has_codegen_attrs() {
456456
let cg_attrs = tcx.codegen_fn_attrs(def_id);
457457

458458
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive

compiler/rustc_passes/src/reachable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'tcx> ReachableContext<'tcx> {
208208
} else {
209209
false
210210
};
211-
let codegen_attrs = if self.tcx.has_codegen_attrs(self.tcx.def_kind(search_item)) {
211+
let codegen_attrs = if self.tcx.def_kind(search_item).has_codegen_attrs() {
212212
self.tcx.codegen_fn_attrs(search_item)
213213
} else {
214214
CodegenFnAttrs::EMPTY
@@ -333,7 +333,7 @@ impl CollectPrivateImplItemsVisitor<'_, '_> {
333333
// Anything which has custom linkage gets thrown on the worklist no
334334
// matter where it is in the crate, along with "special std symbols"
335335
// which are currently akin to allocator symbols.
336-
if self.tcx.has_codegen_attrs(self.tcx.def_kind(def_id)) {
336+
if self.tcx.def_kind(def_id).has_codegen_attrs() {
337337
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
338338
if codegen_attrs.contains_extern_indicator()
339339
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)

compiler/rustc_symbol_mangling/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn compute_symbol_name<'tcx>(
177177
}
178178

179179
// FIXME(eddyb) Precompute a custom symbol name based on attributes.
180-
let attrs = if tcx.has_codegen_attrs(tcx.def_kind(def_id)) {
180+
let attrs = if tcx.def_kind(def_id).has_codegen_attrs() {
181181
tcx.codegen_fn_attrs(def_id)
182182
} else {
183183
CodegenFnAttrs::EMPTY

compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
27232723
if cfg!(debug_assertions) {
27242724
let def_kind = tcx.def_kind(did);
27252725
assert!(
2726-
tcx.has_codegen_attrs(def_kind),
2726+
def_kind.has_codegen_attrs(),
27272727
"unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
27282728
);
27292729
}
@@ -3233,7 +3233,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
32333233
/// inline assembly.
32343234
fn asm_target_features<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx FxHashSet<Symbol> {
32353235
let mut target_features = tcx.sess.target_features.clone();
3236-
if tcx.has_codegen_attrs(tcx.def_kind(did)) {
3236+
if tcx.def_kind(did).has_codegen_attrs() {
32373237
let attrs = tcx.codegen_fn_attrs(did);
32383238
target_features.extend(&attrs.target_features);
32393239
match attrs.instruction_set {

0 commit comments

Comments
 (0)