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

[naga wgsl-in] Handle automatic type conversions for switch selector and case expressions #7250

Merged
merged 1 commit into from
Mar 7, 2025

Conversation

jamienicol
Copy link
Contributor

@jamienicol jamienicol commented Feb 28, 2025

Connections
Fixes #7225

Impacts #7226

Description
When lowering switch statements we weren't handling abstract types at all - we were simply concretizing them to i32. This PR makes us handle them correctly - find the (possibly abstract) type of the switch selector and all case selectors, find a consensus type they can be converted to, then attempt to convert them all to to that type. (concretizing to i32 if they are all abstract).

I've reworked the error handling slightly, differentiating between an invalid switch selector type (must be an int scalar), an invalid case expression (must be an int scalar constexpr), and a type mismatch between cases and the selector.

Note that this removed the note about adding/removing the u suffix, which we have #7226 filed about. We can keep that ticket open to potentially reinstate something similar. But for now I think it's better with no note at all rather than the misleading one. Especially as allowing abstract case expressions should greatly reduce the occurrence of this error in the first place: we will now accept shaders where the selector is a u32 and the case values are abstract.

Testing
Ran tests. Added new snapshot tests for code that should parse, and new wgsl_errors tests for code that should be rejected.

Squash or Rebase?

Surprise me

Checklist

  • Run cargo fmt.
    - [ ] Run taplo format.
  • Run cargo clippy. If applicable, add:
    - [ ] --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@jamienicol jamienicol force-pushed the abstract-switch-case branch 3 times, most recently from fe422ea to b941b89 Compare February 28, 2025 11:35
@jamienicol jamienicol changed the title Handle automatic type conversions for switch selector and case expressions [naga wgsl-in] Handle automatic type conversions for switch selector and case expressions Feb 28, 2025
@ErichDonGubler ErichDonGubler self-assigned this Feb 28, 2025
Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Some diagnostic nitpicks, and that's it. 🙂 Excited to get this in.

@ErichDonGubler ErichDonGubler added type: bug Something isn't working lang: WGSL WebGPU Shading Language naga Shader Translator area: naga front-end labels Mar 5, 2025
@jamienicol jamienicol force-pushed the abstract-switch-case branch 3 times, most recently from 41a08d1 to b705494 Compare March 6, 2025 09:21
…and case expressions

This allows abstract-typed expressions to be used for some or all of
the switch selector and case selectors. If these are all not
convertible to the same concrete scalar integer type we return an
error. If all the selector expressions are abstract then they are
concretized to i32.

The note previously provided by the relevant error message, suggesting
adding or removing the `u` suffix from case values, has been
removed. While useful for simple literal values, it was comically
incorrect for more complex case expressions. The error message should
still be useful enough to allow the user to easily identify the
problem.
@ErichDonGubler ErichDonGubler enabled auto-merge (squash) March 7, 2025 03:37
@ErichDonGubler ErichDonGubler merged commit 6a61e62 into gfx-rs:trunk Mar 7, 2025
34 checks passed
@jamienicol jamienicol deleted the abstract-switch-case branch March 7, 2025 06:56
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: Done
Development

Successfully merging this pull request may close these issues.

WGSL: switch cases do not attempt automatic type conversion
3 participants