Skip to content

Commit 0610490

Browse files
committed
Assume constants can't fail to evaluate
See rust-lang/rust#81327 for the same change to cg_llvm
1 parent 74f39b6 commit 0610490

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/base.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ pub(crate) fn codegen_fn<'tcx>(
7474
.is_uninhabited()
7575
});
7676

77-
if arg_uninhabited {
77+
if !crate::constant::check_constants(&mut fx) {
78+
fx.bcx
79+
.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
80+
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
81+
crate::trap::trap_unreachable(&mut fx, "compilation should have been aborted");
82+
} else if arg_uninhabited {
7883
fx.bcx
7984
.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
8085
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
@@ -205,8 +210,6 @@ pub(crate) fn verify_func(
205210
}
206211

207212
fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
208-
crate::constant::check_constants(fx);
209-
210213
for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() {
211214
let block = fx.get_block(bb);
212215
fx.bcx.switch_to_block(block);

src/constant.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ impl ConstantCx {
3636
}
3737
}
3838

39-
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
39+
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) -> bool {
40+
let mut all_constants_ok = true;
4041
for constant in &fx.mir.required_consts {
4142
let const_ = fx.monomorphize(constant.literal);
4243
match const_.val {
@@ -46,6 +47,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
4647
fx.tcx
4748
.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None)
4849
{
50+
all_constants_ok = false;
4951
match err {
5052
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {
5153
fx.tcx
@@ -69,6 +71,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
6971
| ConstKind::Error(_) => unreachable!("{:?}", const_),
7072
}
7173
}
74+
all_constants_ok
7275
}
7376

7477
pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
@@ -134,14 +137,7 @@ pub(crate) fn codegen_constant<'tcx>(
134137
{
135138
Ok(const_val) => const_val,
136139
Err(_) => {
137-
fx.tcx
138-
.sess
139-
.span_err(constant.span, "erroneous constant encountered");
140-
return crate::trap::trap_unreachable_ret_value(
141-
fx,
142-
fx.layout_of(const_.ty),
143-
"erroneous constant encountered",
144-
);
140+
span_bug!(constant.span, "erroneous constant not captured by required_consts");
145141
}
146142
}
147143
}

0 commit comments

Comments
 (0)