Skip to content

Trait bounds on struct type parameters aren't always enforced (depending on the order of impls) #21837

Closed
@SSheldon

Description

@SSheldon

The following compiles without issue, even though the impl of Trait2 is missing a trait bound that T implements Bound:

pub trait Bound { }
pub struct Foo<T: Bound>;

pub trait Trait1 { }
impl<T: Bound> Trait1 for Foo<T> { }

pub trait Trait2 { }
impl<T> Trait2 for Foo<T> { }

fn main() { }

However, if you switch the order of the impls...

pub trait Bound { }
pub struct Foo<T: Bound>;

pub trait Trait2 { }
impl<T> Trait2 for Foo<T> { }

pub trait Trait1 { }
impl<T: Bound> Trait1 for Foo<T> { }

fn main() { }

You suddenly start getting this error:

error: the trait `Bound` is not implemented for the type `T` [E0277]
impl<T> Trait2 for Foo<T> { }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

I did not expect the ordering of the impls to matter here. I'm using rustc 1.0.0-nightly (1d00c545e 2015-01-30 19:56:34 +0000).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions