Skip to content

Associated type equality fails with trait bound on struct #51446

Closed
@stephaneyfx

Description

@stephaneyfx

This may be related/identical to #47897 and #37883.

The following code fails to compile with a dubious error. This is a simplified reproduction example based on @DutchGhost's code on IRC. Note that commenting the where clause in the definition of struct B makes the code compile. playground

trait Foo {
    type Item;
    fn get(&self) -> Self::Item;
}

fn blah<T, F>(x: T, f: F) -> B<T::Item, impl Fn(T::Item)>
where
    T: Foo,
    F: Fn(T::Item),
{
    B {x: x.get(), f}
}

struct B<T, F>
// Comment the 2 following lines to make the compiler happy
where
    F: Fn(T),
{
    x: T,
    f: F,
}

impl Foo for i32 {
    type Item = i32;
    fn get(&self) -> i32 {*self}
}

fn main() {
    let _ = blah(0, |_| ());
}
error[E0277]: the trait bound `impl std::ops::Fn<(<i32 as Foo>::Item,)>: std::ops::Fn<(i32,)>` is not satisfied
  --> src/main.rs:29:13
   |
29 |     let _ = blah(0, |_| ());
   |             ^^^^^^^^^^^^^^^ the trait `std::ops::Fn<(i32,)>` is not implemented for `impl std::ops::Fn<(<i32 as Foo>::Item,)>`
   |
note: required by `B`
  --> src/main.rs:14:1
   |
14 | / struct B<T, F>
15 | | // Comment the 2 following lines to make the compiler happy
16 | | where
17 | |     F: Fn(T),
...  |
20 | |     f: F,
21 | | }
   | |_^

error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler 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