Skip to content

Smarter derive(Default) for PhantomData #69037

@ark0f

Description

@ark0f
use std::marker::PhantomData;

struct TypeWithoutDefault;

#[derive(Default)]
struct NewType<T>(PhantomData<T>);

fn main() {
    let _ = NewType::<TypeWithoutDefault>::default();
    // ^ E0599
    // no function or associated item named `default` found 
    // for type `NewType<TypeWithoutDefault>` in the current scope
}

But if we implement Default without derive:

impl<T> Default for NewType<T> {
    fn default() -> Self {
        Self(PhantomData)
    }
}

Then code compiles successfully.

Can derive(Default) be smarter for cases like this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions