Skip to content

Commit f957d52

Browse files
authored
Merge pull request #559 from allevato/fix-deriv-reg
Remove the compiler condition guarding `DerivativeRegistrationAttributeArgumentsSyntax`.
2 parents ebc0f9a + 348976f commit f957d52

File tree

2 files changed

+75
-82
lines changed

2 files changed

+75
-82
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,26 +2561,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
25612561
return .visitChildren
25622562
}
25632563

2564-
// `DerivativeRegistrationAttributeArguments` was added after the Swift 5.2 release was cut.
2565-
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
2566-
override func rewrite(_ node: DerivativeRegistrationAttributeArgumentsSyntax)
2567-
-> SyntaxVisitorContinueKind
2568-
{
2569-
// This node encapsulates the entire list of arguments in a `@derivative(...)` or
2570-
// `@transpose(...)` attribute.
2571-
before(node.ofLabel, tokens: .open)
2572-
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
2573-
// The comma after originalDeclName is optional and is only present if there are diffParams.
2574-
after(node.comma ?? node.originalDeclName.lastToken, tokens: .close)
2575-
2576-
if let diffParams = node.diffParams {
2577-
before(diffParams.firstToken, tokens: .break(.same), .open)
2578-
after(diffParams.lastToken, tokens: .close)
2579-
}
2564+
override func visit(_ node: DerivativeRegistrationAttributeArgumentsSyntax)
2565+
-> SyntaxVisitorContinueKind
2566+
{
2567+
// This node encapsulates the entire list of arguments in a `@derivative(...)` or
2568+
// `@transpose(...)` attribute.
2569+
before(node.ofLabel, tokens: .open)
2570+
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
2571+
// The comma after originalDeclName is optional and is only present if there are diffParams.
2572+
after(node.comma ?? node.originalDeclName.lastToken(viewMode: .sourceAccurate), tokens: .close)
25802573

2581-
return .visitChildren
2574+
if let diffParams = node.diffParams {
2575+
before(diffParams.firstToken(viewMode: .sourceAccurate), tokens: .break(.same), .open)
2576+
after(diffParams.lastToken(viewMode: .sourceAccurate), tokens: .close)
25822577
}
2583-
#endif
2578+
2579+
return .visitChildren
2580+
}
25842581

25852582
override func visit(_ node: DifferentiabilityParamsClauseSyntax) -> SyntaxVisitorContinueKind {
25862583
// This node encapsulates the `wrt:` label and value/variable in a `@differentiable`,

Tests/SwiftFormatPrettyPrintTests/DifferentiationAttributeTests.swift

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -96,84 +96,80 @@ final class DifferentiationAttributeTests: PrettyPrintTestCase {
9696
}
9797

9898
func testDerivative() {
99-
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
100-
let input =
101-
"""
102-
@derivative(of: foo)
103-
func deriv<T>() {}
99+
let input =
100+
"""
101+
@derivative(of: foo)
102+
func deriv<T>() {}
104103
105-
@derivative(of: foo, wrt: x)
106-
func deriv<T>(_ x: T) {}
104+
@derivative(of: foo, wrt: x)
105+
func deriv<T>(_ x: T) {}
107106
108-
@derivative(of: foobar, wrt: x)
109-
func deriv<T>(_ x: T) {}
107+
@derivative(of: foobar, wrt: x)
108+
func deriv<T>(_ x: T) {}
110109
111-
@derivative(of: foobarbaz, wrt: theVariableNamedX)
112-
func deriv<T>(_ theVariableNamedX: T) {}
113-
"""
110+
@derivative(of: foobarbaz, wrt: theVariableNamedX)
111+
func deriv<T>(_ theVariableNamedX: T) {}
112+
"""
114113

115-
let expected =
116-
"""
117-
@derivative(of: foo)
118-
func deriv<T>() {}
114+
let expected =
115+
"""
116+
@derivative(of: foo)
117+
func deriv<T>() {}
119118
120-
@derivative(of: foo, wrt: x)
121-
func deriv<T>(_ x: T) {}
119+
@derivative(of: foo, wrt: x)
120+
func deriv<T>(_ x: T) {}
122121
123-
@derivative(
124-
of: foobar, wrt: x
125-
)
126-
func deriv<T>(_ x: T) {}
122+
@derivative(
123+
of: foobar, wrt: x
124+
)
125+
func deriv<T>(_ x: T) {}
127126
128-
@derivative(
129-
of: foobarbaz,
130-
wrt: theVariableNamedX
131-
)
132-
func deriv<T>(
133-
_ theVariableNamedX: T
134-
) {}
127+
@derivative(
128+
of: foobarbaz,
129+
wrt: theVariableNamedX
130+
)
131+
func deriv<T>(
132+
_ theVariableNamedX: T
133+
) {}
135134
136-
"""
135+
"""
137136

138-
assertPrettyPrintEqual(input: input, expected: expected, linelength: 28)
139-
#endif
137+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 28)
140138
}
141139

142140
func testTranspose() {
143-
#if HAS_DERIVATIVE_REGISTRATION_ATTRIBUTE
144-
let input =
145-
"""
146-
@transpose(of: foo, wrt: 0)
147-
func trans<T>(_ v: T) {}
148-
149-
@transpose(of: foobar, wrt: 0)
150-
func trans<T>(_ v: T) {}
151-
152-
@transpose(of: someReallyLongName, wrt: 0)
153-
func trans<T>(_ theVariableNamedV: T) {}
154-
"""
155-
156-
let expected =
157-
"""
158-
@transpose(of: foo, wrt: 0)
159-
func trans<T>(_ v: T) {}
160-
161-
@transpose(
162-
of: foobar, wrt: 0
163-
)
164-
func trans<T>(_ v: T) {}
141+
let input =
142+
"""
143+
@transpose(of: foo, wrt: 0)
144+
func trans<T>(_ v: T) {}
165145
166-
@transpose(
167-
of: someReallyLongName,
168-
wrt: 0
169-
)
170-
func trans<T>(
171-
_ theVariableNamedV: T
172-
) {}
146+
@transpose(of: foobar, wrt: 0)
147+
func trans<T>(_ v: T) {}
173148
174-
"""
149+
@transpose(of: someReallyLongName, wrt: 0)
150+
func trans<T>(_ theVariableNamedV: T) {}
151+
"""
152+
153+
let expected =
154+
"""
155+
@transpose(of: foo, wrt: 0)
156+
func trans<T>(_ v: T) {}
157+
158+
@transpose(
159+
of: foobar, wrt: 0
160+
)
161+
func trans<T>(_ v: T) {}
162+
163+
@transpose(
164+
of: someReallyLongName,
165+
wrt: 0
166+
)
167+
func trans<T>(
168+
_ theVariableNamedV: T
169+
) {}
170+
171+
"""
175172

176-
assertPrettyPrintEqual(input: input, expected: expected, linelength: 27)
177-
#endif
173+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 27)
178174
}
179175
}

0 commit comments

Comments
 (0)