Skip to content

Commit 9e251f9

Browse files
committed
Auto merge of #117166 - oli-obk:mir_const_qualif_perf, r=<try>
Only call `mir_const_qualif` if absolutely necessary Pull the perf change out of #113617 This should not have any impact on behaviour (if it does, we'll see an ICE)
2 parents eb03d40 + c6ca2b2 commit 9e251f9

File tree

1 file changed

+22
-3
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+22
-3
lines changed

compiler/rustc_mir_transform/src/lib.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern crate tracing;
2020
#[macro_use]
2121
extern crate rustc_middle;
2222

23+
use hir::ConstContext;
2324
use required_consts::RequiredConstsVisitor;
2425
use rustc_const_eval::util;
2526
use rustc_data_structures::fx::FxIndexSet;
@@ -251,8 +252,12 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
251252
let const_kind = tcx.hir().body_const_context(def);
252253

253254
// No need to const-check a non-const `fn`.
254-
if const_kind.is_none() {
255-
return Default::default();
255+
match const_kind {
256+
Some(ConstContext::Const | ConstContext::Static(_)) | Some(ConstContext::ConstFn) => {}
257+
None => span_bug!(
258+
tcx.def_span(def),
259+
"`mir_const_qualif` should only be called on const fns and const items"
260+
),
256261
}
257262

258263
// N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -317,7 +322,21 @@ fn mir_promoted(
317322
// Ensure that we compute the `mir_const_qualif` for constants at
318323
// this point, before we steal the mir-const result.
319324
// Also this means promotion can rely on all const checks having been done.
320-
let const_qualifs = tcx.mir_const_qualif(def);
325+
326+
let const_qualifs = match tcx.def_kind(def) {
327+
DefKind::Fn | DefKind::AssocFn | DefKind::Closure
328+
if tcx.constness(def) == hir::Constness::Const
329+
|| tcx.is_const_default_method(def.to_def_id()) =>
330+
{
331+
tcx.mir_const_qualif(def)
332+
}
333+
DefKind::AssocConst
334+
| DefKind::Const
335+
| DefKind::Static(_)
336+
| DefKind::InlineConst
337+
| DefKind::AnonConst => tcx.mir_const_qualif(def),
338+
_ => ConstQualifs::default(),
339+
};
321340
let mut body = tcx.mir_const(def).steal();
322341
if let Some(error_reported) = const_qualifs.tainted_by_errors {
323342
body.tainted_by_errors = Some(error_reported);

0 commit comments

Comments
 (0)