Skip to content

format! debug fails even though Debug is implemented #72197

Closed
@vitorenesduarte

Description

@vitorenesduarte

I tried this code:

use std::fmt::Debug;

trait Trait {
    type A: Debug;
}

struct S1 {}

impl Trait for S1 {
    type A = u64;
}

#[derive(Debug)]
struct S2<T: Trait> {
    a: T::A,
}

fn main() {
    let s = S2::<S1> { a: 10 };
    format!("{:?}", s);
}

When trying to compile it, I get:

error[E0277]: `S1` doesn't implement `std::fmt::Debug`
  --> src/main.rs:20:21
   |
20 |     format!("{:?}", s);
   |                     ^ `S1` cannot be formatted using `{:?}`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `S1`
   = note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `S2<S1>`
   = note: required by `std::fmt::Debug::fmt

The[derive(Debug)] alone seems to work since the program compiles without the format!.

Meta

rustc --version --verbose:

rustc 1.43.1 (8d69840ab 2020-05-04)
binary: rustc
commit-hash: 8d69840ab92ea7f4d323420088dd8c9775f180cd
commit-date: 2020-05-04
host: x86_64-apple-darwin
release: 1.43.1
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions