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,
}
}
/// 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.

View File

@ -1007,7 +1007,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
self.encode_attrs(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));
}
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::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
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::stability;
use crate::mir::interpret::{self, Allocation, ConstAllocation, ConstValue, Scalar};
@ -1066,6 +1067,28 @@ pub struct GlobalCtxt<'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(
self,
def: ty::WithOptConstParam<LocalDefId>,

View File

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

View File

@ -139,42 +139,6 @@ impl<'tcx> TyCtxt<'tcx> {
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> {
match res {
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;
}
let (thir, expr) = match tcx.thir_body(def) {
Ok(body) => body,
Err(_) => return,
let Ok((thir, expr)) = tcx.thir_body(def) else {
return
};
let thir = &thir.borrow();
// 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
}
});
let body_target_features: &[_] = if tcx.has_codegen_attrs(tcx.def_kind(def.did)) {
&tcx.codegen_fn_attrs(def.did).target_features
} else {
&[]
};
let body_target_features = &tcx.body_codegen_attrs(def.did.to_def_id()).target_features;
let safety_context =
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
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;
// Constants don't have codegen attributes, so the body might not have codegen attributes.
let self_features: &[_] = if self.tcx.has_codegen_attrs(self.tcx.def_kind(self.body_did)) {
&self.tcx.codegen_fn_attrs(self.body_did).target_features
} else {
&[]
};
// The body might be a constant, so it doesn't have codegen attributes.
let self_features = &self.tcx.body_codegen_attrs(self.body_did.to_def_id()).target_features;
// Is `callee_features` a subset of `calling_features`?
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);
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);
// #[used], #[no_mangle], #[export_name], etc also keeps the item alive

View File

@ -208,7 +208,7 @@ impl<'tcx> ReachableContext<'tcx> {
} else {
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)
} else {
CodegenFnAttrs::EMPTY
@ -333,7 +333,7 @@ impl CollectPrivateImplItemsVisitor<'_, '_> {
// Anything which has custom linkage gets thrown on the worklist no
// matter where it is in the crate, along with "special std 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);
if codegen_attrs.contains_extern_indicator()
|| 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.
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)
} else {
CodegenFnAttrs::EMPTY

View File

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