diff options
| author | BeetleFunk <124721033+BeetleFunk@users.noreply.github.com> | 2024-03-21 11:59:03 -0400 |
|---|---|---|
| committer | BeetleFunk <124721033+BeetleFunk@users.noreply.github.com> | 2024-03-21 11:59:03 -0400 |
| commit | eb752bc6a79e13eb779eeeba7f589aef58f80e9f (patch) | |
| tree | 414f27349b187b6a05685ac3784e23e95dacc759 /src | |
| parent | c49f6080629e4e0b788a8b58225e1ba35a3dbf1e (diff) | |
| download | rust-eb752bc6a79e13eb779eeeba7f589aef58f80e9f.tar.gz rust-eb752bc6a79e13eb779eeeba7f589aef58f80e9f.zip | |
Avoid ICE when global_asm const operand fails to evaluate
Diffstat (limited to 'src')
| -rw-r--r-- | src/global_asm.rs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/src/global_asm.rs b/src/global_asm.rs index 44650898de8..5a0cd3990f2 100644 --- a/src/global_asm.rs +++ b/src/global_asm.rs @@ -8,6 +8,7 @@ use std::sync::Arc; use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece}; use rustc_hir::{InlineAsmOperand, ItemId}; +use rustc_middle::mir::interpret::ErrorHandled; use rustc_session::config::{OutputFilenames, OutputType}; 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 } => { match asm.operands[operand_idx].0 { InlineAsmOperand::Const { ref anon_const } => { - let const_value = - tcx.const_eval_poly(anon_const.def_id.to_def_id()).unwrap_or_else( - |_| span_bug!(op_sp, "asm const cannot be resolved"), - ); - let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id); - let string = rustc_codegen_ssa::common::asm_const_to_str( - tcx, - op_sp, - const_value, - RevealAllLayoutCx(tcx).layout_of(ty), - ); - global_asm.push_str(&string); + match tcx.const_eval_poly(anon_const.def_id.to_def_id()) { + Ok(const_value) => { + let ty = tcx + .typeck_body(anon_const.body) + .node_type(anon_const.hir_id); + let string = rustc_codegen_ssa::common::asm_const_to_str( + tcx, + op_sp, + const_value, + RevealAllLayoutCx(tcx).layout_of(ty), + ); + 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 } => { if cfg!(not(feature = "inline_asm_sym")) { |
