rust-lang / rust-lang/rust

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

Open
#132,364 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the playground reproduction linked in the issue and compare rustc 1.82.0's diagnostic for the testfn return type with the proposed + use<> diagnostic. Trace the compiler's handling of implicit impl Trait captures and diagnostic emission; done means the reported error clearly identifies the implicitly captured parameter without regressing related cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.