Merge pull request #1467 from BeetleFunk/global-asm-ice

Avoid ICE when global_asm const operand fails to evaluate
This commit is contained in:
bjorn3 2024-03-21 23:06:33 +01:00 committed by GitHub
commit 9ddba16363
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
use rustc_hir::{InlineAsmOperand, ItemId}; use rustc_hir::{InlineAsmOperand, ItemId};
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_session::config::{OutputFilenames, OutputType}; use rustc_session::config::{OutputFilenames, OutputType};
use rustc_target::asm::InlineAsmArch; use rustc_target::asm::InlineAsmArch;
@ -32,18 +33,27 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
InlineAsmTemplatePiece::Placeholder { operand_idx, modifier: _, span: op_sp } => { InlineAsmTemplatePiece::Placeholder { operand_idx, modifier: _, span: op_sp } => {
match asm.operands[operand_idx].0 { match asm.operands[operand_idx].0 {
InlineAsmOperand::Const { ref anon_const } => { InlineAsmOperand::Const { ref anon_const } => {
let const_value = match tcx.const_eval_poly(anon_const.def_id.to_def_id()) {
tcx.const_eval_poly(anon_const.def_id.to_def_id()).unwrap_or_else( Ok(const_value) => {
|_| span_bug!(op_sp, "asm const cannot be resolved"), let ty = tcx
); .typeck_body(anon_const.body)
let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id); .node_type(anon_const.hir_id);
let string = rustc_codegen_ssa::common::asm_const_to_str( let string = rustc_codegen_ssa::common::asm_const_to_str(
tcx, tcx,
op_sp, op_sp,
const_value, const_value,
RevealAllLayoutCx(tcx).layout_of(ty), RevealAllLayoutCx(tcx).layout_of(ty),
); );
global_asm.push_str(&string); global_asm.push_str(&string);
}
Err(ErrorHandled::Reported { .. }) => {
// An error has already been reported and compilation is
// guaranteed to fail if execution hits this path.
}
Err(ErrorHandled::TooGeneric(_)) => {
span_bug!(op_sp, "asm const cannot be resolved; too generic");
}
}
} }
InlineAsmOperand::SymFn { anon_const } => { InlineAsmOperand::SymFn { anon_const } => {
if cfg!(not(feature = "inline_asm_sym")) { if cfg!(not(feature = "inline_asm_sym")) {