Repository navigation
borrowck: Explain implicit object lifetimes in static calls - #164935
Dnreikronos wants to merge 2 commits into
Conversation
|
r? @folkertdev rustbot has assigned @folkertdev. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
25924d9 to
13aef70
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
08a8141 to
9b983cc
Compare
|
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. |
|
r? types |
| if !outlived_region.is_static() { | ||
| return; | ||
| } | ||
| let ConstraintCategory::CallArgument(Some(fn_ty)) = errci.category else { return }; |
There was a problem hiding this comment.
why do this let here instead of in the caller?
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
please add a comment what this function does on a high level
There was a problem hiding this comment.
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.
|
please otherwise squash the commits |
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.
9b983cc to
5ac16e5
Compare
|
sup lcnr o/
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
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
I squashed the diagnostic work into one commit after the baseline tests, so the PR is down to two commits. @rustbot ready |
Fixes #162882
With
&dyn Error, the object lifetime defaults to the borrow's lifetime. Callingisneeds a'staticobject, but E0521 points at the borrow and makes it look like the reference has to be'statictoo. Adding+ 'staticto 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
'staticor those trait lifetimes don't match. If the edit would make an elided return lifetime ambiguous, the diagnostic gives a note instead.