From 59db4a6d62ebd73c54d31ef18c77facd72a77f5e Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Wed, 9 Apr 2025 08:44:36 -0700 Subject: [PATCH] [naga wgsl-in] Don't panic printing struct types for initializers. When an initializer has the wrong type and it's a struct, don't panic trying to format its type for the error message. --- naga/src/front/wgsl/lower/mod.rs | 4 ++-- naga/tests/naga/wgsl_errors.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index fda90346c3..cb1ac387fa 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -1308,8 +1308,8 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { if !explicit_inner.equivalent(init_inner, &ectx.module.types) { return Err(Box::new(Error::InitializationTypeMismatch { name: name.span, - expected: ectx.type_inner_to_string(explicit_inner), - got: ectx.type_inner_to_string(init_inner), + expected: ectx.type_to_string(explicit_ty), + got: ectx.type_to_string(init_ty), })); } ty = explicit_ty; diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 89a8738173..db7534e65e 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -3411,3 +3411,24 @@ fn struct_names_in_conversion_errors() { assert!(variant("1i").is_ok()); assert!(variant("A()").is_err()); } + +/// Naga should not crash just because the type of a +/// bad initializer is a struct. +#[test] +fn struct_names_in_init_errors() { + #[track_caller] + fn variant(init: &str) -> Result { + let input = format!( + r#" + struct A {{ x: i32, }}; + fn f() {{ var y: i32 = {init}; }} + "# + ); + naga::front::wgsl::parse_str(&input) + } + + assert!(variant("1").is_ok()); + assert!(variant("1i").is_ok()); + assert!(variant("1.0").is_err()); + assert!(variant("A()").is_err()); +}