Skip to content

Rustc can't infer GAT type when used as argument to Fn closure outside of trait #88459

Closed
@geom3trik

Description

@geom3trik

I tried this code:

playgorund

#![feature(generic_associated_types)]

trait Trait {
    type Assoc<'a>;
}

fn f<T: Trait>(_: T, _: impl Fn(T::Assoc<'_>)) {}

struct Type;

impl Trait for Type {
    type Assoc<'a> = ();
}

fn main() {
    f(Type, |_|());
}

I expected to see this happen: compiles with no errors.

Instead, this happened:

error[E0631]: type mismatch in closure arguments
  --> src/main.rs:16:5
   |
16 |     f(Type, |_|());
   |     ^       ----- found signature of `fn(()) -> _`
   |     |
   |     expected signature of `for<'r> fn(<Type as Trait>::Assoc<'r>) -> _`
   |
note: required by a bound in `f`
  --> src/main.rs:7:30
   |
7  | fn f<T: Trait>(_: T, _: impl Fn(T::Assoc<'_>)) {}
   |                              ^^^^^^^^^^^^^^^^ required by this bound in `f`

The error seems to suggest that rustc can't figure out that the associated type is () which should satisfy the bound in f.

Meta

1.56.0-nightly ( 5eacec9 )

This could be related to #85921, but unlike that issue this one does not seem to be fixed on latest nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)C-bugCategory: This is a bug.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsGATs-triagedIssues using the `generic_associated_types` feature that have been triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions