Skip to content

Returning impl Fn(T) -> impl Trait does not compile, unless you add an identity call #107883

Closed

Description

@WaffleLapkin

I tried this code:

#![feature(impl_trait_in_fn_trait_return)]
#![feature(unboxed_closures)] // only for `h`

use std::fmt::Debug;

// Fails??
fn f<T>() -> impl Fn(T) -> impl Debug {
    |_x| 15
}

// Works?...
fn g<T>() -> impl MyFn<(T,), Out = impl Debug> {
    |_x| 15
}

trait MyFn<T> { type Out; }
impl<T, U, F: Fn(T) -> U> MyFn<(T,)> for F { type Out = U; }

// Also fails??
fn h<T>() -> impl Fn<(T,), Output = impl Debug> {
    |_x| 15
}

// Works??????
fn f_<T>() -> impl Fn(T) -> impl Debug {
    std::convert::identity(|_x| 15)
}

// Works?????
fn f__<T>() -> impl Fn(T) -> impl Debug {
    let r = |_x| 15;
    r
}

[play]

I expected to see this happen: all of the above functions successfully compile.

Instead, this happened: f and h do not compile for no clear reason, while g, f_ and f__ do compile, even though they are all almost identical.

Compiler output

error: concrete type differs from previous defining opaque type use
 --> src/lib.rs:8:10
  |
8 |     |_x| 15
  |          ^^ expected `impl Debug`, got `i32`
  |
note: previous use here
 --> src/lib.rs:8:5
  |
8 |     |_x| 15
  |     ^^^^^^^

error[[E0720]](https://doc.rust-lang.org/nightly/error-index.html#E0720): cannot resolve opaque type
 --> src/lib.rs:7:28
  |
7 | fn f<T>() -> impl Fn(T) -> impl Debug {
  |                            ^^^^^^^^^^ cannot resolve opaque type

error: concrete type differs from previous defining opaque type use
  --> src/lib.rs:21:10
   |
21 |     |_x| 15
   |          ^^ expected `impl Debug`, got `i32`
   |
note: previous use here
  --> src/lib.rs:21:5
   |
21 |     |_x| 15
   |     ^^^^^^^

error[[E0720]](https://doc.rust-lang.org/nightly/error-index.html#E0720): cannot resolve opaque type
  --> src/lib.rs:20:37
   |
20 | fn h<T>() -> impl Fn<(T,), Output = impl Debug> {
   |                                     ^^^^^^^^^^ cannot resolve opaque type

Meta

Rustc version:

1.69.0-nightly (2023-02-09 8996ea93b6e554148c42)

Activity

  1. added
    A-closuresArea: Closures (`|…| { … }`)
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    C-bugCategory: This is a bug.
    on Feb 10, 2023
  2. veera-sivarajan commented on Jul 20, 2024

    @veera-sivarajan
    Contributor

    Compiles on current nightly: https://rust.godbolt.org/z/e44PcPa4a

  3. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    on Aug 12, 2024
  4. added and removed on Dec 21, 2024
  5. LFS6502 commented on Jan 9, 2025

    @LFS6502
    Contributor

    @rustbot claim

  6. LFS6502 commented on Feb 14, 2025

    @LFS6502
    Contributor

    Compiles on current nightly: https://rust.godbolt.org/z/e44PcPa4a

    For posterity:

    The PR that made it compile:
    #122077

    ab5bda1 errors
    b234e44 compiles

  7. added a commit that references this issue on Feb 14, 2025
    8356c09
  8. added a commit that references this issue on Feb 14, 2025
    2ec48fb
  9. added a commit that references this issue on Feb 15, 2025
    8c864be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-closuresArea: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-type-systemArea: Type 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.F-impl_trait_in_fn_trait_return`#![feature(impl_trait_in_fn_trait_return)]`T-typesRelevant to the types 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