Skip to content

Commit 86d467f

Browse files
authored
Merge pull request swiftlang#188 from dylansturg/derivative_attr_crash
Handle derivative attr without params.
2 parents 9d552fb + 3482b9d commit 86d467f

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
@@ -2140,7 +2140,8 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
21402140
// `@transpose(...)` attribute.
21412141
before(node.ofLabel, tokens: .open)
21422142
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
2143-
after(node.comma, tokens: .close)
2143+
// The comma after originalDeclName is optional and is only present if there are diffParams.
2144+
after(node.comma ?? node.originalDeclName.lastToken, tokens: .close)
21442145

21452146
if let diffParams = node.diffParams {
21462147
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)