2

Given the following code (Rust Playground):

use std::fmt::Debug;
use std::marker::PhantomData;

trait Herp: Debug {}

#[derive(Debug)]
struct Derp<S: ?Sized> {
    phantom: PhantomData<S>
}

impl<S: ?Sized> Herp for Derp<S> {}

The following error occurs:

error[E0277]: the trait bound `S: std::fmt::Debug` is not satisfied
  --> src/...
   |
37 | impl<S: ?Sized> Herp for Derp<S> {}
   |                 ^^^^
   |
   = help: consider adding a `where S: std::fmt::Debug` bound
   = note: required because of the requirements on the impl of `std::fmt::Debug` for `Derp<S>`
   = note: required by `Herp

I find this behaviour inappropriate because Debug is derived properly for Derp and PhantomData<T> should implement Debug regardless of type T (docs). I also couldn't find anything else on my search that could apparently induce this error.

Why can't I implement a trait that requires Debug for a type containing a phantom?

E_net4
  • 27,810
  • 13
  • 101
  • 139

0 Answers0