Skip to content

Suppress E0621 perpetual borrow suggestion#156892

Open
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:fix/e0621-perpetual-borrow-suggestion
Open

Suppress E0621 perpetual borrow suggestion#156892
Dnreikronos wants to merge 2 commits into
rust-lang:mainfrom
Dnreikronos:fix/e0621-perpetual-borrow-suggestion

Conversation

@Dnreikronos
Copy link
Copy Markdown
Contributor

@Dnreikronos Dnreikronos commented May 24, 2026

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] T where 'named also appears inside T, 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 if new_ty is ty::Ref(outer_region, inner_ty, _) where outer_region == named and tcx.any_free_region_meets(inner_ty, |r| r == named).

Before

help: add explicit lifetime `'a` to the type of `buffer`
  |
5 | pub fn foo<'a>(buffer: &'a mut Buffer<'a>) {
  |                         ++

After

help: consider introducing a new lifetime `'b` to distinguish the borrow duration from `'a`
  |
5 | pub fn foo<'b, 'a>(buffer: &'b mut Buffer<'a>) {
  |            +++              ++

@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 May 24, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 24, 2026

r? @mejrs

rustbot has assigned @mejrs.
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 73 candidates
  • Random selection from 19 candidates

@Dnreikronos Dnreikronos force-pushed the fix/e0621-perpetual-borrow-suggestion branch from 394d2f0 to 0a0663f Compare May 24, 2026 19:22
@rust-log-analyzer

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Please drop d061dcb from this pr, you already have another pr open for that and this pr does not build upon it.

View changes since this review

Comment thread tests/ui/lifetimes/lifetime-errors/perpetual-borrow-suggestion.stderr Outdated
@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 May 24, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 24, 2026

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

@Dnreikronos Dnreikronos force-pushed the fix/e0621-perpetual-borrow-suggestion branch from 0a0663f to 1fd9652 Compare May 24, 2026 20:04
@rust-log-analyzer

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.
@Dnreikronos Dnreikronos force-pushed the fix/e0621-perpetual-borrow-suggestion branch from 1fd9652 to 654411c Compare May 24, 2026 21:53
@Dnreikronos
Copy link
Copy Markdown
Contributor Author

Rebased to drop that commit. The PR now only contains the perpetual borrow fix on top of main.

@Dnreikronos Dnreikronos requested a review from mejrs May 24, 2026 21:55
@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 May 24, 2026
@rust-log-analyzer

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

E0621 "explicit lifetime required" recommending a perpetual borrow &'a mut Foo<'a>

4 participants