Skip to content

borrowck: Explain implicit object lifetimes in static calls - #164935

Open
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:codex/borrowck_explicit_object_lifetime
Open

Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:codex/borrowck_explicit_object_lifetime

Conversation

@Dnreikronos

@Dnreikronos Dnreikronos commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #162882

With &dyn Error, the object lifetime defaults to the borrow's lifetime. Calling is needs a 'static object, but E0521 points at the borrow and makes it look like the reference has to be 'static too. Adding + 'static to the object is enough for the example in the issue.

This suggests &(dyn Error + 'static) for the parameter and explains the implicit lifetime in the same help message, as requested in the issue. I think keeping the reason next to the edit makes the fix much easier to understand. When the trait's bounds supply the lifetime, the message explains that too.

I kept the suggestion to arguments passed directly from parameters. The checks also look at the callee's bounds and the lifetimes in trait arguments, to avoid suggesting an object bound when the borrow itself needs to be 'static or those trait lifetimes don't match. If the edit would make an elided return lifetime ambiguous, the diagnostic gives a note instead.

@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 25, 2026
@rustbot

rustbot commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

r? @folkertdev

rustbot has assigned @folkertdev.
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
  • compiler expanded to 77 candidates
  • Random selection from 20 candidates

@rust-log-analyzer

This comment has been minimized.

@Dnreikronos
Dnreikronos force-pushed the codex/borrowck_explicit_object_lifetime branch from 25924d9 to 13aef70 Compare October 26, 2026 11:38
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@Dnreikronos
Dnreikronos force-pushed the codex/borrowck_explicit_object_lifetime branch from 08a8141 to 9b983cc Compare October 27, 2026 12:49
@rustbot

rustbot commented Oct 27, 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.

@folkertdev

Copy link
Copy Markdown
Contributor

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Oct 30, 2026
@rustbot rustbot assigned lcnr and unassigned folkertdev Oct 30, 2026
if !outlived_region.is_static() {
return;
}
let ConstraintCategory::CallArgument(Some(fn_ty)) = errci.category else { return };

@lcnr lcnr Nov 24, 2026 •

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.

why do this let here instead of in the caller?

View changes since the review

@Dnreikronos Dnreikronos Nov 24, 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.

why do this let here instead of in the caller?

I moved it into the caller. It already pulls out the callee type for the 'static note, so passing that same type to the helper makes the flow easier to follow.

}
}

fn suggest_explicit_static_object_lifetime(

@lcnr lcnr Nov 24, 2026 •

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.

please add a comment what this function does on a high level

View changes since the review

@Dnreikronos Dnreikronos Nov 24, 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.

please add a comment what this function does on a high level

I added a comment using the distinction that got me looking at this in the first place. In &dyn Error, the object picks up the reference's lifetime, so the error can point at the borrow even when adding + 'static to the object would fix it. If that edit could make the return lifetime ambiguous, the diagnostic just explains where the object lifetime came from.

@lcnr lcnr 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.

can you add the blessed test before the change and then only have the diff to the test output with the change. That makes the change itself easier to review

View changes since this review

@lcnr

lcnr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

please otherwise squash the commits

@lcnr lcnr 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.

I do feel like the diagnostics code is somewhat odd and likely has false positives. I haven't read it fully, but it would be nice to get some high-level description of what this is detecting/filtering for

View changes since this review

@lcnr lcnr 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 Nov 24, 2026
An implicit trait object bound can share the reference's lifetime.
When a call requires a static object, suggest an explicit bound on
the parameter where the signature and callee bounds support the edit.
@Dnreikronos
Dnreikronos force-pushed the codex/borrowck_explicit_object_lifetime branch from 9b983cc to 5ac16e5 Compare November 24, 2026 13:38
@Dnreikronos

Dnreikronos commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

sup lcnr o/

I do feel like the diagnostics code is somewhat odd and likely has false positives. I haven't read it fully, but it would be nice to get some high-level description of what this is detecting/filtering for

MIR gives us the call span and the callee type, so the helper looks for candidates in the HIR arguments. It sticks to arguments passed straight from a parameter, checks that the trait arguments and associated types line up, and skips cases where the callee may also constrain the borrow itself. I think the suggestion is helpful for the cases this catches. I kept it as MaybeIncorrect because matching a candidate to the error is still a guess.

can you add the blessed test before the change and then only have the diff to the test output with the change. That makes the change itself easier to review

I put the UI tests and their original stderr in the first commit. The diagnostic change and the new output are in the second. That makes it easier to see what changed in the error. The second commit also turns on run-rustfix so the test checks that the suggested edit compiles. The baseline tests and the targeted UI tests both passed.

please otherwise squash the commits

I squashed the diagnostic work into one commit after the baseline tests, so the PR is down to two commits.

@rustbot ready

@rustbot rustbot 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 Nov 24, 2026

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. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad error message for missing static bounds on &dyn Trait

5 participants