Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit eb4e94b

Browse files
committed
Simplify the optimize_mir query
1 parent 1f5fb3e commit eb4e94b

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
lines changed

compiler/rustc_middle/src/mir/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'tcx> TyCtxt<'tcx> {
439439
}
440440

441441
#[inline]
442-
pub fn optimized_mir_opt_const_arg(
442+
pub fn optimized_mir_or_const_arg_mir(
443443
self,
444444
def: ty::WithOptConstParam<DefId>,
445445
) -> &'tcx Body<'tcx> {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3018,7 +3018,7 @@ impl<'tcx> TyCtxt<'tcx> {
30183018
| DefKind::AnonConst => self.mir_for_ctfe_opt_const_arg(def),
30193019
// If the caller wants `mir_for_ctfe` they should not be using `instance_mir`, so
30203020
// we'll assume const fn also wants the optimized version.
3021-
_ => self.optimized_mir_opt_const_arg(def),
3021+
_ => self.optimized_mir_or_const_arg_mir(def),
30223022
},
30233023
ty::InstanceDef::VtableShim(..)
30243024
| ty::InstanceDef::ReifyShim(..)

compiler/rustc_mir/src/transform/mod.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -517,34 +517,26 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
517517

518518
fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
519519
let did = did.expect_local();
520-
if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
521-
tcx.mir_for_ctfe_of_const_arg(def)
522-
} else {
523-
tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptConstParam::unknown(did)))
524-
}
520+
assert_eq!(ty::WithOptConstParam::try_lookup(did, tcx), None);
521+
tcx.arena.alloc(inner_optimized_mir(tcx, did))
525522
}
526523

527-
fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
528-
if tcx.is_constructor(def.did.to_def_id()) {
524+
fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
525+
if tcx.is_constructor(did.to_def_id()) {
529526
// There's no reason to run all of the MIR passes on constructors when
530527
// we can just output the MIR we want directly. This also saves const
531528
// qualification and borrow checking the trouble of special casing
532529
// constructors.
533-
return shim::build_adt_ctor(tcx, def.did.to_def_id());
530+
return shim::build_adt_ctor(tcx, did.to_def_id());
534531
}
535532

536-
match tcx.hir().body_const_context(def.did) {
537-
Some(hir::ConstContext::ConstFn) => {
538-
if let Some((did, param_did)) = def.to_global().as_const_arg() {
539-
tcx.ensure().mir_for_ctfe_of_const_arg((did, param_did))
540-
} else {
541-
tcx.ensure().mir_for_ctfe(def.did)
542-
}
543-
}
533+
match tcx.hir().body_const_context(did) {
534+
Some(hir::ConstContext::ConstFn) => tcx.ensure().mir_for_ctfe(did),
544535
None => {}
545536
Some(other) => panic!("do not use `optimized_mir` for constants: {:?}", other),
546537
}
547-
let mut body = tcx.mir_drops_elaborated_and_const_checked(def).steal();
538+
let mut body =
539+
tcx.mir_drops_elaborated_and_const_checked(ty::WithOptConstParam::unknown(did)).steal();
548540
run_optimization_passes(tcx, &mut body);
549541

550542
debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");

0 commit comments

Comments
 (0)