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

support explicit discriminant numbers on tag variants #1393

Closed
graydon opened this issue Dec 29, 2011 · 7 comments
Closed

support explicit discriminant numbers on tag variants #1393

graydon opened this issue Dec 29, 2011 · 7 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR)

Comments

@graydon
Copy link
Contributor

graydon commented Dec 29, 2011

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.

@kevina
Copy link
Contributor

kevina commented Dec 29, 2011

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

@graydon
Copy link
Contributor Author

graydon commented Dec 30, 2011

see Issue #1397.

@nikomatsakis
Copy link
Contributor

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

@nikomatsakis
Copy link
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
}

@kevina
Copy link
Contributor

kevina commented Dec 31, 2011

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.

kevina added a commit to kevina/rust that referenced this issue Jan 10, 2012
Addresses issue rust-lang#1393.

For now disallow disr. values unless all variants use nullary
contractors (i.e. "enum-like").

Disr. values are now encoded in the crate metadata, but only when it
will differ from the inferred value based on the order.
graydon pushed a commit that referenced this issue Jan 11, 2012
Addresses issue #1393.

For now disallow disr. values unless all variants use nullary
contractors (i.e. "enum-like").

Disr. values are now encoded in the crate metadata, but only when it
will differ from the inferred value based on the order.
kevina added a commit to kevina/rust that referenced this issue Jan 11, 2012
@brson
Copy link
Contributor

brson commented Jan 24, 2012

@kevina - This is done?

@brson
Copy link
Contributor

brson commented Jan 24, 2012

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

@brson brson closed this as completed Jan 24, 2012
bjorn3 added a commit to bjorn3/rust that referenced this issue Oct 9, 2023
organize import sections with rustfmt `group_imports`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR)
Projects
None yet
Development

No branches or pull requests

4 participants