Skip to content

Commit 3113fef

Browse files
committed
move required_consts check to general post-mono-check function
1 parent 90d894e commit 3113fef

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

src/base.rs

Lines changed: 16 additions & 6 deletions
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

src/constant.rs

Lines changed: 7 additions & 29 deletions
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

Lines changed: 1 addition & 2 deletions
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)