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

ICE: "index is not an integer-constant expression" using enum as array index. #23513

Closed
theemathas opened this issue Mar 19, 2015 · 4 comments
Closed
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@theemathas
Copy link
Contributor

Code:

enum E { A, }

const C: [u32; 1] = [1];

fn main() {
    let a = C[E::A as usize];
}

playpen

Error message:

<anon>:6:9: 6:10 warning: unused variable: `a`, #[warn(unused_variables)] on by default
<anon>:6     let a = C[E::A as usize];
                 ^
<anon>:6:15: 6:28 error: internal compiler error: index is not an integer-constant expression
<anon>:6     let a = C[E::A as usize];
                       ^~~~~~~~~~~~~
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:130
@theemathas
Copy link
Contributor Author

The following modifications eliminate the ICE:

  • change from const to static
  • give explicit discriminant (enum E { A=0, })
  • use a temporary variable to store E::A or E::A as usize

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Mar 19, 2015
@theemathas
Copy link
Contributor Author

Possibly related to #5873

bors added a commit that referenced this issue Jul 9, 2015
Fixes #23302.

Note that there's an odd situation regarding the following, most likely due to some inadequacy in `const_eval`:

```rust
enum Y {
    A = 1usize,
    B,
}
```

In this case, `Y::B as usize` might be considered a constant expression in some cases, but not others.  (See #23513, for a related problem where there is only one variant, with no discriminant, and it doesn't behave nicely as a constant expression either.)

Most of the complexity in this PR is basically future-proofing, to ensure that when `Y::B as usize` is fully made to be a constant expression, it can't be used to set `Y::A`, and thus indirectly itself.
@frewsxcv
Copy link
Member

Visiting for triage: this is still an issue

@steveklabnik
Copy link
Member

This was fixed by #30714 👍

steveklabnik added a commit to rust-lang/glacier that referenced this issue Feb 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

No branches or pull requests

3 participants