From cb02f74aa4c24cd6b82437eff3ce219e775530b9 Mon Sep 17 00:00:00 2001 From: Moritz Zielke Date: Fri, 9 Jan 2026 00:06:14 +0100 Subject: [PATCH] fix(naga): avoid vector splat construction from invalid scalar type (#8829) --- CHANGELOG.md | 1 + cts_runner/test.lst | 3 +- naga/src/front/wgsl/lower/construction.rs | 29 +++++++++++++++---- naga/tests/in/wgsl/msl-vpt-formats-x1.wgsl | 2 +- naga/tests/in/wgsl/msl-vpt-formats-x2.wgsl | 2 +- naga/tests/in/wgsl/msl-vpt-formats-x3.wgsl | 2 +- naga/tests/in/wgsl/msl-vpt-formats-x4.wgsl | 2 +- naga/tests/naga/wgsl_errors.rs | 21 ++++++++++++++ .../tests/out/msl/wgsl-msl-vpt-formats-x1.msl | 2 +- .../tests/out/msl/wgsl-msl-vpt-formats-x2.msl | 2 +- .../tests/out/msl/wgsl-msl-vpt-formats-x3.msl | 2 +- .../tests/out/msl/wgsl-msl-vpt-formats-x4.msl | 2 +- 12 files changed, 56 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f001d58ed..80cd23370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ Bottom level categories: #### naga - Reject zero-value construction of a runtime-sized array with a validation error. Previously it would crash in the HLSL backend. By @mooori in [#8741](https://github.com/gfx-rs/wgpu/pull/8741). +- Reject splat vector construction if the argument type does not match the type of the vector's scalar. Previously it would succeed. By @mooori in [#8829](https://github.com/gfx-rs/wgpu/pull/8829). ### Documentation diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 365c1c09e..5f751b9b4 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -247,12 +247,13 @@ webgpu:shader,validation,expression,binary,short_circuiting_and_or:invalid_types webgpu:shader,validation,expression,binary,short_circuiting_and_or:scalar_vector:op="%26%26";lhs="bool";rhs="bool" webgpu:shader,validation,expression,call,builtin,all:arguments:test="ptr_deref" webgpu:shader,validation,expression,call,builtin,max:values:* -// FAIL: others in `value_constructor` due to #8815, #8442, #4720, possibly more +// FAIL: others in `value_constructor` due to https://github.com/gfx-rs/wgpu/issues/4720, possibly more webgpu:shader,validation,expression,call,builtin,value_constructor:array_value:* webgpu:shader,validation,expression,call,builtin,value_constructor:matrix_zero_value:* webgpu:shader,validation,expression,call,builtin,value_constructor:scalar_zero_value:* webgpu:shader,validation,expression,call,builtin,value_constructor:scalar_value:* webgpu:shader,validation,expression,call,builtin,value_constructor:struct_value:* +webgpu:shader,validation,expression,call,builtin,value_constructor:vector_splat:* webgpu:shader,validation,expression,call,builtin,value_constructor:vector_zero_value:* // NOTE: This is supposed to be an exhaustive listing underneath // `webgpu:shader,validation,expression,call,builtin,workgroupUniformLoad:*`, so exceptions can be diff --git a/naga/src/front/wgsl/lower/construction.rs b/naga/src/front/wgsl/lower/construction.rs index 2159ef01a..22a53dbd8 100644 --- a/naga/src/front/wgsl/lower/construction.rs +++ b/naga/src/front/wgsl/lower/construction.rs @@ -7,7 +7,7 @@ use alloc::{ }; use core::num::NonZeroU32; -use crate::common::wgsl::TypeContext; +use crate::common::wgsl::{TryToWgsl, TypeContext}; use crate::front::wgsl::lower::{ExpressionContext, Lowerer}; use crate::front::wgsl::parse::ast; use crate::front::wgsl::{Error, Result}; @@ -315,14 +315,33 @@ impl<'source> Lowerer<'source, '_> { ( Components::One { mut component, - ty_inner: &crate::TypeInner::Scalar(_), - .. + ty_inner: &crate::TypeInner::Scalar(component_scalar), + span, }, - Constructor::Type((_, &crate::TypeInner::Vector { size, scalar })), + Constructor::Type(( + type_handle, + &crate::TypeInner::Vector { + size, + scalar: vec_scalar, + }, + )), ) => { + // Splat only allows automatic conversions of the component's scalar. + if !component_scalar.automatically_converts_to(vec_scalar) { + let component_ty = &ctx.typifier()[component]; + let arg_ty = ctx.type_resolution_to_string(component_ty); + return Err(Box::new(Error::WrongArgumentType { + function: ctx.type_to_string(type_handle), + call_span: ty_span, + arg_span: span, + arg_index: 0, + arg_ty, + allowed: vec![vec_scalar.to_wgsl_for_diagnostics()], + })); + } ctx.convert_slice_to_common_leaf_scalar( core::slice::from_mut(&mut component), - scalar, + vec_scalar, )?; expr = crate::Expression::Splat { size, diff --git a/naga/tests/in/wgsl/msl-vpt-formats-x1.wgsl b/naga/tests/in/wgsl/msl-vpt-formats-x1.wgsl index afdd9627b..5e6237ed5 100644 --- a/naga/tests/in/wgsl/msl-vpt-formats-x1.wgsl +++ b/naga/tests/in/wgsl/msl-vpt-formats-x1.wgsl @@ -58,5 +58,5 @@ fn render_vertex( v_in: VertexInput, ) -> VertexOutput { - return VertexOutput(vec4f(v_in.v_uint8)); + return VertexOutput(vec4f(v_in.v_float32)); } diff --git a/naga/tests/in/wgsl/msl-vpt-formats-x2.wgsl b/naga/tests/in/wgsl/msl-vpt-formats-x2.wgsl index 1863bace1..24ced1034 100644 --- a/naga/tests/in/wgsl/msl-vpt-formats-x2.wgsl +++ b/naga/tests/in/wgsl/msl-vpt-formats-x2.wgsl @@ -58,5 +58,5 @@ fn render_vertex( v_in: VertexInput, ) -> VertexOutput { - return VertexOutput(vec4f(v_in.v_uint8.x)); + return VertexOutput(vec4f(v_in.v_float32.x)); } diff --git a/naga/tests/in/wgsl/msl-vpt-formats-x3.wgsl b/naga/tests/in/wgsl/msl-vpt-formats-x3.wgsl index d1672b5c1..6010b09ca 100644 --- a/naga/tests/in/wgsl/msl-vpt-formats-x3.wgsl +++ b/naga/tests/in/wgsl/msl-vpt-formats-x3.wgsl @@ -58,5 +58,5 @@ fn render_vertex( v_in: VertexInput, ) -> VertexOutput { - return VertexOutput(vec4f(v_in.v_uint8.x)); + return VertexOutput(vec4f(v_in.v_float32.x)); } diff --git a/naga/tests/in/wgsl/msl-vpt-formats-x4.wgsl b/naga/tests/in/wgsl/msl-vpt-formats-x4.wgsl index 329884adb..de7e09cb6 100644 --- a/naga/tests/in/wgsl/msl-vpt-formats-x4.wgsl +++ b/naga/tests/in/wgsl/msl-vpt-formats-x4.wgsl @@ -58,5 +58,5 @@ fn render_vertex( v_in: VertexInput, ) -> VertexOutput { - return VertexOutput(vec4f(v_in.v_uint8.x)); + return VertexOutput(vec4f(v_in.v_float32.x)); } diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 9fbc11563..743889aeb 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -366,6 +366,27 @@ fn vector_constructor_incorrect_component_count() { ); } +#[test] +fn vector_constructor_type_mismatch() { + check( + r#" + fn x(a: u32) -> vec2f { + return vec2f(a); + } + "#, + r#"error: wrong type passed as argument #1 to `vec2` + ┌─ wgsl:3:24 + │ +3 │ return vec2f(a); + │ ^^^^^ ^ argument #1 has type `u32` + │ + = note: `vec2` accepts the following types for argument #1: + = note: allowed type: f32 + +"#, + ); +} + #[test] fn bad_texture_sample_type() { check( diff --git a/naga/tests/out/msl/wgsl-msl-vpt-formats-x1.msl b/naga/tests/out/msl/wgsl-msl-vpt-formats-x1.msl index 766ffff6e..d3577708c 100644 --- a/naga/tests/out/msl/wgsl-msl-vpt-formats-x1.msl +++ b/naga/tests/out/msl/wgsl-msl-vpt-formats-x1.msl @@ -302,6 +302,6 @@ vertex render_vertexOutput render_vertex( v_unorm8x4_bgra = unpackUnorm8x4Bgra(vb_1_elem.data[640], vb_1_elem.data[641], vb_1_elem.data[642], vb_1_elem.data[643]).x; } const VertexInput v_in = { v_uint8_, v_uint8x2_, v_uint8x4_, v_sint8_, v_sint8x2_, v_sint8x4_, v_unorm8_, v_unorm8x2_, v_unorm8x4_, v_snorm8_, v_snorm8x2_, v_snorm8x4_, v_uint16_, v_uint16x2_, v_uint16x4_, v_sint16_, v_sint16x2_, v_sint16x4_, v_unorm16_, v_unorm16x2_, v_unorm16x4_, v_snorm16_, v_snorm16x2_, v_snorm16x4_, v_float16_, v_float16x2_, v_float16x4_, v_float32_, v_float32x2_, v_float32x3_, v_float32x4_, v_uint32_, v_uint32x2_, v_uint32x3_, v_uint32x4_, v_sint32_, v_sint32x2_, v_sint32x3_, v_sint32x4_, v_unorm10_10_10_2_, v_unorm8x4_bgra }; - const auto _tmp = VertexOutput {metal::float4(static_cast(v_in.v_uint8_))}; + const auto _tmp = VertexOutput {metal::float4(v_in.v_float32_)}; return render_vertexOutput { _tmp.position }; } diff --git a/naga/tests/out/msl/wgsl-msl-vpt-formats-x2.msl b/naga/tests/out/msl/wgsl-msl-vpt-formats-x2.msl index 0559be74a..403be7fa1 100644 --- a/naga/tests/out/msl/wgsl-msl-vpt-formats-x2.msl +++ b/naga/tests/out/msl/wgsl-msl-vpt-formats-x2.msl @@ -302,6 +302,6 @@ vertex render_vertexOutput render_vertex( v_unorm8x4_bgra = unpackUnorm8x4Bgra(vb_1_elem.data[640], vb_1_elem.data[641], vb_1_elem.data[642], vb_1_elem.data[643]).xy; } const VertexInput v_in = { v_uint8_, v_uint8x2_, v_uint8x4_, v_sint8_, v_sint8x2_, v_sint8x4_, v_unorm8_, v_unorm8x2_, v_unorm8x4_, v_snorm8_, v_snorm8x2_, v_snorm8x4_, v_uint16_, v_uint16x2_, v_uint16x4_, v_sint16_, v_sint16x2_, v_sint16x4_, v_unorm16_, v_unorm16x2_, v_unorm16x4_, v_snorm16_, v_snorm16x2_, v_snorm16x4_, v_float16_, v_float16x2_, v_float16x4_, v_float32_, v_float32x2_, v_float32x3_, v_float32x4_, v_uint32_, v_uint32x2_, v_uint32x3_, v_uint32x4_, v_sint32_, v_sint32x2_, v_sint32x3_, v_sint32x4_, v_unorm10_10_10_2_, v_unorm8x4_bgra }; - const auto _tmp = VertexOutput {metal::float4(static_cast(v_in.v_uint8_.x))}; + const auto _tmp = VertexOutput {metal::float4(v_in.v_float32_.x)}; return render_vertexOutput { _tmp.position }; } diff --git a/naga/tests/out/msl/wgsl-msl-vpt-formats-x3.msl b/naga/tests/out/msl/wgsl-msl-vpt-formats-x3.msl index 7db698063..5766e66ef 100644 --- a/naga/tests/out/msl/wgsl-msl-vpt-formats-x3.msl +++ b/naga/tests/out/msl/wgsl-msl-vpt-formats-x3.msl @@ -311,6 +311,6 @@ vertex render_vertexOutput render_vertex( v_unorm8x4_bgra = unpackUnorm8x4Bgra(vb_1_elem.data[640], vb_1_elem.data[641], vb_1_elem.data[642], vb_1_elem.data[643]).xyz; } const VertexInput v_in = { v_uint8_, v_uint8x2_, v_uint8x4_, v_sint8_, v_sint8x2_, v_sint8x4_, v_unorm8_, v_unorm8x2_, v_unorm8x4_, v_snorm8_, v_snorm8x2_, v_snorm8x4_, v_uint16_, v_uint16x2_, v_uint16x4_, v_sint16_, v_sint16x2_, v_sint16x4_, v_unorm16_, v_unorm16x2_, v_unorm16x4_, v_snorm16_, v_snorm16x2_, v_snorm16x4_, v_float16_, v_float16x2_, v_float16x4_, v_float32_, v_float32x2_, v_float32x3_, v_float32x4_, v_uint32_, v_uint32x2_, v_uint32x3_, v_uint32x4_, v_sint32_, v_sint32x2_, v_sint32x3_, v_sint32x4_, v_unorm10_10_10_2_, v_unorm8x4_bgra }; - const auto _tmp = VertexOutput {metal::float4(static_cast(v_in.v_uint8_.x))}; + const auto _tmp = VertexOutput {metal::float4(v_in.v_float32_.x)}; return render_vertexOutput { _tmp.position }; } diff --git a/naga/tests/out/msl/wgsl-msl-vpt-formats-x4.msl b/naga/tests/out/msl/wgsl-msl-vpt-formats-x4.msl index d35045ffc..be0b5d1c3 100644 --- a/naga/tests/out/msl/wgsl-msl-vpt-formats-x4.msl +++ b/naga/tests/out/msl/wgsl-msl-vpt-formats-x4.msl @@ -300,6 +300,6 @@ vertex render_vertexOutput render_vertex( v_unorm8x4_bgra = unpackUnorm8x4Bgra(vb_1_elem.data[640], vb_1_elem.data[641], vb_1_elem.data[642], vb_1_elem.data[643]); } const VertexInput v_in = { v_uint8_, v_uint8x2_, v_uint8x4_, v_sint8_, v_sint8x2_, v_sint8x4_, v_unorm8_, v_unorm8x2_, v_unorm8x4_, v_snorm8_, v_snorm8x2_, v_snorm8x4_, v_uint16_, v_uint16x2_, v_uint16x4_, v_sint16_, v_sint16x2_, v_sint16x4_, v_unorm16_, v_unorm16x2_, v_unorm16x4_, v_snorm16_, v_snorm16x2_, v_snorm16x4_, v_float16_, v_float16x2_, v_float16x4_, v_float32_, v_float32x2_, v_float32x3_, v_float32x4_, v_uint32_, v_uint32x2_, v_uint32x3_, v_uint32x4_, v_sint32_, v_sint32x2_, v_sint32x3_, v_sint32x4_, v_unorm10_10_10_2_, v_unorm8x4_bgra }; - const auto _tmp = VertexOutput {metal::float4(static_cast(v_in.v_uint8_.x))}; + const auto _tmp = VertexOutput {metal::float4(v_in.v_float32_.x)}; return render_vertexOutput { _tmp.position }; }