Suppress E0621 perpetual borrow suggestion#156892
Open
Dnreikronos wants to merge 2 commits into
Open
Conversation
Collaborator
|
r? @mejrs rustbot has assigned @mejrs. Use Why was this reviewer chosen?The reviewer was selected based on:
|
394d2f0 to
0a0663f
Compare
This comment has been minimized.
This comment has been minimized.
mejrs
requested changes
May 24, 2026
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
0a0663f to
1fd9652
Compare
This comment has been minimized.
This comment has been minimized.
When E0621 suggests adding a named lifetime to a reference type, detect cases where the resulting type would create a perpetual borrow (the named lifetime appears both on the outer reference and inside the referent type). Instead of the misleading suggestion, emit a correct multipart suggestion that introduces a fresh lifetime parameter and applies it only to the outer reference.
1fd9652 to
654411c
Compare
Contributor
Author
|
Rebased to drop that commit. The PR now only contains the perpetual borrow fix on top of main. |
This comment has been minimized.
This comment has been minimized.
…d types Check the original (pre-fold) parameter type instead of the post-fold type when detecting perpetual borrows, and restrict the check to mutable references only. The previous check on the post-fold type produced false positives for trait object lifetime bounds (`&dyn Foo` → `&'a (dyn Foo + 'a)`) and types where the outer reference already carried the named lifetime (`&'a mut Lexer`). Shared references like `&'a S<'a>` are intentional and should not trigger the alternate suggestion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #156682
E0621 currently suggests changing
&mut Buffer<'a>to&'a mut Buffer<'a>. This creates a perpetual borrow, where the value borrows itself for its entire lifetime and becomes unusable after the call. The suggestion is almost never what the user wants.What changed
When the suggested type would produce
&'named [mut] Twhere'namedalso appears insideT, this PR suppresses the misleading suggestion and instead emits a correct multipart suggestion that introduces a fresh lifetime parameter (e.g.'b) and applies it only to the outer reference.How the detection works
After computing
new_ty(the type with the anonymous region replaced by the named one), we check ifnew_tyisty::Ref(outer_region, inner_ty, _)whereouter_region == namedandtcx.any_free_region_meets(inner_ty, |r| r == named).Before
After