Skip to content

Commit 35eefc1

Browse files
committed
Close up a loophole
1 parent 1bd8557 commit 35eefc1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,10 +1791,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17911791
record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses);
17921792
}
17931793
}
1794+
let mut is_trivial = false;
17941795
if encode_const {
17951796
if let Some((val, ty)) = tcx.is_trivial_const(def_id) {
1797+
is_trivial = true;
17961798
record!(self.tables.is_trivial_const[def_id.to_def_id()] <- (val, ty));
17971799
} else {
1800+
is_trivial = false;
17981801
record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id));
17991802
}
18001803

@@ -1814,7 +1817,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18141817
}
18151818
}
18161819
}
1817-
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
1820+
if !is_trivial {
1821+
record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id));
1822+
}
18181823

18191824
if self.tcx.is_coroutine(def_id.to_def_id())
18201825
&& let Some(witnesses) = tcx.mir_coroutine_witnesses(def_id)

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ fn mir_promoted(
452452
tcx: TyCtxt<'_>,
453453
def: LocalDefId,
454454
) -> (&Steal<Body<'_>>, &Steal<IndexVec<Promoted, Body<'_>>>) {
455+
if tcx.is_trivial_const(def).is_some() {
456+
panic!("Tried to get MIR of a trivial const");
457+
}
458+
455459
// Ensure that we compute the `mir_const_qualif` for constants at
456460
// this point, before we steal the mir-const result.
457461
// Also this means promotion can rely on all const checks having been done.
@@ -479,7 +483,9 @@ fn mir_promoted(
479483
tcx.ensure_done().coroutine_by_move_body_def_id(def);
480484
}
481485

486+
// the `is_trivial_const` uses mir_built, so make sure it is run.
482487
tcx.ensure_done().is_trivial_const(def);
488+
483489
let mut body = tcx.mir_built(def).steal();
484490
if let Some(error_reported) = const_qualifs.tainted_by_errors {
485491
body.tainted_by_errors = Some(error_reported);
@@ -542,9 +548,6 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
542548
/// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't
543549
/// end up missing the source MIR due to stealing happening.
544550
fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
545-
if tcx.is_trivial_const(def).is_some() {
546-
panic!("Tried to get mir_for_ctfe of a trivial const");
547-
}
548551
if tcx.is_coroutine(def.to_def_id()) {
549552
tcx.ensure_done().mir_coroutine_witnesses(def);
550553
}

0 commit comments

Comments
 (0)