Skip to content

Error on enums with explicit discriminant values in SpacetimeType#4617

Open
clockwork-labs-bot wants to merge 2 commits intomasterfrom
bot/reject-enum-discriminants
Open

Error on enums with explicit discriminant values in SpacetimeType#4617
clockwork-labs-bot wants to merge 2 commits intomasterfrom
bot/reject-enum-discriminants

Conversation

@clockwork-labs-bot
Copy link
Collaborator

@clockwork-labs-bot clockwork-labs-bot commented Mar 11, 2026

Fixes #4618

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 causes a mismatch between server and client enum values. This PR makes the SpacetimeType derive macro emit a compile error when it encounters explicit discriminant values:

error: explicit discriminant values are not supported by SpacetimeDB; SATS assigns variant tags by declaration order, so discriminant values would be silently ignored
  --> src/lib.rs:3:7
   |
3  |     A = 5,
   |       ^

Internal enums that need explicit discriminants for non-codegen reasons (e.g. as u8 casts) can opt out with #[sats(allow_discriminants)].

Includes UI tests for:

  • Enums with all explicit discriminants (error on each)
  • Mixed implicit/explicit discriminants (error only on explicit)
  • Plain enums with no discriminants (compiles fine)

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)].
@clockwork-labs-bot clockwork-labs-bot force-pushed the bot/reject-enum-discriminants branch from 2c0d4e3 to 9b370e6 Compare March 11, 2026 19:26
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codegen silently ignores explicit enum discriminant values

1 participant