Error on enums with explicit discriminant values in SpacetimeType#4617
Open
clockwork-labs-bot wants to merge 2 commits intomasterfrom
Open
Error on enums with explicit discriminant values in SpacetimeType#4617clockwork-labs-bot wants to merge 2 commits intomasterfrom
clockwork-labs-bot wants to merge 2 commits intomasterfrom
Conversation
SpacetimeDB's SATS encoding assigns variant tags by declaration
order, ignoring Rust discriminant values. An enum like:
#[derive(SpacetimeType)]
enum Foo { A = 5, B = 10 }
would silently encode A as tag 0 and B as tag 1, which is
confusing and error-prone. Emit a compile error instead.
Internal enums that need explicit discriminants for non-codegen
reasons (e.g. as u8 casts) can opt out with
#[sats(allow_discriminants)].
2c0d4e3 to
9b370e6
Compare
Tests that: - Enums with all explicit discriminants produce an error - Enums with a mix of implicit and explicit discriminants produce an error on the explicit variant - Enums with no explicit discriminants compile fine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4618
SATS encoding assigns variant tags by declaration order, ignoring Rust discriminant values. An enum like:
would silently encode
Aas tag 0 andBas tag 1, which causes a mismatch between server and client enum values. This PR makes theSpacetimeTypederive macro emit a compile error when it encounters explicit discriminant values:Internal enums that need explicit discriminants for non-codegen reasons (e.g.
as u8casts) can opt out with#[sats(allow_discriminants)].Includes UI tests for: