Skip to content

Commit 3482b9d

Browse files
committed
Handle derivative attr without params.
Using `@derivative(of:...)` without a `wrt` clause previously crashed the formatter, due to an unmatched open token. Now the close token is added when there is no `wrt` clause too.
1 parent d8a1666 commit 3482b9d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,8 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21362136
// `@transpose(...)` attribute.
21372137
before(node.ofLabel, tokens: .open)
21382138
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
2139-
after(node.comma, tokens: .close)
2139+
// The comma after originalDeclName is optional and is only present if there are diffParams.
2140+
after(node.comma ?? node.originalDeclName.lastToken, tokens: .close)
21402141

21412142
if let diffParams = node.diffParams {
21422143
before(diffParams.firstToken, tokens: .break(.same), .open)

Tests/SwiftFormatPrettyPrintTests/DifferentiationAttributeTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ final class DifferentiationAttributeTests: PrettyPrintTestCase {
9999
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
100100
let input =
101101
"""
102+
@derivative(of: foo)
103+
func deriv<T>() {}
104+
102105
@derivative(of: foo, wrt: x)
103106
func deriv<T>(_ x: T) {}
104107
@@ -111,6 +114,9 @@ final class DifferentiationAttributeTests: PrettyPrintTestCase {
111114

112115
let expected =
113116
"""
117+
@derivative(of: foo)
118+
func deriv<T>() {}
119+
114120
@derivative(of: foo, wrt: x)
115121
func deriv<T>(_ x: T) {}
116122

0 commit comments

Comments
 (0)