Skip to content

Associated type bound isn't always present for checking #43475

Closed
@Twey

Description

@Twey

Trait bounds on associated types seem to be required, not implied, when the trait in question mentions an associated type on a type parameter.

Concretely, this code (or playground) produces an error:

trait Foo { type FooT: Foo; }
impl Foo for () { type FooT = (); }
trait Bar<T: Foo> { type BarT: Bar<T::FooT>; }
impl Bar<()> for () { type BarT = (); }

fn test<C: Bar<()>>() { }
error[E0277]: the trait bound `<C as Bar<()>>::BarT: Bar<()>` is not satisfied
 --> src/main.rs:6:1
  |
6 | fn test<C: Bar<()>>() { }
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar<()>` is not implemented for `<C as Bar<()>>::BarT`
  |
  = help: consider adding a `where <C as Bar<()>>::BarT: Bar<()>` bound
  = note: required by `Bar`

despite the C::BarT: Bar<()> bound being required by the definition of Bar itself, and in fact this means that the trait Bar<T> can never be mentioned.

This similar example (playground) that doesn't use an associated type in the trait parameter compiles:

trait Foo { type FooT: Foo; }
impl Foo for () { type FooT = (); }
trait Bar<T: Foo> { type BarT: Bar<T>; }
impl Bar<()> for () { type BarT = (); }

fn test<C: Bar<()>>() { }

while this version (playground), which uses a third trait to avoid the self-reference, does not:

trait Foo { type FooT: Foo; }
impl Foo for () { type FooT = (); }
trait Bar<T: Foo> { type BarT: Baz<T::FooT>; }
impl Bar<()> for () { type BarT = (); }
trait Baz<T: Foo> { }
impl Baz<()> for () { }

fn test<C: Bar<()>>() { }

Is this just a missing normalization step?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.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