diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 66ed607aa..60c05367c 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -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), @@ -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]() diff --git a/Tests/RulesTests+Syntax.swift b/Tests/RulesTests+Syntax.swift index 01e9f06c0..0b85ddbc4 100644 --- a/Tests/RulesTests+Syntax.swift +++ b/Tests/RulesTests+Syntax.swift @@ -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>(t: S) { + print(t) + } + """ + let options = FormatOptions(swiftVersion: "5.7") + testFormatting(for: input, rule: FormatRules.opaqueGenericParameters, options: options) + } + // MARK: - genericExtensions func testGenericExtensionNotModifiedBeforeSwift5_7() {