Skip to content

expressions in the type of const params can cause cycle errors #75047

Closed
@lcnr

Description

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

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

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

This results in

warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![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`
  --> src/lib.rs:10:1
   |
10 | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires computing type of `Foo::N`...
  --> src/lib.rs:10:18
   |
10 | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   |                  ^
note: ...which requires const-evaluating + checking `Foo::{{constant}}#0`...
  --> src/lib.rs:10:26
   |
10 | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   |                          ^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `Foo::{{constant}}#0`...
  --> src/lib.rs:10:26
   |
10 | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   |                          ^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating `Foo::{{constant}}#0`...
  --> src/lib.rs:10:26
   |
10 | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   |                          ^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `Foo::{{constant}}#0`...
  --> src/lib.rs:10:26
   |
10 | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   |                          ^^^^^^^^^^^^^^^^^^^
note: ...which requires computing the variances of `Bar`...
  --> src/lib.rs:2:1
   |
2  | struct Bar<T>(T);
   | ^^^^^^^^^^^^^^^^^
   = 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/lib.rs:1:1
   |
1  | / #![feature(const_generics)]
2  | | struct Bar<T>(T);
3  | |
4  | | impl<T> Bar<T> {
...  |
9  | |
10 | | struct Foo<const N: [u8; Bar::<u32>::value()]>;
   | |_______________________________________________^

error: aborting due to previous error; 1 warning emitted

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions