Skip to content

Commit f362387

Browse files
committed
Auto merge of #114502 - cjgillot:steal-ctfe, r=oli-obk
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.
2 parents 85fbb57 + 02e10a0 commit f362387

File tree

1 file changed

+8
-1
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+8
-1
lines changed

compiler/rustc_mir_transform/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,14 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
338338
return shim::build_adt_ctor(tcx, def.to_def_id());
339339
}
340340

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

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

0 commit comments

Comments
 (0)