Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
elidupree opened this issue Jun 2, 2021 · 4 comments · Fixed by #89975
Closed

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

elidupree opened this issue Jun 2, 2021 · 4 comments · Fixed by #89975
Labels
A-associated-items Area: Associated items (types, constants & functions) A-closures Area: Closures (`|…| { … }`) A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. E-needs-test Call 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. GATs GATs-triaged Issues using the `generic_associated_types` feature that have been triaged T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@elidupree
Copy link

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.

@elidupree elidupree added the C-bug Category: This is a bug. label Jun 2, 2021
@Aaron1011
Copy link
Member

I suspect that this will be fixed by #85499

@jackh726 jackh726 added the F-generic_associated_types `#![feature(generic_associated_types)]` a.k.a. GATs label Jun 4, 2021
@inquisitivecrystal
Copy link
Contributor

inquisitivecrystal commented Jun 5, 2021

@rustbot label +A-associated-items +A-closures +T-compiler

@rustbot rustbot added A-associated-items Area: Associated items (types, constants & functions) A-closures Area: Closures (`|…| { … }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 5, 2021
@jackh726
Copy link
Member

Fixed in latest nightly

@jackh726 jackh726 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Jul 19, 2021
@jackh726
Copy link
Member

GATs issue triage: not blocking. Works now, just needs test.

@jackh726 jackh726 added the GATs-triaged Issues using the `generic_associated_types` feature that have been triaged label Oct 16, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 17, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Oct 17, 2021
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#89738 (ty::pretty: prevent infinite recursion for `extern crate` paths.)
 - rust-lang#89888 (Make `llvm.download-ci-llvm="if-available"` work for tier 2 targets with host tools)
 - rust-lang#89945 (Remove a mention to `copy_from_slice` from `clone_from_slice` doc)
 - rust-lang#89946 (Fix an ICE with TAITs and Future)
 - rust-lang#89963 (Some "parenthesis" and "parentheses" fixes)
 - rust-lang#89975 (Add a regression test for rust-lang#85921)
 - rust-lang#89977 (Make Result::as_mut const)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 1ee7c29 Oct 17, 2021
@fmease fmease added the A-GATs Area: Generic associated types (GATs) label Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-closures Area: Closures (`|…| { … }`) A-GATs Area: Generic associated types (GATs) C-bug Category: This is a bug. E-needs-test Call 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. GATs GATs-triaged Issues using the `generic_associated_types` feature that have been triaged T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants