Skip to content

Commit

Permalink
Auto merge of rust-lang#114502 - cjgillot:steal-ctfe, r=oli-obk
Browse files Browse the repository at this point in the history
Steal MIR for CTFE when possible.

Some bodies, like constants, have CTFE MIR but no optimized MIR.
In that case, have `mir_for_ctfe` steal the MIR instead of cloning it.
  • Loading branch information
bors committed Aug 6, 2023
2 parents 85fbb57 + 02e10a0 commit f362387
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,14 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
return shim::build_adt_ctor(tcx, def.to_def_id());
}

let body = tcx.mir_drops_elaborated_and_const_checked(def).borrow().clone();
let body = tcx.mir_drops_elaborated_and_const_checked(def);
let body = match tcx.hir().body_const_context(def) {
// consts and statics do not have `optimized_mir`, so we can steal the body instead of
// cloning it.
Some(hir::ConstContext::Const | hir::ConstContext::Static(_)) => body.steal(),
Some(hir::ConstContext::ConstFn) => body.borrow().clone(),
None => bug!("`mir_for_ctfe` called on non-const {def:?}"),
};

let mut body = remap_mir_for_const_eval_select(tcx, body, hir::Constness::Const);
pm::run_passes(tcx, &mut body, &[&ctfe_limit::CtfeLimit], None);
Expand Down

0 comments on commit f362387

Please sign in to comment.