Skip to content

Fix trailing comma in lifetime suggestion for empty angle brackets#154703

Open
fru1tworld wants to merge 1 commit intorust-lang:mainfrom
fru1tworld:fix-154600-trailing-comma
Open

Fix trailing comma in lifetime suggestion for empty angle brackets#154703
fru1tworld wants to merge 1 commit intorust-lang:mainfrom
fru1tworld:fix-154600-trailing-comma

Conversation

@fru1tworld
Copy link
Copy Markdown

@fru1tworld fru1tworld commented Apr 2, 2026

Fixes #154600

When suggesting a lifetime parameter (e.g., 'a) for a type like Foo<> (empty angle brackets), the compiler was incorrectly producing Foo<'a, > with a trailing comma.

Root Cause

The has_existing_params check used segment.args to determine if generic parameters exist. However, for empty angle brackets (<>), the parser doesn't create any args, so has_existing_params was false — even though the angle brackets themselves exist.

This caused the suggestion logic to skip the comma+space suffix ("'a, "), but the insert position was still inside the angle brackets, leading to Foo<'a, > instead of Foo<'a>.

Fix

Replace the segment.args-based check with a source text inspection using span_to_snippet. The new logic:

  1. Finds the < character in the source text
  2. Extracts the content between < and >
  3. Checks if that content is non-empty (after trimming whitespace)

This correctly identifies <> as having no existing parameters, avoiding the trailing comma.

Test

Added tests/ui/lifetimes/E0106-trailing-comma-in-lifetime-suggestion.rs covering the specific case of empty angle brackets with lifetime suggestions.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 2, 2026
@rust-log-analyzer

This comment has been minimized.

@fru1tworld fru1tworld force-pushed the fix-154600-trailing-comma branch from 37c3b75 to 0f7bc29 Compare April 2, 2026 07:00
@fru1tworld fru1tworld marked this pull request as ready for review April 2, 2026 08:32
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 2, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 2, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 2, 2026

r? @mati865

rustbot has assigned @mati865.
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 69 candidates
  • Random selection from 13 candidates

@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented Apr 2, 2026

i personally think, that having trailing comma is fine, because
a) this code compiles
b) this is not misleading in any way

i mean, sure that ideally we want to not have this trailing comma, but looking at fix it looks to me like using a tank against a fly

so, if I were to weigh up the pros and cons, i think i'd be against it

@fru1tworld
Copy link
Copy Markdown
Author

i mean, sure that ideally we want to not have this trailing comma,

Thanks for the feedback
that's a fair point. I agree the current approach is heavier than it needs to be for a cosmetic issue.

Would a more minimal fix be acceptable? Instead of adding fields to Segment/MissingLifetime, I could just adjust the suggestion string in the MissingLifetimeKind::Comma branch — no struct changes, no new field propagation.

That would keep the diff to a few lines in diagnostics.rs only.

If you still feel it's not worth it even at that scope, I'm fine closing this.

@Kivooeo
Copy link
Copy Markdown
Member

Kivooeo commented Apr 2, 2026

if you know more easier approach why was this chosen originally?

@fru1tworld fru1tworld force-pushed the fix-154600-trailing-comma branch from f7e0899 to adf06b7 Compare April 2, 2026 11:06
@fru1tworld
Copy link
Copy Markdown
Author

I wasn't aware of span_look_ahead when I wrote the original version
This revision drops all struct changes and uses it instead

@fru1tworld fru1tworld force-pushed the fix-154600-trailing-comma branch from adf06b7 to e0fc2da Compare April 2, 2026 11:15
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.

E0106 suggestion includes unnecessary trailing comma in lifetime arguments

5 participants