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 detected when computing type, but only when function is const #112602

Open
K4rakara opened this issue Jun 14, 2023 · 2 comments
Open

Cycle detected when computing type, but only when function is const #112602

K4rakara opened this issue Jun 14, 2023 · 2 comments
Assignees
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. 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

@K4rakara
Copy link

I tried this code:

pub struct Parser<H>(H);

impl<H, T> Parser<H>
where
    H: for<'a> Fn(&'a str) -> T,
{
    pub const fn new(handler: H) -> Parser<H> {
        Parser(handler)
    }

    pub const fn many<'s>(
        &'s self,
    ) -> Parser<impl for<'a> Fn(&'a str) -> Vec<T> + 's> {
        Parser::new(|_| unimplemented!())
    }
}

fn main() {
    println!("Hello, world!");
}

Which causes the following error:

error[E0391]: cycle detected when computing type of `<impl at src/main.rs:3:1: 3:21>::many::{opaque#0}`
  --> src/main.rs:13:17
   |
13 |     ) -> Parser<impl for<'a> Fn(&'a str) -> Vec<T> + 's> {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires borrow-checking `<impl at src/main.rs:3:1: 3:21>::many`...
  --> src/main.rs:11:5
   |
11 | /     pub const fn many<'s>(
12 | |         &'s self,
13 | |     ) -> Parser<impl for<'a> Fn(&'a str) -> Vec<T> + 's> {
   | |________________________________________________________^
note: ...which requires const checking `<impl at src/main.rs:3:1: 3:21>::many`...
  --> src/main.rs:11:5
   |
11 | /     pub const fn many<'s>(
12 | |         &'s self,
13 | |     ) -> Parser<impl for<'a> Fn(&'a str) -> Vec<T> + 's> {
   | |________________________________________________________^
   = note: ...which requires computing whether `Parser<<impl at src/main.rs:3:1: 3:21>::many::{opaque#0}>` is freeze...
   = note: ...which requires evaluating trait selection obligation `Parser<<impl at src/main.rs:3:1: 3:21>::many::{opaque#0}>: core::marker::Freeze`...
   = note: ...which again requires computing type of `<impl at src/main.rs:3:1: 3:21>::many::{opaque#0}`, completing the cycle
note: cycle used when checking item types in top-level module
  --> src/main.rs:1:1
   |
1  | / pub struct Parser<H>(H);
2  | |
3  | | impl<H, T> Parser<H>
4  | | where
...  |
19 | |     println!("Hello, world!");
20 | | }
   | |_^

For more information about this error, try `rustc --explain E0391`.
error: could not compile `miri-cycle` (bin "miri-cycle") due to previous error

However, what is peculiar is that if you remove the const keyword from the many function, compilation succeeds, with absolutely no warnings or errors.

Meta

rustc --version --verbose:

rustc 1.72.0-nightly (df77afbca 2023-06-12)
binary: rustc
commit-hash: df77afbcaf3365a32066a8ca4a00ae6fc9a69647
commit-date: 2023-06-12
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5

No backtrace is emitted when I use RUST_BACKTRACE=1 cargo build.

@K4rakara K4rakara added the C-bug Category: This is a bug. label Jun 14, 2023
@jyn514 jyn514 added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-fn A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. labels Jun 15, 2023
@Noratrieb
Copy link
Member

cc @oli-obk, you may know something here

@oli-obk
Copy link
Contributor

oli-obk commented Jul 1, 2023

Ah looks like we're still invoking mir_const_qualif on const fns, even though I don't think we're using the result anymore

@oli-obk oli-obk self-assigned this Jul 1, 2023
@fmease fmease added the I-cycle Issue: A query cycle occurred while none was expected label Jan 25, 2024
@RalfJung RalfJung added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed A-const-fn labels 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-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. 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

Successfully merging a pull request may close this issue.

6 participants