[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.
This commit is contained in:
Jim Blandy 2025-04-09 07:53:34 -07:00 committed by Erich Gubler
parent b837db37ad
commit 2f7698d390
2 changed files with 29 additions and 2 deletions

View File

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

View File

@ -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());
}