Skip to content

ICE using enum discriminator in constant expression #4926

Closed

Description

@jdm
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

This should be allowed.

Activity

  1. ghost assigned on Feb 20, 2013
  2. added a commit that references this issue on Mar 7, 2013
    95c0747
  3. orenbenkiki commented on Nov 9, 2013

    @orenbenkiki

    Question: Does it make sense to also allow:

    • 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...?)

  4. nikomatsakis commented on Nov 9, 2013

    @nikomatsakis
    Contributor

    On Sat, Nov 09, 2013 at 05:25:33AM -0800, Oren Ben-Kiki wrote:

    Question: Does it make sense to also allow:

    • 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`)?

    Use the FromPrimitive trait, which I believe is derivable.

    • Querying the type system for the total number of constructors
    • (possibly via the type descriptor)? e.g. (hypothetical unrealistic
    • syntax) SomeEnum as uint?

    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):

    trait CLikeEnum  : FromPrimitive, ToPrimitive {
        fn last() -> Self; // Returns the "last" enum, traditionally the one with maximum value
    }
    
  5. orenbenkiki commented on Nov 9, 2013

    @orenbenkiki

    FromPrimitive and ToPrimitive work great, thanks!

    CLikeEnum could probably be renamed to RangeEnum, and, possibly, provide an iterator over all the values, as well as a count/last value?

  6. nikomatsakis commented on Nov 11, 2013

    @nikomatsakis
    Contributor

    CLikeEnum could probably be renamed to RangeEnum, and, possibly, provide an iterator over all the values, as well as a count/last value?

    Yes!

  7. removed their assignment
    on Jun 16, 2014
  8. added a commit that references this issue on Mar 30, 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)E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions