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

Using associated constant as array length: without (funny error) and with (ICE) const generics #68205

Closed
birkenfeld opened this issue Jan 14, 2020 · 3 comments · Fixed by #68763
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. glacier ICE tracked in rust-lang/glacier. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@birkenfeld
Copy link
Contributor

birkenfeld commented Jan 14, 2020

Given the following test and nightly 2020-01-12:

#![allow(unused)]
#![feature(const_generics)]

trait Child {
    const N: usize;
}

struct C1;
impl Child for C1 {
    const N: usize = 2;
}

struct Main<C: Child> {
    c: C
}

impl<C: Child> Main<C> {
    fn test() -> [u8; C::N] {
        [0; C::N]
    }
}

I get that this is not supposed to work without const generics, but the error message suggestion is very funny:

error[E0599]: no associated item named `N` found for type parameter `C` in the current scope
  --> src/main.rs:22:27
   |
22 |     fn test2() -> [u8; C::N] {
   |                           ^ associated item not found in `C`
   |
   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `N`, perhaps you need to restrict type parameter `C` with it:
   |
17 | impl<C: Child + Child> Main<C> {
   |      ^^^^^^^^^^

With const generics:

warning: Error finalizing incremental compilation session directory `/home/gbr/devel/rust-test/target/debug/incremental/rust_test-xwudz02f6bay/s-fjpbjlpyxj-1ihvwt0-working`: No such file or directory (os error 2)

error: internal compiler error: cat_expr Errd
  --> src/main.rs:22:30
   |
22 |       fn test2() -> [u8; C::N] {
   |  ______________________________^
23 | |         [0; C::N]
24 | |     }
   | |_____^

error: internal compiler error: cat_expr Errd
  --> src/main.rs:23:9
   |
23 |         [0; C::N]
   |         ^^^^^^^^^

error: internal compiler error: PromoteTemps: MIR had errors
  --> src/main.rs:22:5
   |
22 | /     fn test2() -> [u8; C::N] {
23 | |         [0; C::N]
24 | |     }
   | |_____^

error: internal compiler error: broken MIR in DefId(0:16 ~ rust_test[3df1]::{{impl}}[1]::test2[0]) ("return type"): bad type [type error]
  --> src/main.rs:22:5
   |
22 | /     fn test2() -> [u8; C::N] {
23 | |         [0; C::N]
24 | |     }
   | |_____^

error: internal compiler error: broken MIR in DefId(0:16 ~ rust_test[3df1]::{{impl}}[1]::test2[0]) (LocalDecl { mutability: Mut, local_info: Other, internal: false, is_block_tail: None, ty: [type error], user_ty: UserTypeProjections { contents: [] }, source_info: SourceInfo { span: src/main.rs:22:5: 24:6, scope: scope[0] } }): bad type [type error]
  --> src/main.rs:22:5
   |
22 | /     fn test2() -> [u8; C::N] {
23 | |         [0; C::N]
24 | |     }
   | |_____^

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:346:17
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

error: internal compiler error: unexpected panic

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.42.0-nightly (3ebcfa145 2020-01-12) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=2 -C incremental --crate-type bin

note: some of the compiler flags provided by cargo are hidden
@JohnTitor JohnTitor added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 14, 2020
@Shadlock0133
Copy link
Contributor

Weird suggestion is on current stable as well.

@Alexendoo
Copy link
Member

Alexendoo commented Jan 24, 2020

The ICE component is fixed by #68388, the diagnostics issue remains (Child + Child)

@Alexendoo Alexendoo added A-diagnostics Area: Messages for errors, warnings, and lints D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Jan 24, 2020
@JohnTitor JohnTitor added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` labels Feb 2, 2020
@JohnTitor
Copy link
Member

I think it's another example of #68695 now.

Centril added a commit to Centril/rust that referenced this issue Feb 2, 2020
@bors bors closed this as completed in 2e1790d Feb 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. glacier ICE tracked in rust-lang/glacier. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants