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 return impl trait inside a type when using HRTBs #67262

Open
samsartor opened this issue Dec 12, 2019 · 4 comments
Open

Can't return impl trait inside a type when using HRTBs #67262

samsartor opened this issue Dec 12, 2019 · 4 comments
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@samsartor
Copy link

samsartor commented Dec 12, 2019

This code fails to compile:

struct Wrapper<T>(T);

fn allowed() -> impl for<'a> Fn(&'a u32) -> &'a u32 {
    |x: &u32| x
}

fn not_allowed() -> Wrapper<impl for<'a> Fn(&'a u32) -> &'a u32> {
    Wrapper(|x: &u32| x)
}

The message is:

error[E0271]: type mismatch resolving `for<'a> <[closure@src/lib.rs:8:13: 8:24] as std::ops::FnOnce<(&'a u32,)>>::Output == &'a u32`
 --> src/lib.rs:7:29
  |
7 | fn not_allowed() -> Wrapper<impl for<'a> Fn(&'a u32) -> &'a u32> {
  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected bound lifetime parameter 'a, found concrete lifetime
  |
  = note: the return type of a function must have a statically known size

I think this may be related to #54729 but it isn't quite clear. Certainly the workaround mentioned there does not work in this case.

@Centril Centril added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-typesystem Area: The type system labels Dec 12, 2019
@Centril
Copy link
Contributor

Centril commented Dec 12, 2019

cc @nikomatsakis @matthewjasper @lqd

@Ekleog
Copy link

Ekleog commented May 3, 2020

Seeing as it's been a while since the last activity on this and I just hit it, I'm just allowing myself to say this is still an issue :)

Here is a playground link if it can help reproducing with the latest nightly for future people who might come in here without having hit the issue themselves.

@Dylan-DPC
Copy link
Member

Current error:

error: lifetime may not live long enough
 --> src/lib.rs:8:23
  |
8 |     Wrapper(|x: &u32| x)
  |                 -   - ^ returning this value requires that `'1` must outlive `'2`
  |                 |   |
  |                 |   return type of closure is &'2 u32
  |                 let's call the lifetime of this reference `'1`

error: higher-ranked lifetime error
 --> src/lib.rs:8:5
  |
8 |     Wrapper(|x: &u32| x)
  |     ^^^^^^^^^^^^^^^^^^^^

@fmease
Copy link
Member

fmease commented Sep 24, 2024

"Fixed"1 by feature F-closure_lifetime_binder `#![feature(closure_lifetime_binder)]` :

#![feature(closure_lifetime_binder)]

struct Wrapper<T>(T);

fn allowed() -> impl for<'a> Fn(&'a u32) -> &'a u32 {
    |x: &u32| x
}

fn not_allowed() -> Wrapper<impl for<'a> Fn(&'a u32) -> &'a u32> {
    Wrapper(for<'a> |x: &'a u32| -> &'a u32 { x })
}

Footnotes

  1. Or rather, "workaround". Though we will likely never infer a higher-ranked lifetime for the closure in this case (see closure_lifetime_binder's RFC / tracking issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants