Skip to content

Commit 48dc828

Browse files
committed
Dispatch MIR pipeline by BodyOwnerKind instead of const context
1 parent 68ffae4 commit 48dc828

3 files changed

Lines changed: 44 additions & 11 deletions

File tree

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ run-pass
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/155803
4+
5+
#![feature(const_closures, const_trait_impl)]
6+
7+
const F: fn() -> i32 = const || 42;
8+
9+
fn main() {
10+
assert_eq!(F(), 42);
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ build-pass
2+
//@ compile-flags: -Clink-dead-code=true
3+
4+
// Regression test for https://github.com/rust-lang/rust/issues/155803
5+
6+
#![feature(const_closures, const_trait_impl)]
7+
8+
const _: () = {
9+
assert!((const || true)());
10+
};
11+
12+
fn main() {}

0 commit comments

Comments
 (0)