swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.3k stars 10.33k forks source link

[SR-14989] [AutoDiff] Struct genericized over differentiable type can't access that type differentiably #57331

Open porterchild opened 3 years ago

porterchild commented 3 years ago
Previous ID SR-14989
Radar rdar://problem/81347317
Original Reporter @porterchild
Type Bug
Environment 7/7 snapshot
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 1 | |Component/s | Compiler | |Labels | Bug, AutoDiff | |Assignee | kaiau.tk@gmail.com (JIRA) | |Priority | Medium | md5: f5d010cdf3f202bb1c50f71d8f3ee6b4

Issue Description:

Even though the P protocol has declared that 'var a' is differentiable, using it below fails:

import Foundation
import _Differentiation

protocol P: Differentiable {
    @differentiable(reverse)
    var a: Double {get set}
}

struct S: P {
    var a: Double = 3
}

// this works when something concrete is held, setA is differentiable
//struct Holder: Differentiable {
//    var p: S
//
//    @differentiable(reverse)
//    mutating func setA(a: Double) {
//        self.p.a = a
//    }
//}

// this doesn't work, after genericizing the Holder,
struct Holder<ConcreteP: P>: Differentiable {
    var p: ConcreteP

    @differentiable(reverse)
    mutating func setA(a: Double) {
        self.p.a = a // "Expression is not differentiable" at line 'self.p.a = a' even though P.a has been marked @differentiable
    }
}
typesanitizer commented 3 years ago

@swift-ci create

jkshtj commented 4 months ago

Does not crash on 05/24 toolchain.