You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ICE using enum discriminator in constant expression #4926
rusti> enum A { A1, A2 }
rusti> let a = A1 as uint;
rusti> const b: uint = A1 as uint;
<anon>:37:16: 37:26 error: internal compiler error: Impossible case reached: bad combination of types for cast
<anon>:37 const b: uint = A1 as uint;
^~~~~~~~~~
rust: task failed at 'explicit failure', /run/media/jdm/ssd/rust/src/libsyntax/diagnostic.rs:69
Safe, checked cast in the opposite direction, at least for the case when the enum constructors have no parameters (that is, let index: uint = 0; index as SomeEnum)?
If interfacing with FFI, both conversion directions would be needed... And it also allows writing some nifty generic code that would work for "every enum".
Querying the type system for the total number of constructors (possibly via the type descriptor)? e.g. (hypothetical unrealistic syntax) SomeEnum as uint?
In C/C++ there's a pattern of defining a final fake enum value to allow for querying the compiler for the number of real values. This is OK there because the compiler doesn't enforce switch statements etc. to cover all values. In Rust, adding such a fake value would pollute every match statement on the enum, otherwise it wouldn't be complete.
(Or maybe these are already possible and I just don't know the incantations...?)
Activity
auto merge of #5182 : youknowone/rust/enum-cast, r=brson
orenbenkiki commentedon Nov 9, 2013
Question: Does it make sense to also allow:
let index: uint = 0; index as SomeEnum)?If interfacing with FFI, both conversion directions would be needed... And it also allows writing some nifty generic code that would work for "every enum".
SomeEnum as uint?In C/C++ there's a pattern of defining a final fake enum value to allow for querying the compiler for the number of real values. This is OK there because the compiler doesn't enforce switch statements etc. to cover all values. In Rust, adding such a fake value would pollute every match statement on the enum, otherwise it wouldn't be complete.
(Or maybe these are already possible and I just don't know the incantations...?)
nikomatsakis commentedon Nov 9, 2013
On Sat, Nov 09, 2013 at 05:25:33AM -0800, Oren Ben-Kiki wrote:
Use the FromPrimitive trait, which I believe is derivable.
It appears we don't have a trait for this. I think we should. Probably
we should just have a trait like "CLikeEnum" (probably with a better
name):
orenbenkiki commentedon Nov 9, 2013
FromPrimitiveandToPrimitivework great, thanks!CLikeEnumcould probably be renamed toRangeEnum, and, possibly, provide an iterator over all the values, as well as a count/last value?nikomatsakis commentedon Nov 11, 2013
Yes!
Merge pull request rust-lang#4926 from RalfJung/native-lib-enums