Skip to content

Suggest adding lifetime for dyn Trait in return type - #164188

Open
estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-41966
Open

estebank wants to merge 1 commit into
rust-lang:mainfrom
estebank:issue-41966

Conversation

@estebank

@estebank estebank commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor
error[E0310]: the parameter type `R` may not live long enough
  --> $DIR/implicit-static-lifetime-in-dyn-trait-return-type.rs:10:5
   |
LL | fn bb<R>(r: R) -> Box<dyn Foo> {
   |                       ------- this `dyn Trait` has an implicit `'static` lifetime bound
LL |     Box::new(Bar(r))
   |     ^^^^^^^^^^^^^^^^
   |     |
   |     the parameter type `R` must be valid for the static lifetime...
   |     ...so that the type `R` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
LL | fn bb<R: 'static>(r: R) -> Box<dyn Foo> {
   |        +++++++++
help: consider adding an explicit lifetime bound
   |
LL | fn bb<'a, R>(r: R) -> Box<dyn Foo + 'a> {
   |       +++                         ++++

Address some cases from #41966.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 12, 2026
@rustbot

rustbot commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 77 candidates
  • Random selection from 19 candidates

Comment thread compiler/rustc_trait_selection/src/error_reporting/infer/region.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/infer/region.rs Outdated
Comment thread compiler/rustc_trait_selection/src/error_reporting/infer/region.rs
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 12, 2026
@rustbot

rustbot commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@estebank estebank added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 12, 2026
Comment on lines +49 to +56
help: consider adding an explicit lifetime bound
|
LL | fn baz<'a>(self) -> Box<dyn Baz + 'a> {
| ++++ ++++

@estebank estebank Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go on the impl like the 'static suggestion...

View changes since the review

@bushrat011899 bushrat011899 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks good to me, but I'm not a member of the compiler team so I don't feel comfortable leaving an approval here. Thanks for answering the couple questions I had!

View changes since this review

@rust-bors

This comment has been minimized.

```
error[E0310]: the parameter type `R` may not live long enough
  --> $DIR/implicit-static-lifetime-in-dyn-trait-return-type.rs:10:5
   |
LL | fn bb<R>(r: R) -> Box<dyn Foo> {
   |                       ------- this `dyn Trait` has an implicit `'static` lifetime bound 3
LL |     Box::new(Bar(r))
   |     ^^^^^^^^^^^^^^^^
   |     |
   |     the parameter type `R` must be valid for the static lifetime...
   |     ...so that the type `R` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
LL | fn bb<R: 'static>(r: R) -> Box<dyn Foo> {
   |        +++++++++
help: consider adding an explicit lifetime bound
   |
LL | fn bb<'a, R>(r: R) -> Box<dyn Foo + 'a> {
   |       +++                         ++++
```
@rustbot

rustbot commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

This branch has not been deployed

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants