Skip to content

Fix trailing comma incorrectly added to generic argument lists within expressions in Swift 6.2#2471

Closed
Copilot wants to merge 5 commits intodevelopfrom
copilot/fix-regression-trailing-comma
Closed

Fix trailing comma incorrectly added to generic argument lists within expressions in Swift 6.2#2471
Copilot wants to merge 5 commits intodevelopfrom
copilot/fix-regression-trailing-comma

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

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. The trailingCommas rule was unconditionally allowing trailing commas in all <> scopes for swiftVersion >= "6.2", causing an incorrect comma to be inserted when > ends up on its own line (e.g. after wrapArguments wraps a generic argument list):

private let value = Array<
    (
        foo: String,
        bar: Bool
    )
>()  // trailing comma incorrectly added before > in Swift 6.2

Investigation via git bisect traced the root cause to commit 02e45820 ("Update trailingCommas rule to support bug fixes in Swift 6.2"), which unconditionally set trailingCommaSupported = true in the endOfScope(">") handler for swiftVersion >= "6.2". The follow-up commit b27e7c59 added a partial fix to the ) handler (outer scope walk for <>) but left the > handler unprotected for expression contexts.

Changes

  • Sources/Rules/TrailingCommas.swift: In the endOfScope(">") handler, when swiftVersion < "6.3" and > is followed by (, use the new isGenericDeclaration(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, set trailingCommaSupported = false. Added the isGenericDeclaration(at:) helper to the Formatter extension at the bottom of the file.

  • Tests/Rules/TrailingCommasTests.swift: Updated testTrailingCommasNotAddedToGenericArgumentListInExpressionInSwift6_2 to use the wrapped > form that directly exercises the regression (where > is on its own line). Updated testTrailingCommasAddedToGenericArgumentListInExpressionInSwift6_3 to confirm Swift 6.3 correctly allows trailing commas in both the inner tuple and before > in expression context. Added testTrailingCommasNotAddedToTupleTypeInsideArrayTypeSugarInSwift6_2 to cover the case where typeSugar converts Array<(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.

@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 88.88889% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.28%. Comparing base (e82dc09) to head (660c05b).

Files with missing lines Patch % Lines
Sources/Rules/TrailingCommas.swift 88.88% 3 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI and others added 2 commits March 19, 2026 23:22
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>
Copilot AI changed the title [WIP] Fix regression by removing trailing comma in Swift example Fix trailing comma incorrectly added to generic argument lists within expressions in Swift 6.2 Mar 19, 2026
Copilot AI requested a review from calda March 19, 2026 23:38
if formatter.options.swiftVersion >= "6.2" {
trailingCommaSupported = true

// In Swift 6.2, trailing commas in generic argument lists within expressions
Copy link
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@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>
@calda calda closed this Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants