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

enum entries are not compile-time constants #31300

Closed
ghost opened this issue Jan 30, 2016 · 5 comments
Closed

enum entries are not compile-time constants #31300

ghost opened this issue Jan 30, 2016 · 5 comments
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ghost
Copy link

ghost commented Jan 30, 2016

pub enum Foo {
    a,
    b,
    CAPACITY,
}

fn main() {
    let arr = [0; Foo::CAPACITY as usize];
}
<anon>:8:19: 8:41 error: expected constant integer for repeat count, but non-constant path in constant expression [E0307]
<anon>:8     let arr = [0; Foo::CAPACITY as usize];
                           ^~~~~~~~~~~~~~~~~~~~~~
@dirk
Copy link
Contributor

dirk commented Jan 30, 2016

@Kingsquee: Quick triage: this looks to be an issue in how the compiler generates and assigns the discriminant (0, 1, 2, etc.) for each field. If you specify these yourself (see below) it will compile just fine.

pub enum Foo {
    A = 0,
    B = 1,
    CAPACITY = 2,
}

fn main() {
    let arr = [0; Foo::CAPACITY as usize];
}

@ghost
Copy link
Author

ghost commented Feb 1, 2016

@dirk Thanks, good to know!

@dirk
Copy link
Contributor

dirk commented Feb 3, 2016

@Kingsquee: Glad to help. 😄

In regards to the original issue: is predictable auto-assignment of descriminants for enums a feature that should be introduced into the language?

@steveklabnik: Would there be a better place to start/revive this discussion than in this issue?

@mitaa
Copy link
Contributor

mitaa commented Feb 4, 2016

This issue is tracked here #23898 (this is iow a duplicate)

@ghost
Copy link
Author

ghost commented Feb 4, 2016

@mitaa Thanks, I'll close this issue then.

@ghost ghost closed this as completed Feb 4, 2016
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 24, 2017
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants