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`.
This commit is contained in:
Veera 2024-03-18 11:26:30 -04:00
parent 935842bf0a
commit 97cc7003ca
2 changed files with 30 additions and 18 deletions

View File

@ -2,6 +2,7 @@
use crate::common; use crate::common;
use crate::traits::*; use crate::traits::*;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::mir::mono::MonoItem; use rustc_middle::mir::mono::MonoItem;
use rustc_middle::mir::mono::{Linkage, Visibility}; use rustc_middle::mir::mono::{Linkage, Visibility};
use rustc_middle::ty; use rustc_middle::ty;
@ -40,23 +41,34 @@ fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) {
.iter() .iter()
.map(|(op, op_sp)| match *op { .map(|(op, op_sp)| match *op {
hir::InlineAsmOperand::Const { ref anon_const } => { hir::InlineAsmOperand::Const { ref anon_const } => {
let const_value = cx match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
.tcx() Ok(const_value) => {
.const_eval_poly(anon_const.def_id.to_def_id()) let ty = cx
.unwrap_or_else(|_| { .tcx()
span_bug!(*op_sp, "asm const cannot be resolved") .typeck_body(anon_const.body)
}); .node_type(anon_const.hir_id);
let ty = cx let string = common::asm_const_to_str(
.tcx() cx.tcx(),
.typeck_body(anon_const.body) *op_sp,
.node_type(anon_const.hir_id); const_value,
let string = common::asm_const_to_str( cx.layout_of(ty),
cx.tcx(), );
*op_sp, GlobalAsmOperandRef::Const { string }
const_value, }
cx.layout_of(ty), Err(ErrorHandled::Reported { .. }) => {
); // An error has already been reported and
GlobalAsmOperandRef::Const { string } // compilation is guaranteed to fail if execution
// hits this path. So an empty string instead of
// a stringified constant value will suffice.
GlobalAsmOperandRef::Const { string: String::new() }
}
Err(ErrorHandled::TooGeneric(_)) => {
span_bug!(
*op_sp,
"asm const cannot be resolved; too generic"
)
}
}
} }
hir::InlineAsmOperand::SymFn { ref anon_const } => { hir::InlineAsmOperand::SymFn { ref anon_const } => {
let ty = cx let ty = cx

View File

@ -1,4 +1,4 @@
//@ build-fail //@ build-fail
#![feature(asm_const)] #![feature(asm_const)]
use std::arch::global_asm; use std::arch::global_asm;