Skip to content

Commit

Permalink
Fix opaqueGenericParameters being applied to declarations with attr…
Browse files Browse the repository at this point in the history
…ibutes or macros
  • Loading branch information
nicklockwood committed Apr 22, 2024
1 parent 84cc138 commit 383fd48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6566,10 +6566,10 @@ public struct _FormatRules {
options: ["someany"]
) { formatter in
formatter.forEach(.keyword) { keywordIndex, keyword in
guard // Apply this rule to any function-like declaration
["func", "init", "subscript"].contains(keyword.string),
// Opaque generic parameter syntax is only supported in Swift 5.7+
guard // Opaque generic parameter syntax is only supported in Swift 5.7+
formatter.options.swiftVersion >= "5.7",
// Apply this rule to any function-like declaration
[.keyword("func"), .keyword("init"), .keyword("subscript")].contains(keyword),
// Validate that this is a generic method using angle bracket syntax,
// and find the indices for all of the key tokens
let paramListStartIndex = formatter.index(of: .startOfScope("("), after: keywordIndex),
Expand All @@ -6579,7 +6579,9 @@ public struct _FormatRules {
genericSignatureStartIndex < paramListStartIndex,
genericSignatureEndIndex < paramListStartIndex,
let openBraceIndex = formatter.index(of: .startOfScope("{"), after: paramListEndIndex),
let closeBraceIndex = formatter.endOfScope(at: openBraceIndex)
let closeBraceIndex = formatter.endOfScope(at: openBraceIndex),
// Ignore anything with attributes
!formatter.modifiersForDeclaration(at: keywordIndex, contains: { $1.hasPrefix("@") })
else { return }

var genericTypes = [Formatter.GenericType]()
Expand Down
11 changes: 11 additions & 0 deletions Tests/RulesTests+Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2856,6 +2856,17 @@ class SyntaxTests: RulesTests {
testFormatting(for: input, rule: FormatRules.opaqueGenericParameters, options: options)
}

func testIssue1684() {
let input = """
@_specialize(where S == Int)
func foo<S: Sequence<Element>>(t: S) {
print(t)
}
"""
let options = FormatOptions(swiftVersion: "5.7")
testFormatting(for: input, rule: FormatRules.opaqueGenericParameters, options: options)
}

// MARK: - genericExtensions

func testGenericExtensionNotModifiedBeforeSwift5_7() {
Expand Down

0 comments on commit 383fd48

Please sign in to comment.