rust-lang / rust-lang/rust

Loop detection error for duplicate const functions

Open
#137,521 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-generic_const_exprs P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=396a3e397abbf6d5d0f417ad2dd21575

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

const fn wrap<const N: usize>() -> usize {
    0
}

const fn inner<const N: usize>() -> usize {
    0
}

const fn flip_magic_base_inner<const N: usize>()
    -> !
    where [(); wrap::<{ inner::<N>() }>()] : {
    todo!()
}

I expected to see this happen: no error

The following code, which is essentially the same, can compile.
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=a568ce2d4a1d358435f825aa172c4440

Instead, this happened:

Compiling playground v0.0.1 (/playground)
error[E0391]: cycle detected when building an abstract representation for `flip_magic_base_inner::{constant#0}`
  --> src/lib.rs:14:16
   |
14 |     where [(); wrap::<{ inner::<N>() }>()] : {
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: ...which requires building THIR for `flip_magic_base_inner::{constant#0}`...
  --> src/lib.rs:14:16
   |
14 |     where [(); wrap::<{ inner::<N>() }>()] : {
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `flip_magic_base_inner::{constant#0}`...
  --> src/lib.rs:14:16
   |
14 |     where [(); wrap::<{ inner::<N>() }>()] : {
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: ...which again requires building an abstract representation for `flip_magic_base_inner::{constant#0}`, completing the cycle
note: cycle used when checking that `flip_magic_base_inner` is well-formed
  --> src/lib.rs:12:1
   |
12 | / const fn flip_magic_base_inner<const N: usize>()
13 | |     -> !
14 | |     where [(); wrap::<{ inner::<N>() }>()] : {
   | |____________________________________________^
   = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

For more information about this error, try `rustc --explain E0391`.
error: could not compile `playground` (lib) due to 1 previous error

There is something wrong with it because it can be compiled if it is not duplicated.
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=aba90728c00aaa4808ecd5762ae9fa26

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (f8a913b13 2025-02-23)
binary: rustc
commit-hash: f8a913b1381e90379c7ca63ac2b88b9518936628
commit-date: 2025-02-23
host: x86_64-pc-windows-msvc
release: 1.87.0-nightly
LLVM version: 20.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the error from the linked playground example in src/lib.rs, then start with the rustc-dev-guide query and cycle documentation referenced in the diagnostic. Trace the abstract-representation, THIR, and type-checking steps named in the error. Done means the duplicated const-function case no longer reports a spurious E0391 cycle while the non-duplicated cases remain compilable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.