Skip to content

Can't pass GAT as FnOnce argument (interaction between GATs and HRTBs?) #85921

Closed
@elidupree

Description

@elidupree

This code [playground]

#![feature(generic_associated_types)]

trait Trait {
  type Assoc<'a>;
  
  fn with_assoc(f: impl FnOnce(Self::Assoc<'_>));
}

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

  fn with_assoc(f: impl FnOnce(Self::Assoc<'_>)) {
    f(5i32)
  }
}

produces these errors:

error[E0308]: mismatched types
  --> src/lib.rs:13:7
   |
10 |   type Assoc<'a> = i32;
   |   --------------------- expected this associated type
...
13 |     f(5i32)
   |       ^^^^ expected associated type, found `i32`
   |
   = note: expected associated type `<() as Trait>::Assoc<'_>`
                         found type `i32`

error[E0277]: expected a `FnOnce<(i32,)>` closure, found `impl FnOnce(Self::Assoc<'_>)`
  --> src/lib.rs:13:5
   |
13 |     f(5i32)
   |     ^^^^^^^ expected an `FnOnce<(i32,)>` closure, found `impl FnOnce(Self::Assoc<'_>)`
   |
help: consider further restricting this bound
   |
12 |   fn with_assoc(f: impl FnOnce(Self::Assoc<'_>) + std::ops::FnOnce<(i32,)>) {
   |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^

in 1.54.0-nightly (2021-05-27 1c6868a)

Note that you get one of these errors even if you allow Rust to infer the type by producing a value from thin air, implying that the compiler first infers that Assoc<'_> is i32, then complains that Assoc<'_> and i32 are different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-GATsArea: Generic associated types (GATs)A-associated-itemsArea: Associated items (types, constants & functions)A-closuresArea: Closures (`|…| { … }`)C-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.F-generic_associated_types`#![feature(generic_associated_types)]` a.k.a. GATsGATs-triagedIssues using the `generic_associated_types` feature that have been triagedT-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