Skip to content

Commit 46b55ae

Browse files
committed
Auto merge of #115748 - RalfJung:post-mono, r=oli-obk
move required_consts check to general post-mono-check function This factors some code that is common between the interpreter and the codegen backends into shared helper functions. Also as a side-effect the interpreter now uses the same `eval` functions as everyone else to get the evaluated MIR constants. Also this is in preparation for another post-mono check that will be needed for (the current hackfix for) rust-lang/rust#115709: ensuring that all locals are dynamically sized. I didn't expect this to change diagnostics, but it's just cycle errors that change. r? `@oli-obk`
2 parents f9f8bff + b7cc765 commit 46b55ae

File tree

3 files changed

+12
-32
lines changed

3 files changed

+12
-32
lines changed

src/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ pub(crate) fn verify_func(
250250
}
251251

252252
fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
253-
if !crate::constant::check_constants(fx) {
253+
if let Err(err) =
254+
fx.mir.post_mono_checks(fx.tcx, ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c)))
255+
{
256+
err.emit_err(fx.tcx);
254257
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
255258
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
256259
// compilation should have been aborted

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.

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,

0 commit comments

Comments
 (0)