Skip to content

Commit 55bfcb8

Browse files
committed
Only call mir_const_qualif if absolutely necessary
1 parent 2101271 commit 55bfcb8

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate tracing;
1919
#[macro_use]
2020
extern crate rustc_middle;
2121

22+
use hir::ConstContext;
2223
use required_consts::RequiredConstsVisitor;
2324
use rustc_const_eval::util;
2425
use rustc_data_structures::fx::FxIndexSet;
@@ -231,8 +232,12 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
231232
let const_kind = tcx.hir().body_const_context(def);
232233

233234
// No need to const-check a non-const `fn`.
234-
if const_kind.is_none() {
235-
return Default::default();
235+
match const_kind {
236+
Some(ConstContext::Const | ConstContext::Static(_)) | Some(ConstContext::ConstFn) => {}
237+
None => span_bug!(
238+
tcx.def_span(def),
239+
"`mir_const_qualif` should only be called on const fns and const items"
240+
),
236241
}
237242

238243
// N.B., this `borrow()` is guaranteed to be valid (i.e., the value
@@ -297,7 +302,21 @@ fn mir_promoted(
297302
// Ensure that we compute the `mir_const_qualif` for constants at
298303
// this point, before we steal the mir-const result.
299304
// Also this means promotion can rely on all const checks having been done.
300-
let const_qualifs = tcx.mir_const_qualif(def);
305+
306+
let const_qualifs = match tcx.def_kind(def) {
307+
DefKind::Fn | DefKind::AssocFn | DefKind::Closure
308+
if tcx.constness(def) == hir::Constness::Const
309+
|| tcx.is_const_default_method(def.to_def_id()) =>
310+
{
311+
tcx.mir_const_qualif(def)
312+
}
313+
DefKind::AssocConst
314+
| DefKind::Const
315+
| DefKind::Static(_)
316+
| DefKind::InlineConst
317+
| DefKind::AnonConst => tcx.mir_const_qualif(def),
318+
_ => ConstQualifs::default(),
319+
};
301320
let mut body = tcx.mir_const(def).steal();
302321
if let Some(error_reported) = const_qualifs.tainted_by_errors {
303322
body.tainted_by_errors = Some(error_reported);

0 commit comments

Comments
 (0)