@@ -502,12 +502,16 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
502502 }
503503
504504 let body = tcx. mir_drops_elaborated_and_const_checked ( def) ;
505- let body = match tcx. hir_body_const_context ( def) {
505+ let body = match tcx. hir_body_owner_kind ( def) {
506506 // consts and statics do not have `optimized_mir`, so we can steal the body instead of
507507 // cloning it.
508- Some ( hir:: ConstContext :: Const { .. } | hir:: ConstContext :: Static ( _) ) => body. steal ( ) ,
509- Some ( hir:: ConstContext :: ConstFn ) => body. borrow ( ) . clone ( ) ,
510- None => bug ! ( "`mir_for_ctfe` called on non-const {def:?}" ) ,
508+ hir:: BodyOwnerKind :: Const { .. } | hir:: BodyOwnerKind :: Static ( _) => body. steal ( ) ,
509+ hir:: BodyOwnerKind :: Fn | hir:: BodyOwnerKind :: Closure
510+ if tcx. is_const_fn ( def. to_def_id ( ) ) =>
511+ {
512+ body. borrow ( ) . clone ( )
513+ }
514+ kind => bug ! ( "`mir_for_ctfe` called on non-const {kind:?}: {def:?}" ) ,
511515 } ;
512516
513517 let mut body = remap_mir_for_const_eval_select ( tcx, body, hir:: Constness :: Const ) ;
@@ -797,13 +801,19 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
797801 return shim:: build_adt_ctor ( tcx, did. to_def_id ( ) ) ;
798802 }
799803
800- match tcx. hir_body_const_context ( did) {
801- // Run the `mir_for_ctfe` query, which depends on `mir_drops_elaborated_and_const_checked`
802- // which we are going to steal below. Thus we need to run `mir_for_ctfe` first, so it
803- // computes and caches its result.
804- Some ( hir:: ConstContext :: ConstFn ) => tcx. ensure_done ( ) . mir_for_ctfe ( did) ,
805- None => { }
806- Some ( other) => panic ! ( "do not use `optimized_mir` for constants: {other:?}" ) ,
804+ match tcx. hir_body_owner_kind ( did) {
805+ hir:: BodyOwnerKind :: Fn | hir:: BodyOwnerKind :: Closure
806+ if tcx. is_const_fn ( did. to_def_id ( ) ) =>
807+ {
808+ // Run the `mir_for_ctfe` query, which depends on `mir_drops_elaborated_and_const_checked`
809+ // which we are going to steal below. Thus we need to run `mir_for_ctfe` first, so it
810+ // computes and caches its result.
811+ tcx. ensure_done ( ) . mir_for_ctfe ( did) ;
812+ }
813+ hir:: BodyOwnerKind :: Fn | hir:: BodyOwnerKind :: Closure | hir:: BodyOwnerKind :: GlobalAsm => { }
814+ kind @ ( hir:: BodyOwnerKind :: Const { .. } | hir:: BodyOwnerKind :: Static ( _) ) => {
815+ bug ! ( "do not use `optimized_mir` for {kind:?}: {did:?}" )
816+ }
807817 }
808818 debug ! ( "about to call mir_drops_elaborated..." ) ;
809819 let body = tcx. mir_drops_elaborated_and_const_checked ( did) . steal ( ) ;
0 commit comments