diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d394fd03..693cdb148 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -222,6 +222,7 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109). - Reject `@location(…)`s in compute shaders. By @ErichDonGubler in [#10026](https://github.com/gfx-rs/wgpu/pull/10026). - Correctly emit primitive_index for the SPIR-V backend, handling mesh and raytracing shaders. Before, you could not use primitive_index with these shader types. An enable primitive_index statement is still required in wgsl shaders, in addition to enable wgpu_mesh_shader/enable wgpu_ray_tracing_pipeline. By @JMS55 in [#10153](https://github.com/gfx-rs/wgpu/pull/10153). - Prevent invalid IR from being generated when using a ray query in a loop. By @Vecvec in [#9945](https://github.com/gfx-rs/wgpu/pull/9945) +- Raise a type error, rather than panic, for some cases of an invalid `select` argument type in WGSL constant evaluation. By @ErichDonGubler in [#10350](https://github.com/gfx-rs/wgpu/pull/10350). #### DX12 diff --git a/naga/src/proc/constant_evaluator.rs b/naga/src/proc/constant_evaluator.rs index df9b8bc91..6fd564d6d 100644 --- a/naga/src/proc/constant_evaluator.rs +++ b/naga/src/proc/constant_evaluator.rs @@ -3740,13 +3740,17 @@ impl<'a> ConstantEvaluator<'a> { }, ) => { let ty_deets = |ty| { - let (size, scalar) = self.types[ty].inner.vector_size_and_scalar().unwrap(); - (size.unwrap(), scalar) + self.types[ty] + .inner + .vector_size_and_scalar() + .map(|(size, scalar)| { + (size.expect("expected vector type, not scalar type"), scalar) + }) }; let expected_vec_size = { let [(reject_vec_size, _), (accept_vec_size, _)] = - [reject_ty, accept_ty].map(ty_deets); + [reject_ty, accept_ty].map(|ty| ty_deets(ty).unwrap()); if reject_vec_size != accept_vec_size { return Err(ConstantEvaluatorError::SelectVecRejectAcceptSizeMismatch { @@ -3765,7 +3769,10 @@ impl<'a> ConstantEvaluator<'a> { ty: condition_ty, components: ref condition_components, } => { - let (condition_vec_size, condition_scalar) = ty_deets(condition_ty); + let Some((condition_vec_size, condition_scalar)) = ty_deets(condition_ty) + else { + return Err(ConstantEvaluatorError::SelectConditionNotAVecBool); + }; if condition_scalar.kind != ScalarKind::Bool { return Err(ConstantEvaluatorError::SelectConditionNotAVecBool); } diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 416a98a95..9dc2f549c 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -2784,6 +2784,80 @@ error: type mismatch for reject and accept values in `select` call │ │\x20\x20\x20\x20\x20\x20 │ reject value of type `bool` +", + ), + ( + " + const cond: array = array(); + @compute @workgroup_size(1, 1) + fn main() { + // Bad: `cond` is an `array`, not a `bool` or a `vecN`. + _ = select(vec4(0u), vec4(1u), cond); + } + ", + "\ +error: Expected boolean vector for condition arg., got something else + ┌─ wgsl:6:17 + │ +6 │ _ = select(vec4(0u), vec4(1u), cond); + │ ^^^^^^ see msg + +", + ), + ( + " + struct S { member: bool } + const cond: S = S(true); + @compute @workgroup_size(1, 1) + fn main() { + // Bad: `cond` is a `struct`, not a `bool` or a `vecN`. + _ = select(vec2(0u), vec2(1u), cond); + } + ", + "\ +error: Expected boolean vector for condition arg., got something else + ┌─ wgsl:7:17 + │ +7 │ _ = select(vec2(0u), vec2(1u), cond); + │ ^^^^^^ see msg + +", + ), + ( + " + @compute @workgroup_size(1, 1) + fn main() { + // Bad: the condition is a matrix, not a `bool` or a `vecN`. + _ = select(vec2(0.0f), vec2(1.0f), mat2x2f(1, 1, 1, 1)); + } + ", + "\ +error: Expected boolean vector for condition arg., got something else + ┌─ wgsl:5:17 + │ +5 │ _ = select(vec2(0.0f), vec2(1.0f), mat2x2f(1, 1, 1, 1)); + │ ^^^^^^ see msg + +", + ), + ( + " + const values: array = array(1, 2, 3, 4); + @compute @workgroup_size(1, 1) + fn main() { + // Bad: `array` reject and accept values, even as const-expressions. + _ = select(values, values, true); + } + ", + "\ +error: unexpected argument type for `select` call + ┌─ wgsl:6:24 + │ +6 │ _ = select(values, values, true); + │ ^^^^^^ this value of type `array` + │ + = note: expected a scalar or a `vecN` of scalars + ", ), ];