Skip to content

#[derive(Debug)] doesn't work on newtype over unsized type #25394

Closed
@japaric

Description

@japaric

STR

#[derive(Debug)]
struct Row<T>([T]);
//~ error: the trait `core::marker::Sized` is not implemented for the type `[T]`

fn main() {}

Expansion:

struct Row<T>([T]);

impl <T: ::std::fmt::Debug> ::std::fmt::Debug for Row<T> where
 T: ::std::fmt::Debug {
    fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
            Row(ref __self_0_0) =>
            __arg_0.debug_tuple("Row").field(&(*__self_0_0)).finish(),
            //~^ error: the trait `core::marker::Sized` is not implemented for the type `[T]`
        }
    }
}

fn main() {}

This is because the DebugTuple::field method expects a &Debug trait object, and fat pointers (like &[T]) can't be coerced to trait objects. This would work if the signature of the DebugTuple::field was changed to:

impl DebugTuple {
    fn field<T: ?Sized + Debug>(self, value: &T) -> DebugTuple { .. }
}

As a workaround you can impl Debug manually as write!(f, "Row({:?})", &self.0)

Version

rustc 1.1.0-nightly (c2b30b86d 2015-05-12) (built 2015-05-13)

cc @alexcrichton
cc @sfackler

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions