Commit 66457ac
authored
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.4 files changed
Lines changed: 55 additions & 8 deletions
File tree
- compiler/rustc_resolve/src/late
- tests/ui
- generics
- lifetimes
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4094 | 4094 | | |
4095 | 4095 | | |
4096 | 4096 | | |
| 4097 | + | |
4097 | 4098 | | |
4098 | 4099 | | |
4099 | 4100 | | |
| |||
4104 | 4105 | | |
4105 | 4106 | | |
4106 | 4107 | | |
4107 | | - | |
4108 | | - | |
| 4108 | + | |
| 4109 | + | |
4109 | 4110 | | |
| 4111 | + | |
| 4112 | + | |
| 4113 | + | |
4110 | 4114 | | |
4111 | 4115 | | |
4112 | 4116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
| 31 | + | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
59 | | - | |
| 58 | + | |
| 59 | + | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
63 | | - | |
| 62 | + | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
0 commit comments