Skip to content

Commit 4c57746

Browse files
committed
add comments explaining where post-mono const eval errors abort compilation
1 parent 39db6a0 commit 4c57746

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,16 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
814814
self.super_rvalue(rvalue, location);
815815
}
816816

817-
/// This does not walk the constant, as it has been handled entirely here and trying
818-
/// to walk it would attempt to evaluate the `ty::Const` inside, which doesn't necessarily
819-
/// work, as some constants cannot be represented in the type system.
817+
/// This does not walk the MIR of the constant as that is not needed for codegen, all we need is
818+
/// to ensure that the constant evaluates successfully and walk the result.
820819
#[instrument(skip(self), level = "debug")]
821820
fn visit_constant(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
822821
let const_ = self.monomorphize(constant.const_);
823822
let param_env = ty::ParamEnv::reveal_all();
823+
// Evaluate the constant. This makes const eval failure a collection-time error (rather than
824+
// a codegen-time error). rustc stops after collection if there was an error, so this
825+
// ensures codegen never has to worry about failing consts.
826+
// (codegen relies on this and ICEs will happen if this is violated.)
824827
let val = match const_.eval(self.tcx, param_env, None) {
825828
Ok(v) => v,
826829
Err(ErrorHandled::Reported(..)) => return,

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,9 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co
11141114

11151115
let (items, usage_map) = collector::collect_crate_mono_items(tcx, collection_mode);
11161116

1117+
// If there was an error during collection (e.g. from one of the constants we evaluated),
1118+
// then we stop here. This way codegen does not have to worry about failing constants.
1119+
// (codegen relies on this and ICEs will happen if this is violated.)
11171120
tcx.dcx().abort_if_errors();
11181121

11191122
let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {

0 commit comments

Comments
 (0)