|
| 1 | +// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s |
| 2 | + |
| 3 | +struct Point { |
| 4 | + let x: Int |
| 5 | + var y: Int |
| 6 | +} |
| 7 | + |
| 8 | +struct Rectangle { |
| 9 | + var topLeft, bottomRight: Point |
| 10 | +} |
| 11 | + |
| 12 | +@dynamicMemberLookup |
| 13 | +struct Lens<T> { |
| 14 | + var obj: T |
| 15 | + |
| 16 | + init(_ obj: T) { |
| 17 | + self.obj = obj |
| 18 | + } |
| 19 | + |
| 20 | + subscript<U>(dynamicMember member: KeyPath<T, U>) -> Lens<U> { |
| 21 | + get { return Lens<U>(obj[keyPath: member]) } |
| 22 | + } |
| 23 | + |
| 24 | + subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> Lens<U> { |
| 25 | + get { return Lens<U>(obj[keyPath: member]) } |
| 26 | + set { obj[keyPath: member] = newValue.obj } |
| 27 | + } |
| 28 | + |
| 29 | + // Used to make sure that keypath and string based lookup are |
| 30 | + // property disambiguated. |
| 31 | + subscript(dynamicMember member: String) -> Lens<Int> { |
| 32 | + return Lens<Int>(42) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +var topLeft = Point(x: 0, y: 0) |
| 37 | +var bottomRight = Point(x: 10, y: 10) |
| 38 | + |
| 39 | +var lens = Lens(Rectangle(topLeft: topLeft, |
| 40 | + bottomRight: bottomRight)) |
| 41 | + |
| 42 | +// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig |
| 43 | +// CHECK-NEXT: apply %45<Rectangle, Point>({{.*}}) |
| 44 | +// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs7KeyPathCyxqd__G_tcluig |
| 45 | +// CHECK-NEXT: apply %54<Point, Int>({{.*}}) |
| 46 | +_ = lens.topLeft.x |
| 47 | + |
| 48 | +// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig |
| 49 | +// CHECK-NEXT: apply %69<Rectangle, Point>({{.*}}) |
| 50 | +// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig |
| 51 | +// CHECK-NEXT: apply %76<Point, Int>({{.*}}) |
| 52 | +_ = lens.topLeft.y |
| 53 | + |
| 54 | +lens.topLeft = Lens(Point(x: 1, y: 2)) // Ok |
| 55 | +lens.bottomRight.y = Lens(12) // Ok |
0 commit comments