From 0610490c8dc71868ba886efbb8707cb77fc35928 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 13:58:56 +0100 Subject: [PATCH] Assume constants can't fail to evaluate See rust-lang/rust#81327 for the same change to cg_llvm --- src/base.rs | 9 ++++++--- src/constant.rs | 14 +++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/base.rs b/src/base.rs index 03889a4687f..0be498172ae 100644 --- a/src/base.rs +++ b/src/base.rs @@ -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]); @@ -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); diff --git a/src/constant.rs b/src/constant.rs index 5702832bcb6..f3e17774fdf 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -36,7 +36,8 @@ pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut impl Module) { } } -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 { @@ -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 @@ -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) { @@ -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"); } } }