rust-lang / rust-lang/rust

[generic_const_exprs] False positive E0391 cycle when `const fn` `where` clause references trait with const param

Open
#153,287 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

A false positive E0391 cycle error occurs when a struct has a const generic parameter of array type, and its where clause calls a const fn that itself has a where clause referencing a const expression.

Code

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

pub struct Trait<const B: bool>;

pub struct Y<const V: [(); 3]>
where
    Trait<{ foo(&V) }>:, {}

const fn foo<const N: usize>(x: &[(); N]) -> bool
where
    Trait<{ N == 3 }>:,
{
    true
}

fn main() {}

Error

error[E0391]: cycle detected when building an abstract representation for `Y::{constant#1}`
 --> src\main.rs:8:11
  |
8 |     Trait<{ foo(&V) }>:, {}
  |           ^^^^^^^^^^^
  |
note: ...which requires building THIR for `Y::{constant#1}`...
 --> src\main.rs:8:11
  |
8 |     Trait<{ foo(&V) }>:, {}
  |           ^^^^^^^^^^^
note: ...which requires type-checking `Y::{constant#1}`...
 --> src\main.rs:8:11
  |
8 |     Trait<{ foo(&V) }>:, {}
  |           ^^^^^^^^^^^
  = note: ...which again requires building an abstract representation for `Y::{constant#1}`, completing the cycle
note: cycle used when checking that `Y` is well-formed
 --> src\main.rs:6:1
  |
6 | pub struct Y<const V: [(); 3]>
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Notes

There is no actual semantic cycle here — this appears to be a false positive. The interaction between a const generic of array type ([(); N]) and a nested where clause const expression in the called const fn seems to confuse the abstract representation builder.

Version

rustc 1.96.0-nightly (80381278a 2026-03-01)

Related Issues

  • #92961 — False positive cycle detection with generic_const_exprs
  • #106994 — generic_const_exprs does not work properly with const/type param defaults
  • #134978 — Cycle error with const expression in function where clause

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 false-positive E0391 using the provided src/main.rs example, then trace the reported cycle through abstract representation building, THIR construction, and type-checking for Y::{constant#1}. Compare the behavior with related issues #92961, #106994, and #134978; done means this valid example no longer reports a cycle and the regression is covered by a test.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.