Skip to content

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

Open
@jamesmunns

Description

@jamesmunns

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: jamesmunns/postcard@ded8c30

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions