Skip to content

Commit e64f523

Browse files
committed
Evaluate all mir.required_consts and report any errors
Fixes #981
1 parent a80d642 commit e64f523

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ pub(crate) fn verify_func(tcx: TyCtxt<'_>, writer: &crate::pretty_clif::CommentW
157157
}
158158

159159
fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
160+
crate::constant::check_constants(fx);
161+
160162
for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
161163
let block = fx.get_block(bb);
162164
fx.bcx.switch_to_block(block);

src/constant.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use rustc_span::DUMMY_SP;
22

3+
use rustc_errors::ErrorReported;
34
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
45
use rustc_middle::mir::interpret::{
5-
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, Pointer, Scalar,
6+
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer, Scalar,
67
};
78
use rustc_middle::ty::{Const, ConstKind};
89
use rustc_target::abi::Align;
@@ -34,6 +35,29 @@ impl ConstantCx {
3435
}
3536
}
3637

38+
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Backend>) {
39+
for constant in &fx.mir.required_consts {
40+
let const_ = fx.monomorphize(&constant.literal);
41+
match const_.val {
42+
ConstKind::Value(_) => {}
43+
ConstKind::Unevaluated(def, ref substs, promoted) => {
44+
if let Err(err) = fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) {
45+
match err {
46+
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {
47+
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
48+
}
49+
ErrorHandled::TooGeneric => {
50+
span_bug!(constant.span, "codgen encountered polymorphic constant: {:?}", err);
51+
}
52+
}
53+
}
54+
}
55+
ConstKind::Param(_) | ConstKind::Infer(_) | ConstKind::Bound(_, _)
56+
| ConstKind::Placeholder(_) | ConstKind::Error(_) => unreachable!("{:?}", const_),
57+
}
58+
}
59+
}
60+
3761
pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
3862
constants_cx.todo.push(TodoItem::Static(def_id));
3963
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
extern crate flate2;
66
#[cfg(feature = "jit")]
77
extern crate libc;
8+
#[macro_use]
89
extern crate rustc_middle;
910
extern crate rustc_codegen_ssa;
1011
extern crate rustc_data_structures;

0 commit comments

Comments
 (0)