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