Fix trailing comma incorrectly added to generic argument lists within expressions in Swift 6.2#2471
Fix trailing comma incorrectly added to generic argument lists within expressions in Swift 6.2#2471
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #2471 +/- ##
========================================
Coverage 95.28% 95.28%
========================================
Files 165 165
Lines 25243 25270 +27
========================================
+ Hits 24052 24079 +27
Misses 1191 1191 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Co-authored-by: calda <1811727+calda@users.noreply.github.com>
…sions in Swift 6.2 Co-authored-by: calda <1811727+calda@users.noreply.github.com>
| if formatter.options.swiftVersion >= "6.2" { | ||
| trailingCommaSupported = true | ||
|
|
||
| // In Swift 6.2, trailing commas in generic argument lists within expressions |
There was a problem hiding this comment.
@copilot, this is a new regression in the past few weeks. Can you find what specific change caused the regression?
This code is pretty complicated. Maybe it would be better to revert the change that caused the regression, or scope that change to only Swift 6.3+.
There was a problem hiding this comment.
The regression is in the > endOfScope handler, which unconditionally sets trailingCommaSupported = true for all swiftVersion >= "6.2" contexts since commit 02e45820. However, trailing commas in generic argument lists within expressions (like Array<T>()) require Swift 6.3+. Swift 6.2 only supports them in type contexts and declarations.
The regression surfaces when > ends up on its own line (e.g., after wrapArguments formats the code), at which point addOrRemoveTrailingComma inserts a comma before >.
Rather than reverting 02e45820 (which would lose all the correct Swift 6.2 improvements), I've scoped the expression-context exclusion to swiftVersion < "6.3": when > is followed by (, we check if it's a declaration (func foo<T>(), init<T>(), init?<T>(), subscript<T>()). If not, it's expression context and no trailing comma is added. Updated in commit 7aa0009.
There was a problem hiding this comment.
@copilot, I'm very certain it is not commit 02e45820 that introduced the regression. That commit is from September. The regression was introduced within the past few weeks.
…sions in Swift 6.2 Co-authored-by: calda <1811727+calda@users.noreply.github.com>
… expressions in Swift 6.2 Co-authored-by: calda <1811727+calda@users.noreply.github.com>
In Swift 6.2, trailing commas in generic argument lists within expressions (e.g.
Array<Int,>()) are not valid — that only became supported in Swift 6.3. ThetrailingCommasrule was unconditionally allowing trailing commas in all<>scopes forswiftVersion >= "6.2", causing an incorrect comma to be inserted when>ends up on its own line (e.g. afterwrapArgumentswraps a generic argument list):Investigation via
git bisecttraced the root cause to commit02e45820("Update trailingCommas rule to support bug fixes in Swift 6.2"), which unconditionally settrailingCommaSupported = truein theendOfScope(">")handler forswiftVersion >= "6.2". The follow-up commitb27e7c59added a partial fix to the)handler (outer scope walk for<>) but left the>handler unprotected for expression contexts.Changes
Sources/Rules/TrailingCommas.swift: In theendOfScope(">")handler, whenswiftVersion < "6.3"and>is followed by(, use the newisGenericDeclaration(at:)helper to detect whether the<>belongs to a declaration (func identifier<T>(),init<T>(),init?<T>(),init!<T>(),subscript<T>()) or an expression (Array<T>()). For expression contexts, settrailingCommaSupported = false. Added theisGenericDeclaration(at:)helper to theFormatterextension at the bottom of the file.Tests/Rules/TrailingCommasTests.swift: UpdatedtestTrailingCommasNotAddedToGenericArgumentListInExpressionInSwift6_2to use the wrapped>form that directly exercises the regression (where>is on its own line). UpdatedtestTrailingCommasAddedToGenericArgumentListInExpressionInSwift6_3to confirm Swift 6.3 correctly allows trailing commas in both the inner tuple and before>in expression context. AddedtestTrailingCommasNotAddedToTupleTypeInsideArrayTypeSugarInSwift6_2to cover the case wheretypeSugarconvertsArray<(T)>()to[(T)]().✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.