-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.