Skip to content

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

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
fru1tworld:fix-154600-trailing-comma
Apr 4, 2026
Merged

Fix trailing comma in lifetime suggestion for empty angle brackets#154703
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
fru1tworld:fix-154600-trailing-comma

Conversation

@fru1tworld
Copy link
Copy Markdown
Contributor

@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
Contributor 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
Contributor 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
let sugg: String = std::iter::repeat_n(existing_name.as_str(), lt.count)
.intersperse(", ")
.collect();
let is_empty_brackets = source_map.span_look_ahead(lt.span, ">", Some(1)).is_some();
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.

nit: span_look_ahead can ignore white space, if you set Some(1) here, " >" will not be checked for your code, so maybe Some(50) for limit is better.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TIL, thanks! fixed!

@chenyukang
Copy link
Copy Markdown
Member

nit: please don't use AI to generate PR description, it just too verbose and noisy, keep it empty for trivial fix and only add some extra important notes for a PR.

@fru1tworld fru1tworld force-pushed the fix-154600-trailing-comma branch from e0fc2da to 59d3092 Compare April 3, 2026 02:20
@fru1tworld
Copy link
Copy Markdown
Contributor Author

Got it, I'll keep it concise. Thanks for the feedback

@rust-log-analyzer

This comment has been minimized.

@mati865
Copy link
Copy Markdown
Member

mati865 commented Apr 3, 2026

r? @Kivooeo

As they already started reviewing, feel free to reroll.

@rustbot rustbot assigned Kivooeo and unassigned mati865 Apr 3, 2026
@fru1tworld fru1tworld requested a review from chenyukang April 3, 2026 10:33
@chenyukang
Copy link
Copy Markdown
Member

Thanks!
@bors r=chenyukang

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 4, 2026

📌 Commit 46b0527 has been approved by chenyukang

It is now in the queue for this repository.

@rust-bors rust-bors bot added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Apr 4, 2026
@rust-bors rust-bors bot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 4, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 4, 2026
…ma, r=chenyukang

Fix trailing comma in lifetime suggestion for empty angle brackets

Fixes rust-lang#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.
rust-bors bot pushed a commit that referenced this pull request Apr 4, 2026
Rollup of 10 pull requests

Successful merges:

 - #154376 (Remove more BuiltinLintDiag variants - part 4)
 - #127534 (feat(core): impl Step for NonZero<u*>)
 - #153286 (various fixes for scalable vectors)
 - #153592 (Add  `min_adt_const_params` gate)
 - #154675 (Improve shadowed private field diagnostics)
 - #154703 (Fix trailing comma in lifetime suggestion for empty angle brackets)
 - #154653 (Remove rustc_on_unimplemented's append_const_msg)
 - #154743 (Remove an unused `StableHash` impl.)
 - #154752 (Add comment to borrow-checker)
 - #154764 (Add tests for three ICEs that have already been fixed)
rust-bors bot pushed a commit that referenced this pull request Apr 4, 2026
Rollup of 5 pull requests

Successful merges:

 - #154376 (Remove more BuiltinLintDiag variants - part 4)
 - #154731 (llvm: Fix array ABI test to not check equality implementation)
 - #127534 (feat(core): impl Step for NonZero<u*>)
 - #154703 (Fix trailing comma in lifetime suggestion for empty angle brackets)
 - #154776 (Fix ICE in read_discriminant for enums with non-contiguous discriminants)
@rust-bors rust-bors bot merged commit 66457ac into rust-lang:main Apr 4, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Apr 4, 2026
rust-timer added a commit that referenced this pull request Apr 4, 2026
Rollup merge of #154703 - fru1tworld:fix-154600-trailing-comma, r=chenyukang

Fix trailing comma in lifetime suggestion for empty angle brackets

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

E0106 suggestion includes unnecessary trailing comma in lifetime arguments

6 participants