Skip to content

support explicit discriminant numbers on tag variants #1393

Closed

Description

@graydon

Currently tags always use 0 ... N, where N is the number of tag variants, as the discriminant values.

This makes them useless for modeling C enums, which they are the natural analogue of. We should support a form of variant-declarator that provides a discriminant value, as (say) an uint const expression or something.

Ideal sort of example:

tag color {
red = 0xff;
blue = 0xfe;
}

It'd also be nice to be able to cast back and forth against uint values, and similar "like in C" enum tricks. But one thing at a time.

Activity

  1. kevina commented on Dec 29, 2011

    @kevina
    Contributor

    I'll look into this. Expect something in the next couple of days.

  2. graydon commented on Dec 30, 2011

    @graydon
    ContributorAuthor

    see Issue #1397.

  3. nikomatsakis commented on Dec 30, 2011

    @nikomatsakis
    Contributor

    Should it be legal to have both associated data and an explicit discriminant value?

  4. nikomatsakis commented on Dec 30, 2011

    @nikomatsakis
    Contributor

    My opinion: why not? Also, the discriminant value ought to increase by 1 from the previous discriminant, just as in an enum. So:

    tag t {
    a = 1; // discriminant == 1
    b = 10; // discriminant == 10
    c(uint); // discriminant == 11
    d(uint) = 15; // discriminant == 15
    }

  5. kevina commented on Dec 31, 2011

    @kevina
    Contributor

    I'm not sure on the value of having both data and discriminant values, and even less sure on the syntax, for now I'll disallow it by not supporting the syntax for it.

    Also, my patch does increase by 1 when not specified just like the C enum does as it is trivial to implement.

  6. brson commented on Jan 24, 2012

    @brson
    Contributor

    @kevina - This is done?

  7. brson commented on Jan 24, 2012

    @brson
    Contributor

    I'm told it's as done as it's going to get. Yay!

  8. added a commit that references this issue on Oct 9, 2023
  9. added 2 commits that reference this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions