Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 88860d5

Browse files
committed
Auto merge of rust-lang#96473 - lcnr:querify-codegen-fn-attrs, r=cjgillot
store `codegen_fn_attrs` in crate metadata extracted from rust-lang#95562 because the change isn't trivial.
2 parents 0dd7e10 + 32b13ac commit 88860d5

File tree

17 files changed

+229
-122
lines changed

17 files changed

+229
-122
lines changed

compiler/rustc_hir/src/def.rs

Lines changed: 37 additions & 0 deletions
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/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
129129
type_of => { table }
130130
variances_of => { table }
131131
fn_sig => { table }
132+
codegen_fn_attrs => { table }
132133
impl_trait_ref => { table }
133134
const_param_default => { table }
134135
thir_abstract_const => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,9 @@ 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 def_kind.has_codegen_attrs() {
1011+
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
1012+
}
10101013
if should_encode_visibility(def_kind) {
10111014
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
10121015
}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hir::definitions::DefKey;
1414
use rustc_hir::lang_items;
1515
use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
1616
use rustc_middle::metadata::ModChild;
17+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1718
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
1819
use rustc_middle::mir;
1920
use rustc_middle::thir;
@@ -329,6 +330,7 @@ define_tables! {
329330
type_of: Table<DefIndex, Lazy!(Ty<'tcx>)>,
330331
variances_of: Table<DefIndex, Lazy<[ty::Variance]>>,
331332
fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
333+
codegen_fn_attrs: Table<DefIndex, Lazy!(CodegenFnAttrs)>,
332334
impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
333335
const_param_default: Table<DefIndex, Lazy<rustc_middle::ty::Const<'tcx>>>,
334336
optimized_mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ bitflags! {
9595
}
9696

9797
impl CodegenFnAttrs {
98-
pub fn new() -> CodegenFnAttrs {
98+
pub const EMPTY: &'static Self = &Self::new();
99+
100+
pub const fn new() -> CodegenFnAttrs {
99101
CodegenFnAttrs {
100102
flags: CodegenFnAttrFlags::empty(),
101103
inline: InlineAttr::None,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,8 @@ rustc_queries! {
10801080
query codegen_fn_attrs(def_id: DefId) -> CodegenFnAttrs {
10811081
desc { |tcx| "computing codegen attributes of `{}`", tcx.def_path_str(def_id) }
10821082
storage(ArenaCacheSelector<'tcx>)
1083-
cache_on_disk_if { true }
1083+
cache_on_disk_if { def_id.is_local() }
1084+
separate_provide_extern
10841085
}
10851086

10861087
query asm_target_features(def_id: DefId) -> &'tcx FxHashSet<Symbol> {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 23 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'tcx> InstanceDef<'tcx> {
246246
match *self {
247247
InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. })
248248
| InstanceDef::Virtual(def_id, _) => {
249-
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
249+
tcx.body_codegen_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
250250
}
251251
InstanceDef::ClosureOnceShim { call_once: _, track_caller } => track_caller,
252252
_ => false,

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::ty::{self, subst::SubstsRef, ReprOptions, Ty, TyCtxt, TypeFoldable};
66
use rustc_ast as ast;
77
use rustc_attr as attr;
88
use rustc_hir as hir;
9+
use rustc_hir::def_id::DefId;
910
use rustc_hir::lang_items::LangItem;
1011
use rustc_index::bit_set::BitSet;
1112
use rustc_index::vec::{Idx, IndexVec};
@@ -2762,14 +2763,22 @@ impl<'tcx> ty::Instance<'tcx> {
27622763
/// with `-Cpanic=abort` will look like they can't unwind when in fact they
27632764
/// might (from a foreign exception or similar).
27642765
#[inline]
2765-
pub fn fn_can_unwind<'tcx>(
2766-
tcx: TyCtxt<'tcx>,
2767-
codegen_fn_attr_flags: CodegenFnAttrFlags,
2768-
abi: SpecAbi,
2769-
) -> bool {
2770-
// Special attribute for functions which can't unwind.
2771-
if codegen_fn_attr_flags.contains(CodegenFnAttrFlags::NEVER_UNWIND) {
2772-
return false;
2766+
pub fn fn_can_unwind<'tcx>(tcx: TyCtxt<'tcx>, fn_def_id: Option<DefId>, abi: SpecAbi) -> bool {
2767+
if let Some(did) = fn_def_id {
2768+
// Special attribute for functions which can't unwind.
2769+
if tcx.codegen_fn_attrs(did).flags.contains(CodegenFnAttrFlags::NEVER_UNWIND) {
2770+
return false;
2771+
}
2772+
2773+
// With -Z panic-in-drop=abort, drop_in_place never unwinds.
2774+
//
2775+
// This is not part of `codegen_fn_attrs` as it can differ between crates
2776+
// and therefore cannot be computed in core.
2777+
if tcx.sess.opts.debugging_opts.panic_in_drop == PanicStrategy::Abort {
2778+
if Some(did) == tcx.lang_items().drop_in_place_fn() {
2779+
return false;
2780+
}
2781+
}
27732782
}
27742783

27752784
// Otherwise if this isn't special then unwinding is generally determined by
@@ -2991,13 +3000,7 @@ fn fn_abi_of_fn_ptr<'tcx>(
29913000
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, FnAbiError<'tcx>> {
29923001
let (param_env, (sig, extra_args)) = query.into_parts();
29933002

2994-
LayoutCx { tcx, param_env }.fn_abi_new_uncached(
2995-
sig,
2996-
extra_args,
2997-
None,
2998-
CodegenFnAttrFlags::empty(),
2999-
false,
3000-
)
3003+
LayoutCx { tcx, param_env }.fn_abi_new_uncached(sig, extra_args, None, None, false)
30013004
}
30023005

30033006
fn fn_abi_of_instance<'tcx>(
@@ -3014,13 +3017,11 @@ fn fn_abi_of_instance<'tcx>(
30143017
None
30153018
};
30163019

3017-
let attrs = tcx.codegen_fn_attrs(instance.def_id()).flags;
3018-
30193020
LayoutCx { tcx, param_env }.fn_abi_new_uncached(
30203021
sig,
30213022
extra_args,
30223023
caller_location,
3023-
attrs,
3024+
Some(instance.def_id()),
30243025
matches!(instance.def, ty::InstanceDef::Virtual(..)),
30253026
)
30263027
}
@@ -3033,7 +3034,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
30333034
sig: ty::PolyFnSig<'tcx>,
30343035
extra_args: &[Ty<'tcx>],
30353036
caller_location: Option<Ty<'tcx>>,
3036-
codegen_fn_attr_flags: CodegenFnAttrFlags,
3037+
fn_def_id: Option<DefId>,
30373038
// FIXME(eddyb) replace this with something typed, like an `enum`.
30383039
force_thin_self_ptr: bool,
30393040
) -> Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, FnAbiError<'tcx>> {
@@ -3205,7 +3206,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
32053206
c_variadic: sig.c_variadic,
32063207
fixed_count: inputs.len(),
32073208
conv,
3208-
can_unwind: fn_can_unwind(self.tcx(), codegen_fn_attr_flags, sig.abi),
3209+
can_unwind: fn_can_unwind(self.tcx(), fn_def_id, sig.abi),
32093210
};
32103211
self.fn_abi_adjust_for_abi(&mut fn_abi, sig.abi)?;
32113212
debug!("fn_abi_new_uncached = {:?}", fn_abi);

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct UnsafetyVisitor<'a, 'tcx> {
2727
body_unsafety: BodyUnsafety,
2828
/// The `#[target_feature]` attributes of the body. Used for checking
2929
/// calls to functions with `#[target_feature]` (RFC 2396).
30-
body_target_features: &'tcx Vec<Symbol>,
30+
body_target_features: &'tcx [Symbol],
3131
/// When inside the LHS of an assignment to a field, this is the type
3232
/// of the LHS and the span of the assignment expression.
3333
assignment_info: Option<(Ty<'tcx>, Span)>,
@@ -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,7 +660,7 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalD
661660
BodyUnsafety::Safe
662661
}
663662
});
664-
let body_target_features = &tcx.codegen_fn_attrs(def.did).target_features;
663+
let body_target_features = &tcx.body_codegen_attrs(def.did.to_def_id()).target_features;
665664
let safety_context =
666665
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
667666
let mut visitor = UnsafetyVisitor {

0 commit comments

Comments
 (0)