From 01250b85fe8f48b45100d4f03c740cb26334ee84 Mon Sep 17 00:00:00 2001 From: Hans Christian Schmitz Date: Wed, 18 Aug 2021 08:06:04 +0200 Subject: [PATCH] Fix WGSL vector splat constructor's type handling Previously the constructor just used the type of the scalar argument regardless of whether or not it actually matched the vector splat constructors target type. This resulted in e.g. `vec2(0)` erroneously getting the type of a bi-vector of Sints with width 4. Now the splat constructor checks that the kind and width of the scalar argument is the same as that of the target specified in the constructor's type parameter. --- src/front/wgsl/mod.rs | 19 +++++++++++++------ src/front/wgsl/tests.rs | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/front/wgsl/mod.rs b/src/front/wgsl/mod.rs index 4bbc70231d..7d2ad1dbfb 100644 --- a/src/front/wgsl/mod.rs +++ b/src/front/wgsl/mod.rs @@ -1746,12 +1746,19 @@ impl Parser { ty_resolution.inner_with(ctx.types), ctx.typifier.get(last_component, ctx.types), ) { - (&crate::TypeInner::Vector { size, .. }, &crate::TypeInner::Scalar { .. }) => { - crate::Expression::Splat { - size, - value: last_component, - } - } + ( + &crate::TypeInner::Vector { + size, kind, width, .. + }, + &crate::TypeInner::Scalar { + kind: arg_kind, + width: arg_width, + .. + }, + ) if arg_kind == kind && arg_width == width => crate::Expression::Splat { + size, + value: last_component, + }, ( &crate::TypeInner::Scalar { kind, width, .. }, &crate::TypeInner::Scalar { .. }, diff --git a/src/front/wgsl/tests.rs b/src/front/wgsl/tests.rs index 4297eccde8..d22310945b 100644 --- a/src/front/wgsl/tests.rs +++ b/src/front/wgsl/tests.rs @@ -65,6 +65,22 @@ fn parse_type_cast() { ", ) .unwrap(); + parse_str( + " + fn main() { + let x: vec2 = vec2(0.0); + } + ", + ) + .unwrap(); + assert!(parse_str( + " + fn main() { + let x: vec2 = vec2(0); + } + ", + ) + .is_err()); } #[test]