Skip to content

Should associated type bounds on supertraits be implied? #112573

Closed
@compiler-errors

Description

@compiler-errors

I tried this code:

#![feature(associated_type_bounds)]

trait Trait: Super<Assoc: Bound> {}

trait Super {
    type Assoc;
}

trait Bound {}

fn foo<T>(x: T) where T: Trait {}

And this happened:

error[E0277]: the trait bound `<T as Super>::Assoc: Bound` is not satisfied
  --> src/lib.rs:13:8
   |
13 |     T: Trait,
   |        ^^^^^ the trait `Bound` is not implemented for `<T as Super>::Assoc`
   |
note: required by a bound in `Trait`
  --> src/lib.rs:3:27
   |
3  | trait Trait: Super<Assoc: Bound> {}
   |                           ^^^^^ required by this bound in `Trait`
help: consider further restricting the associated type
   |
13 |     T: Trait, <T as Super>::Assoc: Bound
   |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For more information about this error, try `rustc --explain E0277`.

I'm not exactly sure whether this should be implied or not! Nominating this for T-lang for discussion, I'd like to know whether this is expected behavior or not.

Argument for the code failing

For non trait aliases, we don't imply currently any where clauses for non-supertrait bounds. In this case, supertrait bounds are precisely trait and associated type bounds which have Self as their self type. Since we can interpret Self: Super<Assoc: Bound> as two bounds -- Self: Super and <Self as Super>::Assoc: Bound -- it's consistent why the latter is not implied.

Fixing this complicates the code even more.

Argument for the code compiling

The concept of "supertraits" from a language perspective (and not a types perspective) is not very well specified -- one may argue again that it could mean anything that comes after the colon in trait Trait: ... or Self: ..., for example. Whether or not that's more intuitive, 🤷

It's also just annoying that this doesn't compile, and who knows if associated type bounds (which I'm personally motivated to compile) becomes more frustrating and/or less useful without these bounds being implied.


Related to #112568, which is essentially the same question but for return type notation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-implied-boundsArea: Implied bounds / inferred outlives-boundsC-bugCategory: This is a bug.F-associated_type_bounds`#![feature(associated_type_bounds)]`T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions