Skip to content

Commit 89ac57d

Browse files
committed
move required_consts check to general post-mono-check function
1 parent ccf817b commit 89ac57d

40 files changed

+319
-257
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/base.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use rustc_ast::InlineAsmOptions;
44
use rustc_index::IndexVec;
5+
use rustc_middle::mir::interpret::ErrorHandled;
56
use rustc_middle::ty::adjustment::PointerCoercion;
67
use rustc_middle::ty::layout::FnAbiOf;
78
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -250,12 +251,21 @@ pub(crate) fn verify_func(
250251
}
251252

252253
fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
253-
if !crate::constant::check_constants(fx) {
254-
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
255-
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
256-
// compilation should have been aborted
257-
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
258-
return;
254+
match fx.mir.post_mono_checks(fx.tcx, ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c))) {
255+
Ok(()) => {}
256+
Err(ErrorHandled::TooGeneric(span)) => {
257+
span_bug!(span, "codegen encountered polymorphic constant");
258+
}
259+
Err(ErrorHandled::Reported(info, span)) => {
260+
if !info.is_tainted_by_errors() {
261+
fx.tcx.sess.span_err(span, "erroneous constant encountered");
262+
}
263+
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
264+
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
265+
// compilation should have been aborted
266+
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
267+
return;
268+
}
259269
}
260270

261271
let arg_uninhabited = fx

Diff for: compiler/rustc_codegen_cranelift/src/constant.rs

+7-29
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
5-
use rustc_middle::mir::interpret::{
6-
read_target_uint, AllocId, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
7-
};
5+
use rustc_middle::mir::interpret::{read_target_uint, AllocId, ConstValue, GlobalAlloc, Scalar};
86

97
use cranelift_module::*;
108

@@ -33,16 +31,6 @@ impl ConstantCx {
3331
}
3432
}
3533

36-
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
37-
let mut all_constants_ok = true;
38-
for constant in &fx.mir.required_consts {
39-
if eval_mir_constant(fx, constant).is_none() {
40-
all_constants_ok = false;
41-
}
42-
}
43-
all_constants_ok
44-
}
45-
4634
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
4735
let mut constants_cx = ConstantCx::new();
4836
constants_cx.todo.push(TodoItem::Static(def_id));
@@ -76,30 +64,20 @@ pub(crate) fn codegen_tls_ref<'tcx>(
7664
pub(crate) fn eval_mir_constant<'tcx>(
7765
fx: &FunctionCx<'_, '_, 'tcx>,
7866
constant: &Constant<'tcx>,
79-
) -> Option<(ConstValue<'tcx>, Ty<'tcx>)> {
67+
) -> (ConstValue<'tcx>, Ty<'tcx>) {
8068
let cv = fx.monomorphize(constant.literal);
69+
// This cannot fail because we checked all required_consts in advance.
8170
let val = cv
8271
.eval(fx.tcx, ty::ParamEnv::reveal_all(), Some(constant.span))
83-
.map_err(|err| match err {
84-
ErrorHandled::Reported(_) => {
85-
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
86-
}
87-
ErrorHandled::TooGeneric => {
88-
span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
89-
}
90-
})
91-
.ok();
92-
val.map(|val| (val, cv.ty()))
72+
.expect("erroneous constant not captured by required_consts");
73+
(val, cv.ty())
9374
}
9475

9576
pub(crate) fn codegen_constant_operand<'tcx>(
9677
fx: &mut FunctionCx<'_, '_, 'tcx>,
9778
constant: &Constant<'tcx>,
9879
) -> CValue<'tcx> {
99-
let (const_val, ty) = eval_mir_constant(fx, constant).unwrap_or_else(|| {
100-
span_bug!(constant.span, "erroneous constant not captured by required_consts")
101-
});
102-
80+
let (const_val, ty) = eval_mir_constant(fx, constant);
10381
codegen_const_value(fx, const_val, ty)
10482
}
10583

@@ -459,7 +437,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
459437
operand: &Operand<'tcx>,
460438
) -> Option<ConstValue<'tcx>> {
461439
match operand {
462-
Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).unwrap().0),
440+
Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).0),
463441
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
464442
// inside a temporary before being passed to the intrinsic requiring the const argument.
465443
// This code tries to find a single constant defining definition of the referenced local.

Diff for: compiler/rustc_codegen_cranelift/src/inline_asm.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ pub(crate) fn codegen_inline_asm<'tcx>(
242242
}
243243
}
244244
InlineAsmOperand::Const { ref value } => {
245-
let (const_value, ty) = crate::constant::eval_mir_constant(fx, value)
246-
.unwrap_or_else(|| span_bug!(span, "asm const cannot be resolved"));
245+
let (const_value, ty) = crate::constant::eval_mir_constant(fx, value);
247246
let value = rustc_codegen_ssa::common::asm_const_to_str(
248247
fx.tcx,
249248
span,

Diff for: compiler/rustc_codegen_ssa/src/mir/block.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1088,9 +1088,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10881088
InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
10891089
}
10901090
mir::InlineAsmOperand::Const { ref value } => {
1091-
let const_value = self
1092-
.eval_mir_constant(value)
1093-
.unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
1091+
let const_value = self.eval_mir_constant(value);
10941092
let string = common::asm_const_to_str(
10951093
bx.tcx(),
10961094
span,

Diff for: compiler/rustc_codegen_ssa/src/mir/constant.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1414
&self,
1515
bx: &mut Bx,
1616
constant: &mir::Constant<'tcx>,
17-
) -> Result<OperandRef<'tcx, Bx::Value>, ErrorHandled> {
18-
let val = self.eval_mir_constant(constant)?;
17+
) -> OperandRef<'tcx, Bx::Value> {
18+
let val = self.eval_mir_constant(constant);
1919
let ty = self.monomorphize(constant.ty());
20-
Ok(OperandRef::from_const(bx, val, ty))
20+
OperandRef::from_const(bx, val, ty)
2121
}
2222

23-
pub fn eval_mir_constant(
24-
&self,
25-
constant: &mir::Constant<'tcx>,
26-
) -> Result<ConstValue<'tcx>, ErrorHandled> {
23+
pub fn eval_mir_constant(&self, constant: &mir::Constant<'tcx>) -> ConstValue<'tcx> {
2724
self.monomorphize(constant.literal)
2825
.eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), Some(constant.span))
29-
.map_err(|err| {
30-
match err {
31-
ErrorHandled::Reported(_) => {
32-
self.cx
33-
.tcx()
34-
.sess
35-
.emit_err(errors::ErroneousConstant { span: constant.span });
36-
}
37-
ErrorHandled::TooGeneric => {
38-
self.cx.tcx().sess.diagnostic().emit_bug(
39-
errors::PolymorphicConstantTooGeneric { span: constant.span },
40-
);
41-
}
42-
}
43-
err
44-
})
26+
.expect("erroneous constant not captured by required_consts")
4527
}
4628

4729
/// This is a convenience helper for `simd_shuffle_indices`. It has the precondition

Diff for: compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -579,23 +579,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
579579
if let Some(dbg_var) = dbg_var {
580580
let Some(dbg_loc) = self.dbg_loc(var.source_info) else { continue };
581581

582-
if let Ok(operand) = self.eval_mir_constant_to_operand(bx, &c) {
583-
self.set_debug_loc(bx, var.source_info);
584-
let base = Self::spill_operand_to_stack(
585-
operand,
586-
Some(var.name.to_string()),
587-
bx,
588-
);
589-
590-
bx.dbg_var_addr(
591-
dbg_var,
592-
dbg_loc,
593-
base.llval,
594-
Size::ZERO,
595-
&[],
596-
fragment,
597-
);
598-
}
582+
let operand = self.eval_mir_constant_to_operand(bx, &c);
583+
self.set_debug_loc(bx, var.source_info);
584+
let base =
585+
Self::spill_operand_to_stack(operand, Some(var.name.to_string()), bx);
586+
587+
bx.dbg_var_addr(dbg_var, dbg_loc, base.llval, Size::ZERO, &[], fragment);
599588
}
600589
}
601590
}

Diff for: compiler/rustc_codegen_ssa/src/mir/mod.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::base;
2+
use crate::errors;
23
use crate::traits::*;
34
use rustc_index::bit_set::BitSet;
45
use rustc_index::IndexVec;
@@ -212,25 +213,22 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
212213

213214
fx.per_local_var_debug_info = fx.compute_per_local_var_debug_info(&mut start_bx);
214215

215-
// Evaluate all required consts; codegen later assumes that CTFE will never fail.
216-
let mut all_consts_ok = true;
217-
for const_ in &mir.required_consts {
218-
if let Err(err) = fx.eval_mir_constant(const_) {
219-
all_consts_ok = false;
220-
match err {
221-
// errored or at least linted
222-
ErrorHandled::Reported(_) => {}
223-
ErrorHandled::TooGeneric => {
224-
span_bug!(const_.span, "codegen encountered polymorphic constant: {:?}", err)
225-
}
216+
// Rust post-monomorphization checks; we later rely on them.
217+
match mir.post_mono_checks(cx.tcx(), ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c))) {
218+
Ok(()) => {}
219+
Err(ErrorHandled::TooGeneric(span)) => {
220+
cx.tcx().sess.diagnostic().emit_bug(errors::PolymorphicConstantTooGeneric { span });
221+
}
222+
Err(ErrorHandled::Reported(info, span)) => {
223+
if !info.is_tainted_by_errors() {
224+
cx.tcx().sess.emit_err(errors::ErroneousConstant { span });
226225
}
226+
// This IR shouldn't ever be emitted, but let's try to guard against any of this code
227+
// ever running.
228+
start_bx.abort();
229+
return;
227230
}
228231
}
229-
if !all_consts_ok {
230-
// We leave the IR in some half-built state here, and rely on this code not even being
231-
// submitted to LLVM once an error was raised.
232-
return;
233-
}
234232

235233
let memory_locals = analyze::non_ssa_locals(&fx);
236234

Diff for: compiler/rustc_codegen_ssa/src/mir/operand.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
575575
self.codegen_consume(bx, place.as_ref())
576576
}
577577

578-
mir::Operand::Constant(ref constant) => {
579-
// This cannot fail because we checked all required_consts in advance.
580-
self.eval_mir_constant_to_operand(bx, constant).unwrap_or_else(|_err| {
581-
span_bug!(constant.span, "erroneous constant not captured by required_consts")
582-
})
583-
}
578+
mir::Operand::Constant(ref constant) => self.eval_mir_constant_to_operand(bx, constant),
584579
}
585580
}
586581
}

Diff for: compiler/rustc_const_eval/src/const_eval/error.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::{DiagnosticArgValue, DiagnosticMessage, IntoDiagnostic, IntoDi
44
use rustc_middle::mir::AssertKind;
55
use rustc_middle::ty::TyCtxt;
66
use rustc_middle::ty::{layout::LayoutError, ConstInt};
7-
use rustc_span::{ErrorGuaranteed, Span, Symbol};
7+
use rustc_span::{ErrorGuaranteed, Span, Symbol, DUMMY_SP};
88

99
use super::InterpCx;
1010
use crate::errors::{self, FrameNote, ReportErrorExt};
@@ -134,11 +134,11 @@ where
134134
// Don't emit a new diagnostic for these errors, they are already reported elsewhere or
135135
// should remain silent.
136136
err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => {
137-
ErrorHandled::TooGeneric
137+
ErrorHandled::TooGeneric(span.unwrap_or(DUMMY_SP))
138138
}
139-
err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar),
139+
err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar, span.unwrap_or(DUMMY_SP)),
140140
err_inval!(Layout(LayoutError::ReferencesError(guar))) => {
141-
ErrorHandled::Reported(guar.into())
141+
ErrorHandled::Reported(guar.into(), span.unwrap_or(DUMMY_SP))
142142
}
143143
// Report remaining errors.
144144
_ => {
@@ -152,7 +152,7 @@ where
152152

153153
// Use *our* span to label the interp error
154154
err.span_label(our_span, msg);
155-
ErrorHandled::Reported(err.emit().into())
155+
ErrorHandled::Reported(err.emit().into(), span)
156156
}
157157
}
158158
}

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
212212
key.param_env = key.param_env.with_user_facing();
213213
match tcx.eval_to_const_value_raw(key) {
214214
// try again with reveal all as requested
215-
Err(ErrorHandled::TooGeneric) => {}
215+
Err(ErrorHandled::TooGeneric(_)) => {}
216216
// deduplicate calls
217217
other => return other,
218218
}
@@ -259,7 +259,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
259259
key.param_env = key.param_env.with_user_facing();
260260
match tcx.eval_to_allocation_raw(key) {
261261
// try again with reveal all as requested
262-
Err(ErrorHandled::TooGeneric) => {}
262+
Err(ErrorHandled::TooGeneric(_)) => {}
263263
// deduplicate calls
264264
other => return other,
265265
}

0 commit comments

Comments
 (0)