Skip to content

Commit 97cc700

Browse files
Fix ICE: global_asm!() Don't Panic When Unable to Evaluate Constant
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`.
1 parent 935842b commit 97cc700

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
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

tests/ui/asm/fail-const-eval-issue-121099.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ build-fail
1+
//@ build-fail
22
#![feature(asm_const)]
33

44
use std::arch::global_asm;

0 commit comments

Comments
 (0)