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
|
// RUN: %target-typecheck-verify-swift
// Check that all combinations of key paths produce the expected result type
// and choose the expected overloads.
#if BUILDING_OUTSIDE_STDLIB
import Swift
#endif
func expect<T>(_: inout T, is: T.Type) {}
func wellTypedAppends<T, U, V>(readOnlyLeft: KeyPath<T, U>,
writableLeft: WritableKeyPath<T, U>,
referenceLeft: ReferenceWritableKeyPath<T, U>,
readOnlyRight: KeyPath<U, V>,
writableRight: WritableKeyPath<U, V>,
referenceRight: ReferenceWritableKeyPath<U, V>){
var a = readOnlyLeft.appending(path: readOnlyRight)
expect(&a, is: KeyPath<T, V>.self)
var b = readOnlyLeft.appending(path: writableRight)
expect(&b, is: KeyPath<T, V>.self)
var c = readOnlyLeft.appending(path: referenceRight)
expect(&c, is: ReferenceWritableKeyPath<T, V>.self)
var d = writableLeft.appending(path: readOnlyRight)
expect(&d, is: KeyPath<T, V>.self)
var e = writableLeft.appending(path: writableRight)
expect(&e, is: WritableKeyPath<T, V>.self)
var f = writableLeft.appending(path: referenceRight)
expect(&f, is: ReferenceWritableKeyPath<T, V>.self)
var g = referenceLeft.appending(path: readOnlyRight)
expect(&g, is: KeyPath<T, V>.self)
var h = referenceLeft.appending(path: writableRight)
expect(&h, is: ReferenceWritableKeyPath<T, V>.self)
var i = referenceLeft.appending(path: referenceRight)
expect(&i, is: ReferenceWritableKeyPath<T, V>.self)
}
func mismatchedAppends<T, U, V>(readOnlyLeft: KeyPath<T, U>,
writableLeft: WritableKeyPath<T, U>,
referenceLeft: ReferenceWritableKeyPath<T, U>,
readOnlyRight: KeyPath<U, V>,
writableRight: WritableKeyPath<U, V>,
referenceRight: ReferenceWritableKeyPath<U, V>){
_ = readOnlyRight.appending(path: readOnlyLeft)
// expected-error@-1 {{cannot convert value of type 'KeyPath<T, U>' to expected argument type 'KeyPath<V, U>'}}
// expected-note@-2 {{arguments to generic parameter 'Root' ('T' and 'V') are expected to be equal}}
_ = readOnlyRight.appending(path: writableLeft)
// expected-error@-1 {{cannot convert value of type 'KeyPath<T, U>' to expected argument type 'KeyPath<V, U>'}}
// expected-note@-2 {{arguments to generic parameter 'Root' ('T' and 'V') are expected to be equal}}
_ = readOnlyRight.appending(path: referenceLeft)
// expected-error@-1 {{no exact matches in call to instance method 'appending'}}
_ = writableRight.appending(path: readOnlyLeft)
// expected-error@-1 {{instance method 'appending(path:)' requires that 'KeyPath<U, V>' inherit from 'KeyPath<U, T>'}}
_ = writableRight.appending(path: writableLeft)
// expected-error@-1 {{cannot convert value of type 'WritableKeyPath<T, U>' to expected argument type 'WritableKeyPath<V, U>'}}
// expected-note@-2 {{arguments to generic parameter 'Root' ('T' and 'V') are expected to be equal}}
_ = writableRight.appending(path: referenceLeft)
// expected-error@-1 {{no exact matches in call to instance method 'appending'}}
_ = referenceRight.appending(path: readOnlyLeft)
// expected-error@-1 {{instance method 'appending(path:)' requires that 'KeyPath<U, V>' inherit from 'KeyPath<U, T>'}}
_ = referenceRight.appending(path: writableLeft)
// expected-error@-1 {{cannot convert value of type 'WritableKeyPath<T, U>' to expected argument type 'WritableKeyPath<V, U>'}}
// expected-note@-2 {{arguments to generic parameter 'Root' ('T' and 'V') are expected to be equal}}
_ = referenceRight.appending(path: referenceLeft)
// expected-error@-1 {{cannot convert value of type 'WritableKeyPath<T, U>' to expected argument type 'WritableKeyPath<V, U>'}}
// expected-note@-2 {{arguments to generic parameter 'Root' ('T' and 'V') are expected to be equal}}
}
func partialAppends<T, U, V>(partial: PartialKeyPath<T>,
concrete: KeyPath<U, V>,
reference: ReferenceWritableKeyPath<U, V>,
any: AnyKeyPath) {
var a = any.appending(path: any)
expect(&a, is: Optional<AnyKeyPath>.self)
var b = any.appending(path: partial)
expect(&b, is: Optional<AnyKeyPath>.self)
var c = any.appending(path: concrete)
expect(&c, is: Optional<AnyKeyPath>.self)
var d = any.appending(path: reference)
expect(&d, is: Optional<AnyKeyPath>.self)
var e = partial.appending(path: any)
expect(&e, is: Optional<PartialKeyPath<T>>.self)
var f = partial.appending(path: partial)
expect(&f, is: Optional<PartialKeyPath<T>>.self)
var g = partial.appending(path: concrete)
expect(&g, is: Optional<KeyPath<T, V>>.self)
var h = partial.appending(path: reference)
expect(&h, is: Optional<ReferenceWritableKeyPath<T, V>>.self)
/* TODO
var i = concrete.appending(path: any)
expect(&i, is: Optional<PartialKeyPath<U>>.self)
var j = concrete.appending(path: partial)
expect(&j, is: Optional<PartialKeyPath<U>>.self)
var m = reference.appending(path: any)
expect(&m, is: Optional<PartialKeyPath<U>>.self)
var n = reference.appending(path: partial)
expect(&n, is: Optional<PartialKeyPath<U>>.self)
*/
}
|