[Tests] Add and update tests

This commit is contained in:
Amritpan Kaur
2023-07-24 12:39:58 -07:00
parent 3140c12691
commit d5f0fff83c
3 changed files with 19 additions and 4 deletions

View File

@@ -136,8 +136,8 @@ func test_mismatch_with_contextual_optional_result() {
var arr: [Int] = []
}
let _ = A(B(), keyPath: \.arr)
// expected-error@-1 {{key path value type '[Int]' cannot be converted to contextual type '[Int]?'}}
let _ = A(B(), keyPath: \.arr) // expected-error {{cannot convert value of type 'KeyPath<B, [Int]>' to expected argument type 'KeyPath<B, [Int]?>'}}
// expected-note@-1 {{arguments to generic parameter 'Value' ('[Int]' and '[Int]?') are expected to be equal}}
}
// https://github.com/apple/swift/issues/53581
@@ -180,6 +180,20 @@ func key_path_root_mismatch<T>(_ base: KeyPathBase?, subBase: KeyPathBaseSubtype
}
func key_path_value_mismatch() {
struct S {
var member: Int
}
func test(_: KeyPath<S, String>) {}
// expected-note@-1 {{found candidate with type 'KeyPath<S, Int>'}}
func test(_: KeyPath<S, Float>) {}
// expected-note@-1 {{found candidate with type 'KeyPath<S, Int>'}}
test(\.member)
// expected-error@-1 {{no exact matches in call to local function 'test'}}
}
// https://github.com/apple/swift/issues/55884
func f_55884() {
func f<T>(_ x: KeyPath<String?, T>) -> T { "1"[keyPath: x] }

View File

@@ -28,4 +28,5 @@ struct S4 {
subscript(v: KeyPath<S4, String>) -> Int { get { 0 } set(newValue) {} }
}
var s4 = S4()
s4[\.x] = 10 // expected-error {{key path value type 'Int' cannot be converted to contextual type 'String'}}
s4[\.x] = 10 // expected-error {{cannot convert value of type 'KeyPath<S4, Int>' to expected argument type 'KeyPath<S4, String>'}}
// expected-note@-1 {{arguments to generic parameter 'Value' ('Int' and 'String') are expected to be equal}}

View File

@@ -228,7 +228,7 @@ func testNoComponents() {
let _: KeyPath<A, A> = \A // expected-error{{must have at least one component}}
let _: KeyPath<C, A> = \C // expected-error{{must have at least one component}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
let _: KeyPath<A, C> = \A // expected-error{{must have at least one component}}
let _: KeyPath<A, C> = \A // expected-error{{must have at least one component}}
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
_ = \A // expected-error {{key path must have at least one component}}
}