From 2f7698d390d9abff770cb30a329509fb316de083 Mon Sep 17 00:00:00 2001
From: Jim Blandy <jimb@red-bean.com>
Date: Wed, 9 Apr 2025 07:53:34 -0700
Subject: [PATCH] [naga wgsl-in] Don't panic printing struct types for
 conversions.

When a type conversion expression is ill-formed and the operand is a
struct type, don't panic trying to format the operand's type for the
error message.
---
 naga/src/front/wgsl/lower/construction.rs | 10 ++++++++--
 naga/tests/naga/wgsl_errors.rs            | 21 +++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/naga/src/front/wgsl/lower/construction.rs b/naga/src/front/wgsl/lower/construction.rs
index 286a893b9..997d5a312 100644
--- a/naga/src/front/wgsl/lower/construction.rs
+++ b/naga/src/front/wgsl/lower/construction.rs
@@ -550,8 +550,14 @@ impl<'source> Lowerer<'source, '_> {
             // ERRORS
 
             // Bad conversion (type cast)
-            (Components::One { span, ty_inner, .. }, constructor) => {
-                let from_type = ctx.type_inner_to_string(ty_inner);
+            (
+                Components::One {
+                    span, component, ..
+                },
+                constructor,
+            ) => {
+                let component_ty = &ctx.typifier()[component];
+                let from_type = ctx.type_resolution_to_string(component_ty);
                 return Err(Box::new(Error::BadTypeCast {
                     span,
                     from_type,
diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs
index 80ad58f9f..89a873817 100644
--- a/naga/tests/naga/wgsl_errors.rs
+++ b/naga/tests/naga/wgsl_errors.rs
@@ -3390,3 +3390,24 @@ fn struct_names_in_argument_errors() {
     assert!(variant("1i").is_err());
     assert!(variant("A()").is_err());
 }
+
+/// Naga should not crash just because the type of a
+/// bad conversion operand is a struct.
+#[test]
+fn struct_names_in_conversion_errors() {
+    #[track_caller]
+    fn variant(argument: &str) -> Result<naga::Module, naga::front::wgsl::ParseError> {
+        let input = format!(
+            r#"
+                struct A {{ x: i32, }};
+                fn f() {{ _ = i32({argument}); }}
+            "#
+        );
+        naga::front::wgsl::parse_str(&input)
+    }
+
+    assert!(variant("1.0").is_ok());
+    assert!(variant("1").is_ok());
+    assert!(variant("1i").is_ok());
+    assert!(variant("A()").is_err());
+}