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

cycle errors for unevaluated constants in the param env #79356

Open
lcnr opened this issue Nov 23, 2020 · 6 comments
Open

cycle errors for unevaluated constants in the param env #79356

lcnr opened this issue Nov 23, 2020 · 6 comments
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-generic_const_exprs `#![feature(generic_const_exprs)]` I-cycle Issue: A query cycle occurred while none was expected T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented Nov 23, 2020

// check-pass
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

struct Foo<T>(T);

fn test() where [u8; {
    let _: Foo<[u8; 3 + 4]>;
    3
}]: Sized {}

ends up failing with

error[E0391]: cycle detected when simplifying constant for the type system `test::{constant#0}`
  --> src/lib.rs:7:22
   |
7  |   fn test() where [u8; {
   |  ______________________^
8  | |     let _: Foo<[u8; 3 + 4]>;
9  | |     3
10 | | }]: Sized {
   | |_^
   |
note: ...which requires const-evaluating + checking `test::{constant#0}`...
  --> src/lib.rs:7:22
   |
7  |   fn test() where [u8; {
   |  ______________________^
8  | |     let _: Foo<[u8; 3 + 4]>;
9  | |     3
10 | | }]: Sized {
   | |_^
note: ...which requires type-checking `test::{constant#0}`...
  --> src/lib.rs:7:22
   |
7  |   fn test() where [u8; {
   |  ______________________^
8  | |     let _: Foo<[u8; 3 + 4]>;
9  | |     3
10 | | }]: Sized {
   | |_^
   = note: ...which requires evaluating trait selection obligation `[u8; _]: std::marker::Sized`...
   = note: ...which again requires simplifying constant for the type system `test::{constant#0}`, completing the cycle
note: cycle used when checking that `test` is well-formed
  --> src/lib.rs:10:5
   |
10 | }]: Sized {
   |     ^^^^^

The cycle happens while typechecking the outer array length. This happens because Foo<[u8; 3 + 4]> requires [u8; 3 + 4] to be sized. While trying to fulfill this we see the [u8; ...]: Sized bound in the param_env and try to unify these two. This causes us to evaluate both constants which once again relies on typechecking them.

*This test succeeds with min_const_generics and currently fails with const_generics.

@lcnr
Copy link
Contributor Author

lcnr commented Feb 2, 2021

i think the same problem should also be also responsible for the cycle error in this example:

#![feature(generic_const_exprs)]

trait Foo {
    const N: usize;
}

struct Num<const N: usize>
where
    [(); <Self as Foo>::N]: ;

impl<const N: usize> Foo for Num<N> {
    const N: usize = N - 1;
}

@lcnr
Copy link
Contributor Author

lcnr commented Feb 3, 2021

edit: using <X as Trait>::ASSOC not only requires X: Trait but also all predicates of that trait, which is what's causing the cycle here, so this is a slightly different issue

and

#![feature(generic_const_exprs)]

trait Foo where [(); Self::N]: {
  const N: usize;
}

@lcnr
Copy link
Contributor Author

lcnr commented Feb 10, 2021

from zulip: https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/lazy.20norm.20future.20compat/near/217760369

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

struct Foo<T>(T);

fn test() where [u8; {
    let _: Foo<[u8; 3 + 4]>;
    3
}]: Sized,
[u8; {
    let _: Foo<[u8; 3 + 4]>;
    4
}]: Sized  {}

@lcnr
Copy link
Contributor Author

lcnr commented Feb 10, 2021

the array len from the obligation does not have to be unevaluated.

#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

struct Foo<T>(T);

fn test() where [u8; {
    let _: Foo<[u8; 7]>;
    3
}]: Sized {}

@Dylan-DPC

This comment was marked as outdated.

@lcnr

This comment was marked as outdated.

@fmease fmease added I-cycle Issue: A query cycle occurred while none was expected T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 25, 2024
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-generic_const_exprs `#![feature(generic_const_exprs)]` I-cycle Issue: A query cycle occurred while none was expected 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