Fix trailing comma in lifetime suggestion for empty angle brackets#154703
Fix trailing comma in lifetime suggestion for empty angle brackets#154703rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
37c3b75 to
0f7bc29
Compare
|
r? @mati865 rustbot has assigned @mati865. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
i personally think, that having trailing comma is fine, because 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 |
Thanks for the feedback 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. |
|
if you know more easier approach why was this chosen originally? |
f7e0899 to
adf06b7
Compare
|
I wasn't aware of span_look_ahead when I wrote the original version |
adf06b7 to
e0fc2da
Compare
| 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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
TIL, thanks! fixed!
|
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. |
e0fc2da to
59d3092
Compare
|
Got it, I'll keep it concise. Thanks for the feedback |
This comment has been minimized.
This comment has been minimized.
59d3092 to
46b0527
Compare
|
r? @Kivooeo As they already started reviewing, feel free to reroll. |
|
Thanks! |
…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.
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)
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)
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.
Fixes #154600
When suggesting a lifetime parameter (e.g.,
'a) for a type likeFoo<>(empty angle brackets), the compiler was incorrectly producingFoo<'a, >with a trailing comma.Root Cause
The
has_existing_paramscheck usedsegment.argsto determine if generic parameters exist. However, for empty angle brackets (<>), the parser doesn't create anyargs, sohas_existing_paramswasfalse— 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 toFoo<'a, >instead ofFoo<'a>.Fix
Replace the
segment.args-based check with a source text inspection usingspan_to_snippet. The new logic:<character in the source text<and>This correctly identifies
<>as having no existing parameters, avoiding the trailing comma.Test
Added
tests/ui/lifetimes/E0106-trailing-comma-in-lifetime-suggestion.rscovering the specific case of empty angle brackets with lifetime suggestions.