1
Fork 0
This commit is contained in:
lcnr 2022-05-09 17:47:02 +02:00
parent d371ebe117
commit 32b13ac928
11 changed files with 73 additions and 59 deletions

View file

@ -229,6 +229,43 @@ impl DefKind {
_ => false, _ => false,
} }
} }
/// Whether `query get_codegen_attrs` should be used with this definition.
pub fn has_codegen_attrs(self) -> bool {
match self {
DefKind::Fn
| DefKind::AssocFn
| DefKind::Ctor(..)
| DefKind::Closure
| DefKind::Generator
| DefKind::Static(_) => true,
DefKind::Mod
| DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Variant
| DefKind::Trait
| DefKind::TyAlias
| DefKind::ForeignTy
| DefKind::TraitAlias
| DefKind::AssocTy
| DefKind::Const
| DefKind::AssocConst
| DefKind::Macro(..)
| DefKind::Use
| DefKind::ForeignMod
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Field
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm
| DefKind::ExternCrate => false,
}
}
} }
/// The resolution of a path or export. /// The resolution of a path or export.

View file

@ -1007,7 +1007,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id)); record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
self.encode_attrs(def_id); self.encode_attrs(def_id);
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
if tcx.has_codegen_attrs(def_kind) { if def_kind.has_codegen_attrs() {
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
} }
if should_encode_visibility(def_kind) { if should_encode_visibility(def_kind) {

View file

@ -5,6 +5,7 @@ use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
use crate::hir::place::Place as HirPlace; use crate::hir::place::Place as HirPlace;
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos}; use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintLevelSource}; use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintLevelSource};
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath}; use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath};
use crate::middle::stability; use crate::middle::stability;
use crate::mir::interpret::{self, Allocation, ConstAllocation, ConstValue, Scalar}; use crate::mir::interpret::{self, Allocation, ConstAllocation, ConstValue, Scalar};
@ -1066,6 +1067,28 @@ pub struct GlobalCtxt<'tcx> {
} }
impl<'tcx> TyCtxt<'tcx> { impl<'tcx> TyCtxt<'tcx> {
/// Expects a body and returns its codegen attributes.
///
/// Unlike `codegen_fn_attrs`, this returns `CodegenFnAttrs::EMPTY` for
/// constants.
pub fn body_codegen_attrs(self, def_id: DefId) -> &'tcx CodegenFnAttrs {
let def_kind = self.def_kind(def_id);
if def_kind.has_codegen_attrs() {
self.codegen_fn_attrs(def_id)
} else if matches!(
def_kind,
DefKind::AnonConst | DefKind::AssocConst | DefKind::Const | DefKind::InlineConst
) {
CodegenFnAttrs::EMPTY
} else {
bug!(
"body_codegen_fn_attrs called on unexpected definition: {:?} {:?}",
def_id,
def_kind
)
}
}
pub fn typeck_opt_const_arg( pub fn typeck_opt_const_arg(
self, self,
def: ty::WithOptConstParam<LocalDefId>, def: ty::WithOptConstParam<LocalDefId>,

View file

@ -246,8 +246,7 @@ impl<'tcx> InstanceDef<'tcx> {
match *self { match *self {
InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. }) InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. })
| InstanceDef::Virtual(def_id, _) => { | InstanceDef::Virtual(def_id, _) => {
tcx.has_codegen_attrs(tcx.def_kind(def_id)) tcx.body_codegen_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
&& tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
} }
InstanceDef::ClosureOnceShim { call_once: _, track_caller } => track_caller, InstanceDef::ClosureOnceShim { call_once: _, track_caller } => track_caller,
_ => false, _ => false,

View file

@ -139,42 +139,6 @@ impl<'tcx> TyCtxt<'tcx> {
hasher.finish() hasher.finish()
} }
pub fn has_codegen_attrs(self, def_kind: DefKind) -> bool {
match def_kind {
DefKind::Fn
| DefKind::AssocFn
| DefKind::Ctor(..)
| DefKind::Closure
| DefKind::Generator
| DefKind::Static(_) => true,
DefKind::Mod
| DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Variant
| DefKind::Trait
| DefKind::TyAlias
| DefKind::ForeignTy
| DefKind::TraitAlias
| DefKind::AssocTy
| DefKind::Const
| DefKind::AssocConst
| DefKind::Macro(..)
| DefKind::Use
| DefKind::ForeignMod
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Field
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::LifetimeParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm
| DefKind::ExternCrate => false,
}
}
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> { pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
match res { match res {
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => { Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {

View file

@ -643,9 +643,8 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
return; return;
} }
let (thir, expr) = match tcx.thir_body(def) { let Ok((thir, expr)) = tcx.thir_body(def) else {
Ok(body) => body, return
Err(_) => return,
}; };
let thir = &thir.borrow(); let thir = &thir.borrow();
// If `thir` is empty, a type error occurred, skip this body. // 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
BodyUnsafety::Safe BodyUnsafety::Safe
} }
}); });
let body_target_features: &[_] = if tcx.has_codegen_attrs(tcx.def_kind(def.did)) { let body_target_features = &tcx.body_codegen_attrs(def.did.to_def_id()).target_features;
&tcx.codegen_fn_attrs(def.did).target_features
} else {
&[]
};
let safety_context = let safety_context =
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe }; if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
let mut visitor = UnsafetyVisitor { let mut visitor = UnsafetyVisitor {

View file

@ -375,12 +375,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> {
} }
let callee_features = &self.tcx.codegen_fn_attrs(func_did).target_features; let callee_features = &self.tcx.codegen_fn_attrs(func_did).target_features;
// Constants don't have codegen attributes, so the body might not have codegen attributes. // The body might be a constant, so it doesn't have codegen attributes.
let self_features: &[_] = if self.tcx.has_codegen_attrs(self.tcx.def_kind(self.body_did)) { let self_features = &self.tcx.body_codegen_attrs(self.body_did.to_def_id()).target_features;
&self.tcx.codegen_fn_attrs(self.body_did).target_features
} else {
&[]
};
// Is `callee_features` a subset of `calling_features`? // Is `callee_features` a subset of `calling_features`?
if !callee_features.iter().all(|feature| self_features.contains(feature)) { if !callee_features.iter().all(|feature| self_features.contains(feature)) {

View file

@ -452,7 +452,7 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
} }
let def_id = tcx.hir().local_def_id(id); let def_id = tcx.hir().local_def_id(id);
if tcx.has_codegen_attrs(tcx.def_kind(def_id)) { if tcx.def_kind(def_id).has_codegen_attrs() {
let cg_attrs = tcx.codegen_fn_attrs(def_id); let cg_attrs = tcx.codegen_fn_attrs(def_id);
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive // #[used], #[no_mangle], #[export_name], etc also keeps the item alive

View file

@ -208,7 +208,7 @@ impl<'tcx> ReachableContext<'tcx> {
} else { } else {
false false
}; };
let codegen_attrs = if self.tcx.has_codegen_attrs(self.tcx.def_kind(search_item)) { let codegen_attrs = if self.tcx.def_kind(search_item).has_codegen_attrs() {
self.tcx.codegen_fn_attrs(search_item) self.tcx.codegen_fn_attrs(search_item)
} else { } else {
CodegenFnAttrs::EMPTY CodegenFnAttrs::EMPTY
@ -333,7 +333,7 @@ impl CollectPrivateImplItemsVisitor<'_, '_> {
// Anything which has custom linkage gets thrown on the worklist no // Anything which has custom linkage gets thrown on the worklist no
// matter where it is in the crate, along with "special std symbols" // matter where it is in the crate, along with "special std symbols"
// which are currently akin to allocator symbols. // which are currently akin to allocator symbols.
if self.tcx.has_codegen_attrs(self.tcx.def_kind(def_id)) { if self.tcx.def_kind(def_id).has_codegen_attrs() {
let codegen_attrs = self.tcx.codegen_fn_attrs(def_id); let codegen_attrs = self.tcx.codegen_fn_attrs(def_id);
if codegen_attrs.contains_extern_indicator() if codegen_attrs.contains_extern_indicator()
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) || codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)

View file

@ -177,7 +177,7 @@ fn compute_symbol_name<'tcx>(
} }
// FIXME(eddyb) Precompute a custom symbol name based on attributes. // FIXME(eddyb) Precompute a custom symbol name based on attributes.
let attrs = if tcx.has_codegen_attrs(tcx.def_kind(def_id)) { let attrs = if tcx.def_kind(def_id).has_codegen_attrs() {
tcx.codegen_fn_attrs(def_id) tcx.codegen_fn_attrs(def_id)
} else { } else {
CodegenFnAttrs::EMPTY CodegenFnAttrs::EMPTY

View file

@ -2723,7 +2723,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
let def_kind = tcx.def_kind(did); let def_kind = tcx.def_kind(did);
assert!( assert!(
tcx.has_codegen_attrs(def_kind), def_kind.has_codegen_attrs(),
"unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}", "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
); );
} }
@ -3233,7 +3233,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
/// inline assembly. /// inline assembly.
fn asm_target_features<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx FxHashSet<Symbol> { fn asm_target_features<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx FxHashSet<Symbol> {
let mut target_features = tcx.sess.target_features.clone(); let mut target_features = tcx.sess.target_features.clone();
if tcx.has_codegen_attrs(tcx.def_kind(did)) { if tcx.def_kind(did).has_codegen_attrs() {
let attrs = tcx.codegen_fn_attrs(did); let attrs = tcx.codegen_fn_attrs(did);
target_features.extend(&attrs.target_features); target_features.extend(&attrs.target_features);
match attrs.instruction_set { match attrs.instruction_set {