[naga wgsl-in] Handle automatic type conversions for switch selector and case expressions #7250
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
cargo fmt
.- [ ] Runtaplo format
.cargo clippy
. If applicable, add:- [ ]--target wasm32-unknown-unknown
cargo xtask test
to run tests.CHANGELOG.md
. See simple instructions inside file.