@@ -4,16 +4,21 @@ use rustc_hir::def_id::{DefId, LocalDefId};
44use rustc_middle:: query:: Providers ;
55use rustc_middle:: ty:: TyCtxt ;
66
7- fn is_parent_const_impl_raw ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> bool {
7+ fn parent_impl_constness ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> hir :: Constness {
88 let parent_id = tcx. local_parent ( def_id) ;
9- matches ! ( tcx. def_kind( parent_id) , DefKind :: Impl { .. } )
10- && tcx. constness ( parent_id) == hir:: Constness :: Const
9+ if matches ! ( tcx. def_kind( parent_id) , DefKind :: Impl { .. } )
10+ && let Some ( header) = tcx. impl_trait_header ( parent_id)
11+ {
12+ header. constness
13+ } else {
14+ hir:: Constness :: NotConst
15+ }
1116}
1217
13- /// Checks whether an item is considered to be `const`. If it is a constructor, anonymous const,
14- /// const block, const item or associated const, it is const. If it is a trait impl/ function,
18+ /// Checks whether an item is considered to be `const`. If it is a constructor, it is const.
19+ /// If it is an assoc method or function,
1520/// return if it has a `const` modifier. If it is an intrinsic, report whether said intrinsic
16- /// has a `rustc_const_{un,}stable` attribute. Otherwise, return `Constness::NotConst` .
21+ /// has a `rustc_const_{un,}stable` attribute. Otherwise, panic .
1722fn constness ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> hir:: Constness {
1823 let node = tcx. hir_node_by_def_id ( def_id) ;
1924
@@ -22,7 +27,6 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
2227 | hir:: Node :: ImplItem ( hir:: ImplItem { kind : hir:: ImplItemKind :: Const ( ..) , .. } ) => {
2328 hir:: Constness :: Const
2429 }
25- hir:: Node :: Item ( hir:: Item { kind : hir:: ItemKind :: Impl ( impl_) , .. } ) => impl_. constness ,
2630 hir:: Node :: ForeignItem ( _) => {
2731 // Foreign items cannot be evaluated at compile-time.
2832 hir:: Constness :: NotConst
@@ -36,8 +40,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3640
3741 // If the function itself is not annotated with `const`, it may still be a `const fn`
3842 // if it resides in a const trait impl.
39- let is_const = is_parent_const_impl_raw ( tcx, def_id) ;
40- if is_const { hir:: Constness :: Const } else { hir:: Constness :: NotConst }
43+ parent_impl_constness ( tcx, def_id)
4144 } else {
4245 tcx. dcx ( ) . span_bug (
4346 tcx. def_span ( def_id) ,
0 commit comments