Evaluate all mir.required_consts and report any errors

Fixes #981
This commit is contained in:
bjorn3 2020-08-20 16:51:01 +02:00
parent a80d6423e6
commit e64f523b27
3 changed files with 28 additions and 1 deletions

View File

@ -157,6 +157,8 @@ pub(crate) fn verify_func(tcx: TyCtxt<'_>, writer: &crate::pretty_clif::CommentW
}
fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Backend>) {
crate::constant::check_constants(fx);
for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
let block = fx.get_block(bb);
fx.bcx.switch_to_block(block);

View File

@ -1,8 +1,9 @@
use rustc_span::DUMMY_SP;
use rustc_errors::ErrorReported;
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir::interpret::{
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, Pointer, Scalar,
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer, Scalar,
};
use rustc_middle::ty::{Const, ConstKind};
use rustc_target::abi::Align;
@ -34,6 +35,29 @@ impl ConstantCx {
}
}
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Backend>) {
for constant in &fx.mir.required_consts {
let const_ = fx.monomorphize(&constant.literal);
match const_.val {
ConstKind::Value(_) => {}
ConstKind::Unevaluated(def, ref substs, promoted) => {
if let Err(err) = fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) {
match err {
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
}
ErrorHandled::TooGeneric => {
span_bug!(constant.span, "codgen encountered polymorphic constant: {:?}", err);
}
}
}
}
ConstKind::Param(_) | ConstKind::Infer(_) | ConstKind::Bound(_, _)
| ConstKind::Placeholder(_) | ConstKind::Error(_) => unreachable!("{:?}", const_),
}
}
}
pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
constants_cx.todo.push(TodoItem::Static(def_id));
}

View File

@ -5,6 +5,7 @@
extern crate flate2;
#[cfg(feature = "jit")]
extern crate libc;
#[macro_use]
extern crate rustc_middle;
extern crate rustc_codegen_ssa;
extern crate rustc_data_structures;