Skip to content

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

Closed
@WaffleLapkin

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)

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