Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-12002] [swift-format] Incorrect formatting for @differentiable, @derivative, @transpose attributes #337

Closed
dan-zheng opened this issue Jan 9, 2020 · 2 comments
Assignees
Labels
bug Something isn't working Format swift-format

Comments

@dan-zheng
Copy link

Previous ID SR-12002
Radar None
Original Reporter @dan-zheng
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s swift-format
Labels Bug, Format
Assignee @allevato
Priority Medium

md5: 51a61469373937af2c405988c17ca635

Issue Description:

The spaces between arguments in @differentiable, @derivative, and @transpose attributes gets deleted by swift-format.

Before swift-format (expected correct formatting):

@differentiable(wrt: x, vjp: derivativeFoo where T: Differentiable)
func foo<T>(_ x: T) -> T {
    x
}

@derivative(of: foo, wrt: x)
func derivativeFoo<T>(_ x: T) -> (value: T, pullback: (T.TangentVector) -> T.TangentVector)
where T: Differentiable {
    (x, { $0 })
}

@transpose(of: foo, wrt: 0)
func transposeFoo<T: Differentiable>(_ v: T) -> T where T == T.TangentVector {
    v
}

After swift-format:

$ swift-format test.swift
swift-format test.swift
@differentiable(wrt:x,vjp:derivativeFoowhere T: Differentiable)
func foo<T>(_ x: T) -> T {
    x
}

@derivative(of:foo,wrt:x)
func derivativeFoo<T>(_ x: T) -> (value: T, pullback: (T.TangentVector) -> T.TangentVector)
where T: Differentiable {
    (x, { $0 })
}

@transpose(of:foo,wrt:0)
func transposeFoo<T: Differentiable>(_ v: T) -> T where T == T.TangentVector {
    v
}

The spaces between attribute arguments is incorrectly removed, e.g. @differentiable(wrt:x,vjp:derivativeFoowhere T: Differentiable).

@dan-zheng
Copy link
Author

Consequence: this issue prevents us from running swift-format on code repositories that use @differentiable attributes with a where clause, since those attributes get formatted into invalid code.

Example repository: https://github.com/tensorflow/swift-apis.

$ swift-format -i -r Sources
$ swift build
...
tensorflow-swift-apis/Sources/TensorFlow/Operators/Math.swift:2362:43: error: protocol 'TensorFlowFloatingPoint' can only be used as a generic constraint because it has Self or associated type requirements
    @differentiable(wrt:selfwhere Scalar: TensorFlowFloatingPoint)
                                          ^
tensorflow-swift-apis/Sources/TensorFlow/Operators/Math.swift:2362:35: error: invalid redeclaration of 'Scalar'
    @differentiable(wrt:selfwhere Scalar: TensorFlowFloatingPoint)
                                  ^
tensorflow-swift-apis/Sources/TensorFlow/Operators/Basic.swift:800:35: note: 'Scalar' previously declared here
    @differentiable(wrt:selfwhere Scalar: TensorFlowFloatingPoint)
                                  ^

@allevato
Copy link
Collaborator

Fixed by #134.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 9, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Format swift-format
Projects
None yet
Development

No branches or pull requests

2 participants