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

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

Closed
WaffleLapkin opened this issue Feb 10, 2023 · 3 comments · Fixed by #136971
Closed
Assignees
Labels
A-closures Area: Closures (`|…| { … }`) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-type-system Area: Type system 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-impl_trait_in_fn_trait_return `#![feature(impl_trait_in_fn_trait_return)]` T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@WaffleLapkin
Copy link
Member

WaffleLapkin commented Feb 10, 2023

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)
@WaffleLapkin WaffleLapkin added A-closures Area: Closures (`|…| { … }`) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. C-bug Category: This is a bug. F-impl_trait_in_fn_trait_return `#![feature(impl_trait_in_fn_trait_return)]` labels Feb 10, 2023
@veera-sivarajan
Copy link
Contributor

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

@WaffleLapkin WaffleLapkin added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. A-type-system Area: Type system T-types Relevant to the types team, which will review and decide on the PR/issue. labels Aug 12, 2024
@fmease fmease added A-type-system Area: Type system and removed A-type-system Area: Type system labels Dec 21, 2024
@LFS6502
Copy link
Contributor

LFS6502 commented Jan 9, 2025

@rustbot claim

@LFS6502
Copy link
Contributor

LFS6502 commented Feb 14, 2025

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

For posterity:

The PR that made it compile:
#122077

ab5bda1 errors
b234e44 compiles

jhpratt added a commit to jhpratt/rust that referenced this issue Feb 14, 2025
Add a new check-pass UI test for returning `impl Fn(T) -> impl Trait`

This PR closes rust-lang#107883 by adding a ui test.
@bors bors closed this as completed in 2ec48fb Feb 15, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 15, 2025
Rollup merge of rust-lang#136971 - HypheX:patch1, r=WaffleLapkin

Add a new check-pass UI test for returning `impl Fn(T) -> impl Trait`

This PR closes rust-lang#107883 by adding a ui test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-type-system Area: Type system 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-impl_trait_in_fn_trait_return `#![feature(impl_trait_in_fn_trait_return)]` T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants