Skip to content

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

@samsartor

Description

@samsartor

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions