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-14959] [AutoDiff] Declaring protocol conformances for synthesized TangentVectors sometimes doesn't do anything #57301

Closed
porterchild opened this issue Jul 23, 2021 · 2 comments
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@porterchild
Copy link

Previous ID SR-14959
Radar rdar://problem/81019918
Original Reporter @porterchild
Type Bug
Status Resolved
Resolution Duplicate
Environment

7/7/21 trunk snapshot

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AutoDiff
Assignee None
Priority Medium

md5: f2964dbf441e2772fbb2a62aa7c4105e

duplicates:

  • SR-14229 [AutoDiff] conformance of synthesized TangentVector doesn't work

Issue Description:

Here is an example where declaring a conformance of a synthesized TangentVector to a protocol doesn't actually do anything:

import Foundation
import _Differentiation

public protocol P {}

struct S: Differentiable {
    var a: Float = 23
}

// conformance to P works fine with S
extension S: P {}
func asdf(p: P) -> S{
    return p as! S
}

// conformance to P doesn't work with S.TangentVector ("Cast from 'P' to unrelated type 'S.TangentVector' always fails")
extension S.TangentVector: P {}
func asdf2(p: P) -> S.TangentVector{
    return p as! S.TangentVector
}


let instance = S()
let tangent = S.TangentVector(a: 9)


// conformance to P works fine with S
print(asdf(p: instance))
// conformance to P doesn't work with S.TangentVector, despite declaring that exact conformance above ("Argument type 'S.TangentVector' does not conform to expected type 'P'")
print(asdf2(p: tangent))

Current workaround is to make a manual TangentVector (thanks @BradLarson):

import Foundation
import _Differentiation

protocol P {}

struct S: Differentiable {
    var a: Float = 23
    struct STangent: Differentiable, AdditiveArithmetic {
        var a: Float
    }
    typealias TangentVector = STangent
}

extension S {
    mutating func move(by offset: STangent) {
        self.a += offset.a
    }
}

extension S: P {}
func asdf(p: P) -> S{
    return p as! S
}

extension S.TangentVector: P {}
func asdf2(p: P) -> S.TangentVector{
    return p as! S.TangentVector
}

let instance = S()
let tangent = S.TangentVector(a: 9)

print(asdf(p: instance))
print(asdf2(p: tangent))
@typesanitizer
Copy link

@swift-ci create

@rxwei
Copy link
Member

rxwei commented Jul 27, 2021

This is a known issue and duplicates SR-14229. We don’t have plans to fix it anytime soon but patches are welcome!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants