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: 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.