From f44d64527a01cdd77ef21afc4dedc42ff7705013 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Thu, 5 Oct 2023 14:03:07 -0700 Subject: [PATCH] Properly recognize `Literal` expressions as non-dynamic indices. (#2537) Restore `negative_index` test in `tests/wgsl-errors.rs`, as part of the `invalid_arrays` test function. --- src/proc/mod.rs | 12 +++++++----- tests/wgsl-errors.rs | 39 ++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/proc/mod.rs b/src/proc/mod.rs index fab2ae9c96..026e75911b 100644 --- a/src/proc/mod.rs +++ b/src/proc/mod.rs @@ -465,11 +465,13 @@ impl crate::Expression { /// [`Access`]: crate::Expression::Access /// [`ResolveContext`]: crate::proc::ResolveContext pub fn is_dynamic_index(&self, module: &crate::Module) -> bool { - if let Self::Constant(handle) = *self { - let constant = &module.constants[handle]; - !matches!(constant.r#override, crate::Override::None) - } else { - true + match *self { + Self::Literal(_) | Self::ZeroValue(_) => false, + Self::Constant(handle) => { + let constant = &module.constants[handle]; + !matches!(constant.r#override, crate::Override::None) + } + _ => true, } } } diff --git a/tests/wgsl-errors.rs b/tests/wgsl-errors.rs index 90acc6c62a..9f6118f954 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -107,25 +107,6 @@ fn unknown_identifier() { ); } -// #[test] -// fn negative_index() { -// check( -// r#" -// fn main() -> f32 { -// let a = array(0., 1., 2.); -// return a[-1]; -// } -// "#, -// r#"error: expected unsigned integer constant expression, found `-1` -// ┌─ wgsl:4:26 -// │ -// 4 │ return a[-1]; -// │ ^^ expected unsigned integer - -// "#, -// ); -// } - #[test] fn bad_texture() { check( @@ -921,6 +902,26 @@ fn invalid_arrays() { }) } + check_validation! { + r#" + fn main() -> f32 { + let a = array(0., 1., 2.); + return a[-1]; + } + "#: + Err( + naga::valid::ValidationError::Function { + name, + source: naga::valid::FunctionError::Expression { + source: naga::valid::ExpressionError::NegativeIndex(_), + .. + }, + .. + } + ) + if name == "main" + } + check( "alias Bad = array;", r###"error: must be a const-expression that resolves to a concrete integer scalar (u32 or i32)