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

Rust cannot derive traits from associated types #34377

Closed
ghost opened this issue Jun 20, 2016 · 5 comments
Closed

Rust cannot derive traits from associated types #34377

ghost opened this issue Jun 20, 2016 · 5 comments
Labels
A-associated-items Area: Associated items (types, constants & functions)

Comments

@ghost
Copy link

ghost commented Jun 20, 2016

#[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.

@ghost
Copy link
Author

ghost commented Jun 20, 2016

pub trait Foo {
    type Type: Copy;
}

While this will make it compile, it seems a bit strange the error would occur - it's true in the original the trait Foo doesn't have a Copy bound on Type, but the implementation of Bar resolves to a type that does have Copy implemented - and the error itself is seems to be about the implementation, not the trait.

If this hasty error-throwing behavior is intentional, perhaps the error could be made to refer to the Foo trait? It feels like a bug.

@steveklabnik steveklabnik added the A-associated-items Area: Associated items (types, constants & functions) label Jun 20, 2016
@Boddlnagg
Copy link
Contributor

This is probably the same as #31518

@durka
Copy link
Contributor

durka commented Jun 21, 2016

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

@Aatch
Copy link
Contributor

Aatch commented Jun 21, 2016

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.

@Aatch
Copy link
Contributor

Aatch commented Jun 21, 2016

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

@bors bors closed this as completed in 243e45a Jan 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions)
Projects
None yet
Development

No branches or pull requests

4 participants