Skip to content

Commit

Permalink
Assume constants can't fail to evaluate
Browse files Browse the repository at this point in the history
See rust-lang/rust#81327 for the same change to cg_llvm
  • Loading branch information
bjorn3 committed Feb 21, 2021
1 parent 74f39b6 commit 0610490
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ pub(crate) fn codegen_fn<'tcx>(
.is_uninhabited()
});

if arg_uninhabited {
if !crate::constant::check_constants(&mut fx) {
fx.bcx
.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
crate::trap::trap_unreachable(&mut fx, "compilation should have been aborted");
} else if arg_uninhabited {
fx.bcx
.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
Expand Down Expand Up @@ -205,8 +210,6 @@ pub(crate) fn verify_func(
}

fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
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);
Expand Down
14 changes: 5 additions & 9 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ impl ConstantCx {
}
}

pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) -> bool {
let mut all_constants_ok = true;
for constant in &fx.mir.required_consts {
let const_ = fx.monomorphize(constant.literal);
match const_.val {
Expand All @@ -46,6 +47,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
fx.tcx
.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None)
{
all_constants_ok = false;
match err {
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {
fx.tcx
Expand All @@ -69,6 +71,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
| ConstKind::Error(_) => unreachable!("{:?}", const_),
}
}
all_constants_ok
}

pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
Expand Down Expand Up @@ -134,14 +137,7 @@ pub(crate) fn codegen_constant<'tcx>(
{
Ok(const_val) => const_val,
Err(_) => {
fx.tcx
.sess
.span_err(constant.span, "erroneous constant encountered");
return crate::trap::trap_unreachable_ret_value(
fx,
fx.layout_of(const_.ty),
"erroneous constant encountered",
);
span_bug!(constant.span, "erroneous constant not captured by required_consts");
}
}
}
Expand Down

0 comments on commit 0610490

Please sign in to comment.