rust-lang / rust-lang/rust

Cycle errors lead to confusing errors referencing rustc-dev-guide (i.e., not user-facing docs)

Open
#130,032 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug D-confusing T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

struct NamedType {
    parent: &'static NamedType,
}

const SCHEMA: &'static NamedType = &NamedType {
    parent: SCHEMA,
};

I expected to see this happen: Maybe "just work", but maybe give a helpful error

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error[E0391]: cycle detected when simplifying constant for the type system `SCHEMA`
 --> src/lib.rs:5:1
  |
5 | const SCHEMA: &'static NamedType = &NamedType {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: ...which requires const-evaluating + checking `SCHEMA`...
 --> src/lib.rs:6:13
  |
6 |     parent: SCHEMA,
  |             ^^^^^^
  = note: ...which again requires simplifying constant for the type system `SCHEMA`, completing the cycle
  = note: cycle used when running analysis passes on this crate
  = 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
Meta

Reproduces on 1.81.0 stable and 2024-09-05 nightly.

Bigger picture

I'm working on "Schema"s (which are kind of home-rolled reflection) for postcard, and I attempted to implement the Schema trait for the types used for schemas themselves.

Actual commit here: https://github.com/jamesmunns/postcard/commit/ded8c308cc506bca87fb2c20be0928a8e904c3c9

I'm at least partially convinced this can't actually work in my case (it ends up making infinitely recursive consts), but the error message was somewhat surprising (I don't know if "normal" user errors should reference the rustc-dev-guide), and maybe would expect a more direct error like "Tried to make a constant value with infinite size due to recursion".

CC @dirbaio who helped minimize this.

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

Start by reproducing the provided recursive-constant example on stable or nightly and inspect the E0391 diagnostic, including the rustc --explain E0391 output. Read the referenced rustc-dev-guide overview and query pages to understand why they appear. Done means the cycle remains diagnosable while the user-facing error no longer directs ordinary users to internal compiler documentation.

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.