@@ -357,28 +357,43 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -
357357 return shim:: build_adt_ctor ( tcx, def. did . to_def_id ( ) ) ;
358358 }
359359
360- assert_ne ! (
361- tcx. hir( ) . body_const_context( def. did) ,
362- None ,
363- "mir_for_ctfe should not be used for runtime functions"
364- ) ;
360+ let context = tcx
361+ . hir ( )
362+ . body_const_context ( def. did )
363+ . expect ( "mir_for_ctfe should not be used for runtime functions" ) ;
365364
366365 let mut body = tcx. mir_drops_elaborated_and_const_checked ( def) . borrow ( ) . clone ( ) ;
367366
368- #[ rustfmt:: skip]
369- let optimizations: & [ & dyn MirPass < ' _ > ] = & [
370- & const_prop:: ConstProp ,
371- ] ;
372-
373- #[ rustfmt:: skip]
374- run_passes (
375- tcx,
376- & mut body,
377- MirPhase :: Optimization ,
378- & [
379- optimizations,
380- ] ,
381- ) ;
367+ match context {
368+ // Do not const prop functions, either they get executed at runtime or exported to metadata,
369+ // so we run const prop on them, or they don't, in which case we const evaluate some control
370+ // flow paths of the function and any errors in those paths will get emitted as const eval
371+ // errors.
372+ hir:: ConstContext :: ConstFn => { }
373+ // Static items always get evaluated, so we can just let const eval see if any erroneous
374+ // control flow paths get executed.
375+ hir:: ConstContext :: Static ( _) => { }
376+ // Associated constants get const prop run so we detect common failure situations in the
377+ // crate that defined the constant.
378+ // Technically we want to not run on regular const items, but oli-obk doesn't know how to
379+ // conveniently detect that at this point without looking at the HIR.
380+ hir:: ConstContext :: Const => {
381+ #[ rustfmt:: skip]
382+ let optimizations: & [ & dyn MirPass < ' _ > ] = & [
383+ & const_prop:: ConstProp ,
384+ ] ;
385+
386+ #[ rustfmt:: skip]
387+ run_passes (
388+ tcx,
389+ & mut body,
390+ MirPhase :: Optimization ,
391+ & [
392+ optimizations,
393+ ] ,
394+ ) ;
395+ }
396+ }
382397
383398 debug_assert ! ( !body. has_free_regions( ) , "Free regions in MIR for CTFE" ) ;
384399
0 commit comments