Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WGSL precedence parsing is too lenient #4536

Open
raphlinus opened this issue Oct 21, 2022 · 1 comment
Open

WGSL precedence parsing is too lenient #4536

raphlinus opened this issue Oct 21, 2022 · 1 comment
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working

Comments

@raphlinus
Copy link
Contributor

raphlinus commented Oct 21, 2022

Per gpuweb/gpuweb#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.

raphlinus referenced this issue in linebender/vello Nov 9, 2022
Note that this is evidence in favor of https://github.com/gfx-rs/naga/issues/2098 - my code is actually wrong, and it was caught by trying to port it to run in Chrome Canary.
@jimblandy
Copy link
Member

Concrete example:

index & 1u != 0u

should parse as

(index & 1u) != 0u

but Naga parses it as

index & (1u != 0u)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working
Projects
Status: No status
Development

No branches or pull requests

4 participants