File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -1920,6 +1920,12 @@ func _setAtWritableKeyPath<Root, Value>(
1920
1920
keyPath: WritableKeyPath < Root , Value > ,
1921
1921
value: __owned Value
1922
1922
) {
1923
+ if type ( of: keyPath) . kind == . reference {
1924
+ return _setAtReferenceWritableKeyPath ( root: root,
1925
+ keyPath: _unsafeUncheckedDowncast ( keyPath,
1926
+ to: ReferenceWritableKeyPath< Root, Value> . self ) ,
1927
+ value: value)
1928
+ }
1923
1929
// TODO: we should be able to do this more efficiently than projecting.
1924
1930
let ( addr, owner) = keyPath. _projectMutableAddress ( from: & root)
1925
1931
addr. pointee = value
Original file line number Diff line number Diff line change @@ -1028,5 +1028,20 @@ keyPath.test("tail allocated c array") {
1028
1028
expectEqual ( 4 , offset)
1029
1029
}
1030
1030
1031
+ keyPath. test ( " ReferenceWritableKeyPath statically typed as WritableKeyPath " ) {
1032
+ let inner = C < Int > ( x: 42 , y: nil , z: 43 )
1033
+ var outer = C < C < Int > > ( x: 44 , y: nil , z: inner)
1034
+ let keyPath = \C < C < Int > > . z. x
1035
+ let upcastKeyPath = keyPath as WritableKeyPath
1036
+
1037
+ expectEqual ( outer [ keyPath: keyPath] , 42 )
1038
+ outer [ keyPath: keyPath] = 43
1039
+ expectEqual ( outer [ keyPath: keyPath] , 43 )
1040
+
1041
+ expectEqual ( outer [ keyPath: upcastKeyPath] , 43 )
1042
+ outer [ keyPath: upcastKeyPath] = 44
1043
+ expectEqual ( outer [ keyPath: upcastKeyPath] , 44 )
1044
+ }
1045
+
1031
1046
runAllTests ( )
1032
1047
You can’t perform that action at this time.
0 commit comments