Skip to content

Don't target synthetic APIT params in lifetime suggestion - #159017

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ozankenangungor:issue-158954-invalid-lifetime-suggestion
Aug 8, 2026
Merged

Don't target synthetic APIT params in lifetime suggestion#159017
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
ozankenangungor:issue-158954-invalid-lifetime-suggestion

Conversation

@ozankenangungor

@ozankenangungor ozankenangungor commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #158954

This fixes an invalid lifetime suggestion involving argument-position impl Trait.

rustc could insert the new lifetime before the impl Trait argument, producing invalid code like 'a, impl Clone. Now it introduces the lifetime on the function generics instead.

This only changes the diagnostic suggestion.

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

rustbot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
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 75 candidates
  • Random selection from 20 candidates

@Enselic Enselic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have done a fairly detailed review of this fix, and as far as I can tell, this is a legitimate fix. Since the fix looks good to me, I will r+ it, but I will wait a day or two to allow others to raise objections. Since this is just a diagnostics change, it's relatively low-risk though.

r? me

The old logic tried to find "the first generic parameter with an explicit name", and assumed that would be the right place to insert 'a, at. However, as pointed out in the issue, APIT is lowered in such a way that impl Trait is considered a generic parameter with an explicit name, but its Span is not inside <...>, but its original place. So 'a, ends up inserted in the wrong spot.

The fix instead uses a helper that essentially instead checks "is there any <...> at all?", by checking if the span of the first generic arg is inside the Generics Span.

This helper is used in the same way in two more places. So adding one more such place seems very uncontroversial. Here is one such other place:

let new_param_sugg =
if let Some(span) = generics.span_for_lifetime_suggestion() {
(span, "'a, ".to_owned())
} else {
(generics.span, "<'a>".to_owned())
};

I'd also like to make a couple of bonus points:

  • It looks like the other uses of the same helper can be de-duplicated, but that's optional and can be done in a follow-up PR, if the diff of such a refactoring ends up nice in the first place.
  • When adding a new test to test a fix, it's good to add the test in a separate commit so that it becomes blessed without the fix, and then make a second commit that introduces the fix and re-blesses the output. That way it becomes super clear what impact the fix has on the diagnostics.
  • The test uses // instead of /// for docs, but some other tests do the same, and if we really care we should add a tidy check for it and fix other tests.

View changes since this review

@rustbot rustbot assigned Enselic and unassigned wesleywiser Aug 7, 2026
@Enselic

Enselic commented Aug 8, 2026

Copy link
Copy Markdown
Member

@bors r+ rollup

(See my previous comment.)

@rust-bors

rust-bors Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 3d96096 has been approved by Enselic

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 8, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 8, 2026
Rollup of 5 pull requests

Successful merges:

 - #160634 (miri subtree update)
 - #159017 (Don't target synthetic APIT params in lifetime suggestion)
 - #160303 (rustc_parse: A few cleanups to expression parsing next to attributes)
 - #160429 (tidy: Update Python version requirements to 3.10)
 - #160543 (Default `RawOsError` to `i16` for 16-bit targets)
@rust-bors
rust-bors Bot merged commit c2983cc into rust-lang:main Aug 8, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Aug 8, 2026
rust-timer added a commit that referenced this pull request Aug 8, 2026
Rollup merge of #159017 - ozankenangungor:issue-158954-invalid-lifetime-suggestion, r=Enselic

Don't target synthetic APIT params in lifetime suggestion

Fixes #158954

This fixes an invalid lifetime suggestion involving argument-position `impl Trait`.

rustc could insert the new lifetime before the `impl Trait` argument, producing invalid code like `'a, impl Clone`. Now it introduces the lifetime on the function generics instead.

This only changes the diagnostic suggestion.
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Aug 9, 2026
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#160634 (miri subtree update)
 - rust-lang/rust#159017 (Don't target synthetic APIT params in lifetime suggestion)
 - rust-lang/rust#160303 (rustc_parse: A few cleanups to expression parsing next to attributes)
 - rust-lang/rust#160429 (tidy: Update Python version requirements to 3.10)
 - rust-lang/rust#160543 (Default `RawOsError` to `i16` for 16-bit targets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

"Lifetime may not live enough"'s suggestion adds a lifetime (incorrectly) to an unrelated impl Trait argument

5 participants