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

struct in type of const param causes ICE #74199

Closed
lcnr opened this issue Jul 9, 2020 · 5 comments
Closed

struct in type of const param causes ICE #74199

lcnr opened this issue Jul 9, 2020 · 5 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. 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

@lcnr
Copy link
Contributor

lcnr commented Jul 9, 2020

// check-pass
#![feature(const_generics)]

struct Foo<const N: [u8; {
    struct Foo<const N: usize>;

    impl<const N: usize> Foo<N> {
        fn value() -> usize {
            N
        }
    }

    Foo::<17>::value()
}]>;

fn main() {}

causes an ICE with

warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/main.rs:2:12
  |
2 | #![feature(const_generics)]
  |            ^^^^^^^^^^^^^^
  |
  = note: `#[warn(incomplete_features)]` on by default
  = note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information

error[E0391]: cycle detected when computing type of `Foo::N`
 --> src/main.rs:4:18
  |
4 | struct Foo<const N: [u8; {
  |                  ^
  |
  = note: ...which again requires computing type of `Foo::N`, completing the cycle
note: cycle used when computing type of `Foo`
 --> src/main.rs:4:1
  |
4 | struct Foo<const N: [u8; {
  | ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0391]: cycle detected when computing type of `Foo`
  --> src/main.rs:4:1
   |
4  | struct Foo<const N: [u8; {
   | ^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires computing type of `Foo::N`...
  --> src/main.rs:4:18
   |
4  | struct Foo<const N: [u8; {
   |                  ^
note: ...which requires const-evaluating + checking `Foo::{{constant}}#0`...
  --> src/main.rs:4:26
   |
4  |   struct Foo<const N: [u8; {
   |  __________________________^
5  | |     struct Foo<const N: usize>;
6  | |
7  | |     impl<const N: usize> Foo<N> {
...  |
13 | |     Foo::<17>::value()
14 | | }]>;
   | |_^
note: ...which requires const-evaluating + checking `Foo::{{constant}}#0`...
  --> src/main.rs:4:26
   |
4  |   struct Foo<const N: [u8; {
   |  __________________________^
5  | |     struct Foo<const N: usize>;
6  | |
7  | |     impl<const N: usize> Foo<N> {
...  |
13 | |     Foo::<17>::value()
14 | | }]>;
   | |_^
note: ...which requires const-evaluating `Foo::{{constant}}#0`...
  --> src/main.rs:4:26
   |
4  |   struct Foo<const N: [u8; {
   |  __________________________^
5  | |     struct Foo<const N: usize>;
6  | |
7  | |     impl<const N: usize> Foo<N> {
...  |
13 | |     Foo::<17>::value()
14 | | }]>;
   | |_^
note: ...which requires type-checking `Foo::{{constant}}#0`...
  --> src/main.rs:4:26
   |
4  |   struct Foo<const N: [u8; {
   |  __________________________^
5  | |     struct Foo<const N: usize>;
6  | |
7  | |     impl<const N: usize> Foo<N> {
...  |
13 | |     Foo::<17>::value()
14 | | }]>;
   | |_^
note: ...which requires computing the variances of `Foo::{{constant}}#0::Foo`...
  --> src/main.rs:5:5
   |
5  |     struct Foo<const N: usize>;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...which requires computing the variances for items in this crate...
   = note: ...which again requires computing type of `Foo`, completing the cycle
note: cycle used when collecting item types in top-level module
  --> src/main.rs:2:1
   |
2  | / #![feature(const_generics)]
3  | |
4  | | struct Foo<const N: [u8; {
5  | |     struct Foo<const N: usize>;
...  |
15 | |
16 | | fn main() {}
   | |____________^

error: internal compiler error: src/librustc_typeck/variance/constraints.rs:165:17: `build_constraints_for_item` unsupported for this item
  --> src/main.rs:4:1
   |
4  | / struct Foo<const N: [u8; {
5  | |     struct Foo<const N: usize>;
6  | |
7  | |     impl<const N: usize> Foo<N> {
...  |
13 | |     Foo::<17>::value()
14 | | }]>;
   | |____^

thread 'rustc' panicked at 'Box<Any>', /rustc/8aa18cbdc5d4bc33bd61e2d9a4b643d87f5d21de/src/libstd/macros.rs:13:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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.46.0-nightly (8aa18cbdc 2020-07-08) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

error: aborting due to 3 previous errors; 1 warning emitted
@lcnr lcnr added 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. C-bug Category: This is a bug. A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` labels Jul 9, 2020
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jul 14, 2020
@ayushmishra2005
Copy link
Contributor

@lcnr I would like to fix it. Can you please guide me?

@lcnr
Copy link
Contributor Author

lcnr commented Jul 18, 2020

I am not completely sure what's going wrong here myself yet.

I will try to look into what we can do here soon. If you open a new topic on zulip we can discuss it there.

@lcnr
Copy link
Contributor Author

lcnr commented Jul 18, 2020

Not completely sure why this ICEs after the cycle error, will have to ask about this myself.
edit: cycle errors don't actually cause a fatal error in some cases and return an error value instead, so the above now makes sense to me
We can fix the ice for now by ignoring ty::Error here

_ => {
span_bug!(
tcx.def_span(def_id),
"`build_constraints_for_item` unsupported for this item"
);
}

The cycle error itself seems like a quite difficult problem though x.x

#![feature(const_generics)]
struct Bar<T>(T);

impl<T> Bar<T> {
    const fn value() -> usize {
        42
    }
}

struct Foo<const N: [u8; Bar::<u32>::value()]>;

I don't have the time to think too much about this, but the short overview is the following:

  • we want to evaluate the type of N while we compute the type of Foo.
  • while doing so we actually eval Bar::<u32>::value() somewhat eagerly (for whatever reason), not sure about that one
  • evaluating Bar::<u32>::value() requires the variance of T in Bar<T>.
  • computing variance is a global computation, so getting the variance of Bar<T> computes the variance of Foo<N> at the same time.
  • we need the type of Foo for this, which causes an ICE

This luckily is outside of min_const_generics as proposed in rust-lang/lang-team#37 😅

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jul 31, 2020
Fix Const-Generic Cycle ICE rust-lang#74199

This PR intends to fix the bug in Issue rust-lang#74199 by following the suggestion provided of ignoring the error that causes the ICE.

This does not fix the underlying cycle detection issue, but fixes the ICE.
Also adds a test to check that it doesn't causes an ICE but returns a valid error for now.

r? @lcnr

Edit: Also it's funny how this PR number is an anagram of the issue number
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 31, 2020
Rollup of 6 pull requests

Successful merges:

 - rust-lang#74644 (Remove `linked_list_extras` methods.)
 - rust-lang#74968 (Run all tests if have no specified tests)
 - rust-lang#74982 (1.45.2 release notes)
 - rust-lang#74984 (Miri: fix ICE when unwinding past topmost stack frame)
 - rust-lang#74986 (fix part of comparison that would always evaluate to "true", probably an oversight)
 - rust-lang#74991 (Fix Const-Generic Cycle ICE rust-lang#74199)

Failed merges:

r? @ghost
@Alexendoo
Copy link
Member

Fixed by #74991

@lcnr
Copy link
Contributor Author

lcnr commented Aug 2, 2020

This shouldn't error, will open a new issue for that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` glacier ICE tracked in rust-lang/glacier. 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

4 participants