Skip to content

Commit

Permalink
Properly recognize Literal expressions as non-dynamic indices.
Browse files Browse the repository at this point in the history
Restore `negative_index` test in `tests/wgsl-errors.rs`, as part of
the `invalid_arrays` test function.
  • Loading branch information
jimblandy committed Oct 5, 2023
1 parent 2d070b1 commit b961ea8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
12 changes: 7 additions & 5 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
39 changes: 20 additions & 19 deletions tests/wgsl-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,6 @@ fn unknown_identifier() {
);
}

// #[test]
// fn negative_index() {
// check(
// r#"
// fn main() -> f32 {
// let a = array<f32, 3>(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(
Expand Down Expand Up @@ -921,6 +902,26 @@ fn invalid_arrays() {
})
}

check_validation! {
r#"
fn main() -> f32 {
let a = array<f32, 3>(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<f32, true>;",
r###"error: must be a const-expression that resolves to a concrete integer scalar (u32 or i32)
Expand Down

0 comments on commit b961ea8

Please sign in to comment.