Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive(Default) fails on struct using associated type of param. #28553

Closed
jpetkau opened this issue Sep 20, 2015 · 2 comments
Closed

derive(Default) fails on struct using associated type of param. #28553

jpetkau opened this issue Sep 20, 2015 · 2 comments

Comments

@jpetkau
Copy link

jpetkau commented Sep 20, 2015

#[derive(Default)] fails on objects that contain a field of an associated type, even if that field is Default.

For example (also https://gist.github.com/4c85bcab2cf9f8c56dc2):

trait TR {
  type Assoc : Clone + Copy + Default;
}

#[derive(Clone, Copy, Default)]
struct Foo<T: TR> {
  thing : T::Assoc,
}

impl <T: TR> Foo<T> {
    fn bar() {
        // This works
        let _ : T::Assoc = Default::default();
        // This does not.
        let _ : Foo<T> = Default::default();
    }
}

This fails with "error: the trait core::default::Default is not implemented for the type T [E0277]". Except that we don't want a default T, we want a default Node.

Manually implementing Default works as a workaround:

impl<T: TR> Default for Foo<T> {
    fn default() -> Self { Foo { thing : Default::default() } }
}

Happens in 1.3, beta, and nightly as of Sep. 20.

@eefriedman
Copy link
Contributor

See also #26925.

@jpetkau
Copy link
Author

jpetkau commented Sep 20, 2015

duplicate of #26925.

Thanks, I figured there must be a dup but my search-fu failed.

@jpetkau jpetkau closed this as completed Sep 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants