Uh oh!
There was an error while loading. Please reload this page.
Implement a discriminant_value intrinsic - #20907
Conversation
Implements an intrinsic for extracting the value of the discriminant enum variant values. For non-enum types, this returns zero, otherwise it returns the value we use for discriminant comparisons. This means that enum types that do not have a discriminant will also work in this arrangement.
rust-highfive
commented
Jan 11, 2015
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
sfackler
commented
Jan 11, 2015
cc #20856 |
Aatch
commented
Jan 11, 2015
For comparison, with @sfackler's |
alexcrichton
commented
Jan 11, 2015
cc #13860, a discussion on a topic somewhat related to this awhile ago. Note that for |
sfackler
commented
Jan 11, 2015
How hard would it be to generate an error when monomorphizing |
luqmana
commented
Jan 11, 2015
@sfackler it would be simple enough, the |
huonw
commented
Jan 11, 2015
That sounds good to me, although I think we would then have to restrict Possible optimisations like #14540 may mean that the rules have to be quite restrictive, but that doesn't seem like such a problem. However, it would be unstable at 1.0 and is mostly an implementation detail, but using it in |
alexcrichton
commented
Jan 13, 2015
I agree with @huonw that monomorphization should never generate a compile-time error wherever reasonable. One other possibility could be perhaps to return I'm also a little curious about how we'd spec this in terms of enumerations such as |
sfackler
commented
Jan 13, 2015
|
Aatch
commented
Jan 14, 2015
Just to clarify for those that aren't as familiar with this part of the compiler internals, we always have a discriminant value for an enum, sometimes we read from a memory location, sometimes we do a comparison against a pointer value, but there is always an discriminant. Right now, I don't think the specific values need to be specced at all. I think that, if we decide to spec anything here at all, it should simply be that the value you get is guaranteed only to be the same value as any another instance of the variant of the same type. This does mean we can't use the value for ordering, but it also means we don't have to worry about the various representations. |
nikomatsakis
commented
Jan 14, 2015
I'm not sure there's a problem with giving an actual integer result, and spec'ing that the results count from 0, but we can be cautious here for the time being. It's hard for me to imagine a representation that wouldn't efficiently allow us to get to an integer ( |
alexcrichton
commented
Jan 20, 2015
(previous comment: #21186 (comment)) Ok, we also ended up chatting about this PR in the meeting today as well! We think that a similar strategy may be able to happen here as well. As is this is a pretty unobtrusive feature, and the perf wins it may gain us are certainly quite enticing! Our conclusion was that if we have a concrete use case for this in the standard libraries that it'd be a good thing to land, but otherwise it may want to hold of for an RFC. I'd of course be willing to help out writing an RFC and sheperding it through as well! In general we'd just want to make sure that it's put to good use in the standard library before landing an implementation in the compiler. Thanks again for the awesome perf work here! |
taralx
commented
Jan 21, 2015
Went looking for a "get discriminant" trait today in order to implement EnumSet. If this were a (compiler-generated?) trait instead of an intrinsic, the monomorphization wouldn't be a problem... |
alexcrichton
commented
Jan 27, 2015
cc rust-lang/rfcs#639 (corresponding RFC) |
bors
commented
Mar 9, 2015
☔ The latest upstream changes (presumably #23153) made this pull request unmergeable. Please resolve the merge conflicts. |
alexcrichton
commented
Mar 17, 2015
For now I'm going to close this in favor of the RFC to see what happens there. |
Implements an intrinsic for extracting the value of the discriminant
enum variant values. For non-enum types, this returns zero, otherwise it
returns the value we use for discriminant comparisons. This means that
enum types that do not have a discriminant will also work in this
arrangement.