Skip to content

Recursive types with Debug derive cause type check overflow #484

Open

Description

@skius

This might be an inherent limitation in the Rust compiler, but trying to make recursive types derive Debug even if generic type arguments are not Debug needs a hacky workaround:

struct Undebuggable(fn());

#[derive(derive_more::Debug)]
enum Expr<A> {
    #[debug("Atom(?)")]
    Atom(A),
    If(Branches<A>),
}

#[derive(derive_more::Debug)]
struct Branches<A> {
    // remove this attribute to cause compile error:
/*```
error[E0275]: overflow evaluating the requirement `Expr<Undebuggable>: Debug`
   --> playground/derive_more_debug_test/src/main.rs:25:5
    |
25  |     dbg!(expr);
    |     ^^^^^^^^^^
    |
    = note: required for `Vec<Expr<Undebuggable>>` to implement `Debug`
    = note: 2 redundant requirements hidden
    = note: required for `&Branches<Undebuggable>` to implement `Debug`
note: required by a bound in `derive_more::core::fmt::rt::Argument::<'_>::new_debug`
   --> /home/nius/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/rt.rs:120:25
    |
120 |     pub fn new_debug<T: Debug>(x: &T) -> Argument<'_> {
    |                         ^^^^^ required by this bound in `Argument::<'_>::new_debug`
    = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0275`.
error: could not compile `derive_more_debug_test` (bin "derive_more_debug_test") due to 1 previous error    
```*/
    #[debug("[{}]", exprs.iter().map(|e| format!("{e:?}")).collect::<Vec<_>>().join(", "))]
    exprs: Vec<Expr<A>>,
}


// ... code that tries to debug `Expr<Undebuggable>` ...

See here for a cloneable repro repo: https://github.com/skius/derive_more_debug_test/blob/main/src/main.rs

Is there a way to do this (derive Debug with potentially non-Debug generic arguments in recursive types) with a less hacky workaround?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions