Skip to content

Rust cannot derive traits from associated types #34377

Closed

Description

#[derive(Copy, Clone)]
pub struct Bar(<Bar as Foo>::Type);

#[derive(Copy, Clone)]
pub struct Baz(i8);

pub trait Foo {
    type Type;
}

impl Foo for Bar {
    type Type = i8;
}

fn main() {
    println!("Ha, I don't compile!");
}
<anon>:1:10: 1:14 error: the trait `Copy` may not be implemented for this type; field `<unnamed_field>` does not implement `Copy` [E0204]
<anon>:1 #[derive(Copy, Clone)]

Baz is fine, Bar fails.

Activity

  1. Boddlnagg commented on Jun 20, 2016

    @Boddlnagg
    Contributor

    This is probably the same as #31518

  2. durka commented on Jun 21, 2016

    @durka
    Contributor

    It's kinda like #31518, but it's more that rust doesn't substitute the associated type before doing the Copy check. You can trigger it without #[derive]:

    pub trait Foo {
        type Type;
    }
    
    pub struct Bar(<Bar as Foo>::Type);
    
    impl Foo for Bar {
        type Type = i8;
    }
    
    impl Copy for Bar {} //~ERROR the trait `Copy` may not be implemented
  3. Aatch commented on Jun 21, 2016

    @Aatch
    Contributor

    It's not probably not normalizing associated types for this check. As far as I know, it's the only trait that has a check like that, so it's not that surprising that it's fallen through the cracks.

  4. Aatch commented on Jun 21, 2016

    @Aatch
    Contributor

    Yeah, looking at the code, it doesn't look like it tries to resolve the associated type when checking that the impl of Copy is valid. I don't know the code well enough to know where or how it should be fixed though.

    cc @rust-lang/compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions