about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa
diff options
context:
space:
mode:
authorVeera <sveera.2001@gmail.com>2024-03-18 11:26:30 -0400
committerVeera <sveera.2001@gmail.com>2024-03-18 11:35:40 -0400
commit97cc7003ca23289d04e4111b81453e2584856f54 (patch)
tree0be2575a702a3ef44d02ddabc0923b600cfca307 /compiler/rustc_codegen_ssa
parent935842bf0a7b3401355ea5285f1b8c6646a242c4 (diff)
downloadrust-97cc7003ca23289d04e4111b81453e2584856f54.tar.gz
rust-97cc7003ca23289d04e4111b81453e2584856f54.zip
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`.
Diffstat (limited to 'compiler/rustc_codegen_ssa')
-rw-r--r--compiler/rustc_codegen_ssa/src/mono_item.rs46
1 files changed, 29 insertions, 17 deletions
diff --git a/compiler/rustc_codegen_ssa/src/mono_item.rs b/compiler/rustc_codegen_ssa/src/mono_item.rs
index 7b7cdae0ed6..df564f705bc 100644
--- a/compiler/rustc_codegen_ssa/src/mono_item.rs
+++ b/compiler/rustc_codegen_ssa/src/mono_item.rs
@@ -2,6 +2,7 @@ use crate::base;
 use crate::common;
 use crate::traits::*;
 use rustc_hir as hir;
+use rustc_middle::mir::interpret::ErrorHandled;
 use rustc_middle::mir::mono::MonoItem;
 use rustc_middle::mir::mono::{Linkage, Visibility};
 use rustc_middle::ty;
@@ -40,23 +41,34 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
                         .iter()
                         .map(|(op, op_sp)| match *op {
                             hir::InlineAsmOperand::Const { ref anon_const } => {
-                                let const_value = cx
-                                    .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 = cx
-                                    .tcx()
-                                    .typeck_body(anon_const.body)
-                                    .node_type(anon_const.hir_id);
-                                let string = common::asm_const_to_str(
-                                    cx.tcx(),
-                                    *op_sp,
-                                    const_value,
-                                    cx.layout_of(ty),
-                                );
-                                GlobalAsmOperandRef::Const { string }
+                                match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
+                                    Ok(const_value) => {
+                                        let ty = cx
+                                            .tcx()
+                                            .typeck_body(anon_const.body)
+                                            .node_type(anon_const.hir_id);
+                                        let string = common::asm_const_to_str(
+                                            cx.tcx(),
+                                            *op_sp,
+                                            const_value,
+                                            cx.layout_of(ty),
+                                        );
+                                        GlobalAsmOperandRef::Const { string }
+                                    }
+                                    Err(ErrorHandled::Reported { .. }) => {
+                                        // An error has already been reported and
+                                        // 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 } => {
                                 let ty = cx