Skip to content

impl Trait in return position implicitly captures non-'static type parameter despite + 'static #132364

Open
@progval

Description

Code

trait MyTrait {}
struct MyStruct;
impl MyTrait for MyStruct {}

trait DeserTrait<'de> {}
struct DeserStruct;
impl<'de> DeserTrait<'de> for &'de DeserStruct {}

pub fn testfn<'de, D: DeserTrait<'de>>(_deserializer: D) -> impl MyTrait + 'static {
    MyStruct
}

fn test() -> impl Send {
    let deserializer = DeserStruct;
    let graph = testfn(&deserializer);
    graph
}

(playground)

Current output

error[E0597]: `deserializer` does not live long enough
  --> src/lib.rs:15:24
   |
14 |     let deserializer = DeserStruct;
   |         ------------ binding `deserializer` declared here
15 |     let graph = testfn(&deserializer);
   |                 -------^^^^^^^^^^^^^-
   |                 |      |
   |                 |      borrowed value does not live long enough
   |                 argument requires that `deserializer` is borrowed for `'static`
16 |     graph
17 | }
   | - `deserializer` dropped here while still borrowed

Desired output

I'm not sure, but adding + use<> to the function's return type explains what the issue is actually that impl Trait implicitly captured the trait bound:

error: `impl Trait` must mention all type parameters in scope in `use<...>`
 --> src/lib.rs:9:61
  |
9 | pub fn testfn<'de, D: DeserTrait<'de>>(_deserializer: D) -> impl MyTrait + use<> + 'static {
  |                    -                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                    |
  |                    type parameter is implicitly captured by this `impl Trait`
  |
  = note: currently, all type parameters are required to be mentioned in the precise captures list

Rationale and extra context

It's impossible to debug (or google) otherwise, and I only figured it out because I remembered + use<> from the stabilization blog post

Other cases

No response

Rust Version

rustc 1.82.0

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler 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