mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[TypeChecker] NFC: Add tests for keypath dynamic member lookup
This commit is contained in:
55
test/Constraints/keypath_dynamic_member_lookup.swift
Normal file
55
test/Constraints/keypath_dynamic_member_lookup.swift
Normal file
@@ -0,0 +1,55 @@
|
||||
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s
|
||||
|
||||
struct Point {
|
||||
let x: Int
|
||||
var y: Int
|
||||
}
|
||||
|
||||
struct Rectangle {
|
||||
var topLeft, bottomRight: Point
|
||||
}
|
||||
|
||||
@dynamicMemberLookup
|
||||
struct Lens<T> {
|
||||
var obj: T
|
||||
|
||||
init(_ obj: T) {
|
||||
self.obj = obj
|
||||
}
|
||||
|
||||
subscript<U>(dynamicMember member: KeyPath<T, U>) -> Lens<U> {
|
||||
get { return Lens<U>(obj[keyPath: member]) }
|
||||
}
|
||||
|
||||
subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> Lens<U> {
|
||||
get { return Lens<U>(obj[keyPath: member]) }
|
||||
set { obj[keyPath: member] = newValue.obj }
|
||||
}
|
||||
|
||||
// Used to make sure that keypath and string based lookup are
|
||||
// property disambiguated.
|
||||
subscript(dynamicMember member: String) -> Lens<Int> {
|
||||
return Lens<Int>(42)
|
||||
}
|
||||
}
|
||||
|
||||
var topLeft = Point(x: 0, y: 0)
|
||||
var bottomRight = Point(x: 10, y: 10)
|
||||
|
||||
var lens = Lens(Rectangle(topLeft: topLeft,
|
||||
bottomRight: bottomRight))
|
||||
|
||||
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
|
||||
// CHECK-NEXT: apply %45<Rectangle, Point>({{.*}})
|
||||
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs7KeyPathCyxqd__G_tcluig
|
||||
// CHECK-NEXT: apply %54<Point, Int>({{.*}})
|
||||
_ = lens.topLeft.x
|
||||
|
||||
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
|
||||
// CHECK-NEXT: apply %69<Rectangle, Point>({{.*}})
|
||||
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
|
||||
// CHECK-NEXT: apply %76<Point, Int>({{.*}})
|
||||
_ = lens.topLeft.y
|
||||
|
||||
lens.topLeft = Lens(Point(x: 1, y: 2)) // Ok
|
||||
lens.bottomRight.y = Lens(12) // Ok
|
||||
Reference in New Issue
Block a user