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

Internal compiler error when attempting to implement an associated const with a local variable #44239

Closed
petertodd opened this issue Sep 1, 2017 · 1 comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@petertodd
Copy link
Contributor

Specifically this code:

trait Foo {
    const N: usize;
}

fn main() {
    struct MyFoo;

    let n = 0;

    impl Foo for MyFoo {
        const N: usize = n;
    }   
}

results in the following error message:

$ rustc test.rs 
error: internal compiler error: /checkout/src/librustc_typeck/check/mod.rs:1846: no type for local variable local n (id=15)
  --> test.rs:11:26
   |
11 |         const N: usize = n;
   |                            ^

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: rustc 1.21.0-nightly (97b01abf3 2017-08-31) running on x86_64-unknown-linux-gnu

thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc_errors/lib.rs:439:8
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Expected behavior: While I can understand if this level of meta isn't possible, it should give a proper error message! :)

@alexcrichton alexcrichton added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Sep 1, 2017
@shepmaster shepmaster added A-const-fn A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 1, 2017
@seventh-chord
Copy link
Contributor

seventh-chord commented Oct 14, 2017

Just to note, a slightly simpler case which triggers the same error is to have the associated const in a impl block for the struct directly:

fn main() {
    let n = 0;

    struct Foo;
    impl Foo {
        const N: usize = n;
    }   
}

Error message:

error: internal compiler error: /checkout/src/librustc_typeck/check/mod.rs:1785: no type for local variable local n (id=8)
 --> test.rs:6:26
  |
6 |         const N: usize = n;
  |                          ^

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: rustc 1.22.0-nightly (02a24dbdd 2017-10-13) running on x86_64-unknown-linux-gnu

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc_errors/lib.rs:439:8
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace: (I have cut out the stacktrace for brevity)

bors added a commit that referenced this issue Oct 22, 2017
…etrochenkov

Cleanly error for non-const variable in associated const

Not sure if wrapping the whole `visit::walk_impl_item` call is correct.
Closes #44239
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ 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

5 participants