You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[AutoDiff] Make Differentiable derivation support property wrappers.
Differentiable conformance derivation now "peers through" property wrappers.
Synthesized TangentVector structs contain wrapped properties' TangentVectors as
stored properties, not wrappers' TangentVectors.
Property wrapper types are not required to conform to `Differentiable`.
Property wrapper types are required to provide `wrappedValue.set`, which is
needed to synthesize `mutating func move(along:)`.
```
import _Differentiation
@propertyWrapper
struct Wrapper<Value> {
var wrappedValue: Value
}
struct Struct: Differentiable {
@wrapper var x: Float = 0
// Compiler now synthesizes:
// struct TangentVector: Differentiable & AdditiveArithmetic {
// var x: Float
// ...
// }
}
```
Resolves SR-12638.
Copy file name to clipboardExpand all lines: test/AutoDiff/Sema/DerivedConformances/class_differentiable.swift
+25-15Lines changed: 25 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ class ImmutableStoredProperties: Differentiable {
38
38
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Int' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
39
39
letnondiff:Int
40
40
41
-
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'ImmutableStoredProperties' requires all stored properties to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
41
+
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'ImmutableStoredProperties' requires all stored properties not marked with `@noDerivative` to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
42
42
letdiff:Float
43
43
44
44
init(){
@@ -56,7 +56,8 @@ class MutableStoredPropertiesWithInitialValue: Differentiable {
56
56
}
57
57
// Test class with both an empty constructor and memberwise initializer.
letx=Float(1) // expected-warning {{synthesis of the 'Differentiable.move(along:)' requirement for 'AllMixedStoredPropertiesHaveInitialValue' requires all stored properties to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
59
+
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'AllMixedStoredPropertiesHaveInitialValue' requires all stored properties not marked with `@noDerivative` to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
60
+
letx=Float(1)
60
61
vary=Float(1)
61
62
// Memberwise initializer should be `init(y:)` since `x` is immutable.
62
63
staticfunc testMemberwiseInitializer(){
@@ -506,26 +507,35 @@ where T: AdditiveArithmetic {}
// TF-1190: Test `@noDerivative` warning for property wrapper backing storage properties.
510
512
511
513
@propertyWrapper
512
-
structWrapper<Value>{
514
+
structImmutableWrapper<Value>{
513
515
privatevarvalue:Value
514
-
varwrappedValue:Value{
515
-
get{ value }
516
-
set{value =newValue }
516
+
varwrappedValue:Value{ value }
517
+
init(wrappedValue:Value){
518
+
self.value =wrappedValue
517
519
}
518
520
}
519
-
structTF_1190<T>{}
520
-
classTF_1190_Outer:Differentiable{
521
-
// expected-warning @+1 {{stored property '_x' has no derivative because 'Wrapper<TF_1190<Float>>' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'WrappedProperties' requires all stored properties not marked with `@noDerivative` to be mutable; add an explicit '@noDerivative' attribute}}
// expected-warning @+1 {{stored property 'mutableInt' has no derivative because 'Generic<Int>' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Int' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute, or conform 'ImmutableStoredProperties' to 'AdditiveArithmetic'}} {{3-3=@noDerivative }}
25
25
letnondiff:Int
26
26
27
-
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'ImmutableStoredProperties' requires all stored properties to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute, or conform 'ImmutableStoredProperties' to 'AdditiveArithmetic'}} {{3-3=@noDerivative }}
27
+
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'ImmutableStoredProperties' requires all stored properties not marked with `@noDerivative` to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute, or conform 'ImmutableStoredProperties' to 'AdditiveArithmetic}} {{3-3=@noDerivative }}
letx=Float(1) // expected-warning {{synthesis of the 'Differentiable.move(along:)' requirement for 'AllMixedStoredPropertiesHaveInitialValue' requires all stored properties to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
39
+
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'AllMixedStoredPropertiesHaveInitialValue' requires all stored properties not marked with `@noDerivative` to be mutable; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
40
+
letx=Float(1)
40
41
vary=Float(1)
41
42
// Memberwise initializer should be `init(y:)` since `x` is immutable.
42
43
staticfunc testMemberwiseInitializer(){
@@ -323,20 +324,32 @@ where T: AdditiveArithmetic {}
// TF-1190: Test `@noDerivative` warning for property wrapper backing storage properties.
327
329
328
330
@propertyWrapper
329
-
structWrapper<Value>{
331
+
structImmutableWrapper<Value>{
330
332
privatevarvalue:Value
331
-
varwrappedValue:Value{
332
-
value
333
-
}
333
+
varwrappedValue:Value{ value }
334
+
}
335
+
336
+
@propertyWrapper
337
+
structWrapper<Value>{
338
+
varwrappedValue:Value
334
339
}
335
-
structTF_1190<T>{}
336
-
structTF_1190_Outer:Differentiable{
337
-
// expected-warning @+1 {{stored property '_x' has no derivative because 'Wrapper<TF_1190<Float>>' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
// expected-warning @+1 {{synthesis of the 'Differentiable.move(along:)' requirement for 'WrappedProperties' requires all stored properties not marked with `@noDerivative` to be mutable; add an explicit '@noDerivative' attribute}}
346
+
@ImmutableWrappervarimmutableInt:Generic<Int>
347
+
348
+
// expected-warning @+1 {{stored property 'mutableInt' has no derivative because 'Generic<Int>' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
0 commit comments