[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.
This commit is contained in:
Jim Blandy
2025-04-09 08:44:36 -07:00
committed by Erich Gubler
parent 2f7698d390
commit 59db4a6d62
2 changed files with 23 additions and 2 deletions

View File

@@ -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;

View File

@@ -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<naga::Module, naga::front::wgsl::ParseError> {
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());
}