Skip to content

Commit 2ad2492

Browse files
authored
Rollup merge of #122691 - veera-sivarajan:bugfix-121099, r=Amanieu
Fix ICE: `global_asm!()` Don't Panic When Unable to Evaluate Constant Fixes #121099 A bit of an inelegant fix but given that the error is created only after call to `const_eval_poly()` and that the calling function cannot propagate the error anywhere else, the error has to be explicitly handled inside `mono_item.rs`. r? `@Amanieu`
2 parents 17386b8 + 3948210 commit 2ad2492

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

compiler/rustc_codegen_ssa/src/mono_item.rs

+29-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::base;
22
use crate::common;
33
use crate::traits::*;
44
use rustc_hir as hir;
5+
use rustc_middle::mir::interpret::ErrorHandled;
56
use rustc_middle::mir::mono::MonoItem;
67
use rustc_middle::mir::mono::{Linkage, Visibility};
78
use rustc_middle::ty;
@@ -40,23 +41,34 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
4041
.iter()
4142
.map(|(op, op_sp)| match *op {
4243
hir::InlineAsmOperand::Const { ref anon_const } => {
43-
let const_value = cx
44-
.tcx()
45-
.const_eval_poly(anon_const.def_id.to_def_id())
46-
.unwrap_or_else(|_| {
47-
span_bug!(*op_sp, "asm const cannot be resolved")
48-
});
49-
let ty = cx
50-
.tcx()
51-
.typeck_body(anon_const.body)
52-
.node_type(anon_const.hir_id);
53-
let string = common::asm_const_to_str(
54-
cx.tcx(),
55-
*op_sp,
56-
const_value,
57-
cx.layout_of(ty),
58-
);
59-
GlobalAsmOperandRef::Const { string }
44+
match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
45+
Ok(const_value) => {
46+
let ty = cx
47+
.tcx()
48+
.typeck_body(anon_const.body)
49+
.node_type(anon_const.hir_id);
50+
let string = common::asm_const_to_str(
51+
cx.tcx(),
52+
*op_sp,
53+
const_value,
54+
cx.layout_of(ty),
55+
);
56+
GlobalAsmOperandRef::Const { string }
57+
}
58+
Err(ErrorHandled::Reported { .. }) => {
59+
// An error has already been reported and
60+
// compilation is guaranteed to fail if execution
61+
// hits this path. So an empty string instead of
62+
// a stringified constant value will suffice.
63+
GlobalAsmOperandRef::Const { string: String::new() }
64+
}
65+
Err(ErrorHandled::TooGeneric(_)) => {
66+
span_bug!(
67+
*op_sp,
68+
"asm const cannot be resolved; too generic"
69+
)
70+
}
71+
}
6072
}
6173
hir::InlineAsmOperand::SymFn { ref anon_const } => {
6274
let ty = cx
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ build-fail
2+
//@ needs-asm-support
3+
#![feature(asm_const)]
4+
5+
use std::arch::global_asm;
6+
7+
fn main() {}
8+
9+
global_asm!("/* {} */", const 1 << 500); //~ ERROR evaluation of constant value failed [E0080]
10+
11+
global_asm!("/* {} */", const 1 / 0); //~ ERROR evaluation of constant value failed [E0080]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/fail-const-eval-issue-121099.rs:9:31
3+
|
4+
LL | global_asm!("/* {} */", const 1 << 500);
5+
| ^^^^^^^^ attempt to shift left by `500_i32`, which would overflow
6+
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/fail-const-eval-issue-121099.rs:11:31
9+
|
10+
LL | global_asm!("/* {} */", const 1 / 0);
11+
| ^^^^^ attempt to divide `1_i32` by zero
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)