gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.58k stars 920 forks source link

WGSL precedence parsing is too lenient #4536

Open raphlinus opened 2 years ago

raphlinus commented 2 years ago

Per https://github.com/gpuweb/gpuweb/issues/3346, WGSL has a partial order of precedence, meaning that among other things the arguments to a shift expression must be parenthesized if they are not unary: a + b << c is not valid and should not parse (in C precedence rules, this would be (a + b) << c which is very likely not what's intended, hence the nudge to always explicitly parenthesize).

Looking at the code this should be fairly straightforward. From what I can see, parse_equality_expression currently nests the additive_expression parser inside a call to parse_binary_op for shift_expression, and this should be changed to more closely match the structure of the WGSL spec.

jimblandy commented 1 year ago

Concrete example:

index & 1u != 0u

should parse as

(index & 1u) != 0u

but Naga parses it as

index & (1u != 0u)