@@ -4,10 +4,15 @@ 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
1318/// Checks whether an item is considered to be `const`. If it is a constructor, anonymous const,
@@ -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