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 errors lead to confusing errors referencing rustc-dev-guide (i.e., not user-facing docs) #130032

Open
jamesmunns opened this issue Sep 6, 2024 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jamesmunns
Copy link
Member

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.

@jamesmunns jamesmunns added the C-bug Category: This is a bug. label Sep 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 6, 2024
@theemathas
Copy link
Contributor

FYI, this compiles

struct NamedType {
    parent: &'static NamedType,
}

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

@jamesmunns
Copy link
Member Author

@theemathas the actual trait (in my real code) looks more like this:

trait Schema {
    const SCHEMA: &'static NamedType;
}

And you can't reference statics in consts:

struct NamedType {
    parent: &'static NamedType,
}

struct NamedValue;

trait Schema {
    const SCHEMA: &'static NamedType;
}
impl Schema for NamedValue {
    const SCHEMA: &'static NamedType = &BODGE;
}

static BODGE: NamedType = NamedType {
    parent: NamedValue::SCHEMA,
};
error[E0658]: referencing statics in constants is unstable
  --> src/lib.rs:11:41
   |
11 |     const SCHEMA: &'static NamedType = &BODGE;
   |                                         ^^^^^
   |
   = note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
   = note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
   = help: to fix this, the value can be extracted to a `const` and then used.

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

The code linked above is just a minimization that triggers the same "cycle error" warning.

@lolbinarycat lolbinarycat added A-diagnostics Area: Messages for errors, warnings, and lints T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-confusing Diagnostics: Confusing error or lint that should be reworked. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 6, 2024
@RalfJung
Copy link
Member

Yes this is expected. statics are places and support recursion (you can mention a place without knowing the value stored inside of it) but constants are values and cannot meaningfully support recursion (you cannot mention a value without knowing the value).

but maybe give a helpful error

What error would you expect? The error tells you that you have a cyclic dependency in your code.

referencing statics in constants is unstable

This will become stable fairly soon: #119618.

Cc @rust-lang/wg-const-eval

@RalfJung
Copy link
Member

I guess the point is that the cycle error looks a bit like it is intended for rustc devs, not users, with its reference to the dev guide and all that? That's fair. I don't know why the error looks like that... @lcnr might know?

@lcnr
Copy link
Contributor

lcnr commented Sep 26, 2024

yeah, not sure why we link to the dev guide and I agree that it's a bit odd. I am unsure whether we have another explanation of query cycles? 🤔 it's just part of the query_system_cycle diagnostic, so we could freely change it to something else

@RalfJung RalfJung changed the title Recursive const definitions do not work, and give errors referencing rustc-dev-guide Cycle errors lead to confusing errors referencing rustc-dev-guide (i.e., not user-facing docs) Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants