Skip to content

Trait bounds for generic types do not imply themselves when they restrict associated types #109325

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

Open
Yttiricon opened this issue Mar 18, 2023 · 3 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@Yttiricon
Copy link

Yttiricon commented Mar 18, 2023

When restricting a generic type parameter to a trait which is a super trait of another trait, and also places trait bounds on an associated type of the other trait.

For example:

trait TypeRestriction {}

trait SubTrait {
    type AssociatedType;
}

trait SuperTrait: SubTrait where Self::AssociatedType: TypeRestriction {}

fn function<T: SuperTrait>(_arg: T) {}

gives the error:

error[E0277]: the trait bound `<T as SubTrait>::AssociatedType: TypeRestriction` is not satisfied
 --> file.rs:9:16
  |
9 | fn function<T: SuperTrait>(_arg: T) {}
  |                ^^^^^^^^^^ the trait `TypeRestriction` is not implemented for `<T as SubTrait>::AssociatedType`
  |
note: required by a bound in `SuperTrait`
 --> file.rs:7:56
  |
7 | trait SuperTrait: SubTrait where Self::AssociatedType: TypeRestriction {}
  |                                                        ^^^^^^^^^^^^^^^ required by this bound in `SuperTrait`
help: consider further restricting the associated type
  |
9 | fn function<T: SuperTrait>(_arg: T) where <T as SubTrait>::AssociatedType: TypeRestriction {}
  |                                     ++++++++++++++++++++++++++++++++++++++++++++++++++++++

However, T must be able to implement Supertrait, since we asserted that it does with the trait bound T: SuperTrait.

In general, I think checking the trait bounds for trait requirements for generics (in a where clause, in <T: Trait> or (arg: impl Trait) is unnecessary, since if the trait is implemented then it shouldn't matter how, it inherently must satisfy the trait bounds, and if it does not implement the trait, then it cannot be used as that generic type, so nothing breaks.

In the example above, the function doesn't do anything at all, so any arbitrary restrictions on T should be valid, since they have no effect on the function.

@compiler-errors
Copy link
Member

Unfortunately where clauses are not, in general, implied like this. Only bounds that concern the trait's Self type are implied.

@Yttiricon
Copy link
Author

In general I understand that this makes sense when applying restrictions to the associated type or in relation to other traits, but I don't understand how a trait does not imply itself (T:SuperTrait does not imply T:SuperTrait).

@KiruyaMomochi
Copy link

Is this issue the same thing as #103387?

@jyn514 jyn514 added A-trait-system Area: Trait system A-associated-items Area: Associated items (types, constants & functions) T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 26, 2023
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) A-trait-system Area: Trait system T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants