rust-lang / rust-lang/rust

Diagnostic recommends `+ '_` instead of `+ use<'_>` or nothing to express capturing

Open
#144,043 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
use std::fmt::Debug;

fn f(x: &i32) -> (Box<dyn Debug>, impl Debug) {
    (Box::new(x), x)
}
Current output
error: lifetime may not live long enough
 --> src/lib.rs:4:5
  |
3 | fn f(x: &i32) -> (Box<dyn Debug>, impl Debug) {
  |         - let's call the lifetime of this reference `'1`
4 |     (Box::new(x), x)
  |     ^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
  |
help: to declare that the trait object captures data from argument `x`, you can add an explicit `'_` lifetime bound
  |
3 | fn f(x: &i32) -> (Box<dyn Debug + '_>, impl Debug) {
  |                                 ++++
help: to declare that `impl Debug` captures data from argument `x`, you can add an explicit `'_` lifetime bound
  |
3 | fn f(x: &i32) -> (Box<dyn Debug>, impl Debug + '_) {
  |                                              ++++
Desired output

Rationale and extra context

Legacy from #72543 when we didn't have precise capturing and a good understanding of outlives vs captures relationships.

We should probably replace the advice for the RPIT with impl Debug + use<'_>. On edition 2024, the RPIT hint should be removed altogether -- there doesn't seem to be any check against recommending implicit captures currently.

I have no idea about the right solution for dyn. We could maybe leave it as-is, but

fn f<'a, 'b>(x: &'a i32, y: &'b i32) -> Box<dyn Debug> {
    if true {
        Box::new(x)
    } else {
        Box::new(y)
    }
}

currently recommends to add both + 'a and + 'b, which obviously doesn't work. Does anyone know if there's ongoing work on adding use to dyn?

Other cases

Rust Version
rustc 1.90.0-nightly (a00149764 2025-07-14)
binary: rustc
commit-hash: a001497644bc229f1abcc5b2528733386591647f
commit-date: 2025-07-14
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8
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 by reproducing the diagnostic with the issue's Rust code on the noted nightly version, then trace the compiler diagnostic that suggests lifetime bounds for RPIT and dyn trait objects. Done should include appropriate use<'_> advice for RPIT, no inappropriate RPIT hint on edition 2024, and a decided behavior for dyn captures, covered by regression tests.

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.