Skip to content

enum variants aren't considered to be const #5873

Closed

Description

@doy

This compiles:

static i: int = 4;

fn main () {
    let x: [int, ..i] = [1, 2, 3, 4];
    println(fmt!("%?", x));
}

But this doesn't:

enum Foo {
    Bar = 4,
}
static i: int = Bar as int;

fn main () {
    let x: [int, ..i] = [1, 2, 3, 4];
    println(fmt!("%?", x));
}
test2.rs:7:11: 7:21 error: expected constant expr for vector length: Can't cast str to int
test2.rs:7     let x: [int, ..i] = [1, 2, 3, 4];
                      ^~~~~~~~~~

That error message doesn't seem to make sense, but that may be because it's just confused - I also get this error message in other circumstances, and I don't understand what the difference is:

src/info/builtin.rs:79:11: 79:52 error: expected constant expr for vector length: Non-constant path in constant expr
src/info/builtin.rs:79 static db: [[Option<&'static str>, ..ncap], ..nterm] = [
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Activity

  1. added a commit that references this issue on Apr 16, 2013
  2. catamorphism commented on Jun 3, 2013

    @catamorphism
    Contributor

    It seems like the constant checker believes that Bar is a constant, whereas the constant evaluator does not. They should agree. I'm not sure which behavior is right. Also, it seems counter-intuitive that you can't refer to a constant (i) in other constants.

    Nominating for milestone 1, welll-defined.

  3. huonw commented on Jul 30, 2013

    @huonw
    Contributor

    Works now. Fixed by 877bba9 (possibly; at the very least, it includes a testcase).

  4. tikue commented on Aug 28, 2013

    @tikue
    Contributor

    This doesn't seem to work now.

    enum Category {
        A,
        B,
        C,
        NumCategories,
    }
    static NUM_CATEGORIES: uint = NumCategories as uint;
    
    fn main() {
        let the_categories: [Category, ..NUM_CATEGORIES] = [A, B, C];
        printfln!(the_categories);
    }
    

    $ x86_64-apple-darwin/stage2/bin/rust run static_test.rs
    static_test.rs:10:24: 10:52 error: expected constant expr for vector length: Non-constant path in constant expr
    static_test.rs:10 let the_categories: [Category, ..NUM_CATEGORIES] = [A, B, C];

  5. reopened this on Aug 28, 2013
  6. jdm commented on Aug 28, 2013

    @jdm
    Contributor

    I'm doubtful that the earlier testcase ever worked; 877bba9 explicitly calls out in the commit message that C style enums don't work in type signatures, only values.

  7. pnkfelix commented on Aug 29, 2013

    @pnkfelix
    Contributor

    Part of #5551; inherits its maturity milestone implicitly.

  8. 17 remaining items

  9. pnkfelix commented on Mar 8, 2015

    @pnkfelix
    Contributor

    I believe the phrase "C-like enum variant" is meant to refer to variants that have been given an explicit value.

    I might be wrong on this point; I will check more carefully when I am in front of a computer again

  10. pnkfelix commented on Mar 8, 2015

    @pnkfelix
    Contributor

    @tommit I was not attempting to claim that it was a language feature that these aren't compile-time constants. (I do concede that my earlier response can be interpreted that way.)

    I was trying to claim that changing the current language to treat them as compile-time constants would be a language feature, and thus should go through the RFC process. (After all, a ticket like this really isn't the appropriate venue to debate the value of such a change.)


    Here's the essence of what I was thinking when I wrote that response: I have been long under the impression that we were reserving the right to change how discriminant values are assigned by the compiler if not explicitly assigned by programmer.

    (Of course actually allowing such a change would probably require restricting the current language; i.e. Variant as Type when Variant would need to be outlawed during dynamic execution in addition to it being disallowed at compile time. And I imagine we could not do that unless we simultaneously accepted rust-lang/rfcs#639 and probably provided a stable API to that RFC as well.)

    Its quite possible that my mental model of what we were planning to permit in the future was simply over-aggressive; i.e. that no one else on the team was planning on ever changing the assignment of discriminant values, at least not for enum variants that do not have a data payload attached.

  11. pnkfelix commented on Mar 8, 2015

    @pnkfelix
    Contributor

    reopening and putting on agenda for next meeting; this deserves discussion at the team meeting, if only to get clarification about what guarantees we plan with respect to discriminant value assignment.

  12. nikomatsakis commented on Mar 10, 2015

    @nikomatsakis
    Contributor

    Hmm, I tend to agree with @tommit that it should work for both explicitly assigned and unassigned values, if it works at all.

  13. nikomatsakis commented on Mar 10, 2015

    @nikomatsakis
    Contributor

    (I didn't fully appreciate when commenting before that it worked only in one case and not the other.)

  14. pnkfelix commented on Mar 12, 2015

    @pnkfelix
    Contributor

    So after discussing with @nikomatsakis i concluded that my mental model is simply wrong, and that there is not sufficient value in trying to future-proof our future discriminant value assignment. In particular @nikomatsakis pointed out to me multiple times that the chosen discriminant value need not be directly represented as the numeric value chosen; thus various tricks I was anticipating (usually revolving around ensuring that e.g. bit patterns are non-zero) should be able to be adopted in the future.

    Anyway, the bugs been reopened; we should fix this, for consistency's sake.

  15. pnkfelix commented on Mar 12, 2015

    @pnkfelix
    Contributor

    triage: P-high

    (In some sense this is a feature request, or at least a request for consistently deploying a particular feature. Fixing this may be important when/if const fn comes, but its hard to tell right now.)

  16. added
    P-medium
    on Mar 19, 2015
  17. pnkfelix commented on Mar 31, 2015

    @pnkfelix
    Contributor

    Decided to file a fresh issue, #23898, to ease triage. (I.e. right now the main item left on this bug is hard to actually extract from the thread of conversation as presented.)

  18. added a commit that references 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-type-systemP-medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions