1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
class MyLabel {
var text = "label"
}
class Controller {
fileprivate let label = MyLabel()
fileprivate var secondLabel: MyLabel? = MyLabel()
public var thirdLabel: MyLabel? = MyLabel()
subscript(string: String) -> String {
get {
""
}
set {
}
}
subscript(int int: Int, str str: String, otherInt: Int) -> Int {
get {
0
}
set {
}
}
subscript() -> Int {
0
}
subscript<T>(array: [T]) -> T? {
array.first
}
subscript<T, U>(array: [T], array2: [U]) -> T? {
array.first
}
subscript<T>(array array: [T]) -> T? {
array.first
}
subscript<T, U>(array array: [T], array2 array2: [U]) -> T? {
array.first
}
}
struct S {
var a: Int
}
struct Container<V> {
var v : V
init(_ v: V) {
self.v = v
}
func useKeyPath<V2: AnyObject>(_ keyPath: KeyPath<V, V2>) -> String {
return (v[keyPath: keyPath] as! MyLabel).text
}
}
extension Container where V: Controller {
func test() -> String {
return useKeyPath(\.label)
}
}
// CHECK: label
print(Container(Controller()).test())
public class GenericController<U> {
init(_ u: U) {
self.u = u
}
var u : U
fileprivate let label = MyLabel()
}
public func generic_class_constrained_keypath<U, V>(_ c: V) where V : GenericController<U> {
let kp = \V.label
print(kp)
print(c[keyPath: kp].text)
}
// CHECK: GenericController<Int>.label
// CHECK: label
generic_class_constrained_keypath(GenericController(5))
// CHECK: {{\\Controller\.secondLabel!\.text|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>!\.<computed 0x.* \(String\)>}}
print(\Controller.secondLabel!.text)
// CHECK: {{\\Controller\.subscript\(_: String\)|\\Controller\.<computed 0x.* \(String\)>}}
print(\Controller["abc"])
// CHECK: \S.a
print(\S.a)
// CHECK: {{\\Controller\.subscript\(int: Int, str: String, _: Int\)|\\Controller\.<computed 0x.* \(Int\)>}}
print(\Controller[int: 0, str: "", 0])
// CHECK: {{\\Controller\.thirdLabel|\\Controller\.<computed 0x.* \(Optional<MyLabel>\)>}}
print(\Controller.thirdLabel)
// CHECK: {{\\Controller\.subscript\(\)|\\Controller\.<computed 0x.* \(Int\)>}}
print(\Controller.[])
// CHECK: \Controller.self
print(\Controller.self)
// Subscripts with dependent generic types don't produce good output currently,
// so we're just checking to make sure they don't crash.
// CHECK: \Controller.
print(\Controller[[42]])
// CHECK: Controller.
print(\Controller[[42], [42]])
// CHECK: \Controller.
print(\Controller[array: [42]])
// CHECK: \Controller.
print(\Controller[array: [42], array2: [42]])
do {
struct S {
var i: Int
}
func test<T, U>(v: T, _ kp: any KeyPath<T, U> & Sendable) {
print(v[keyPath: kp])
}
// CHECK: 42
test(v: S(i: 42), \.i)
}
do {
@dynamicMemberLookup
struct Test<T> {
var obj: T
subscript<U>(dynamicMember member: KeyPath<T, U> & Sendable) -> U {
get { obj[keyPath: member] }
}
}
// CHECK: 5
print(Test(obj: "Hello").utf8.count)
}
do {
struct S1: Hashable {}
struct S2 {
subscript(param: S1) -> String { "Subscript with private type" }
}
let kp = \S2[S1()]
// CHECK: Subscript with private type
print(S2()[keyPath: kp])
// CHECK: {{\\S2\.subscript\(_: S1 #[0-9]+\)|\S2\.<computed 0x.* \(String\)>}}
print(kp)
}
|