Skip to content

Commit d5f0fff

Browse files
author
Amritpan Kaur
committed
[Tests] Add and update tests
1 parent 3140c12 commit d5f0fff

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

test/Constraints/keypath.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ func test_mismatch_with_contextual_optional_result() {
136136
var arr: [Int] = []
137137
}
138138

139-
let _ = A(B(), keyPath: \.arr)
140-
// expected-error@-1 {{key path value type '[Int]' cannot be converted to contextual type '[Int]?'}}
139+
let _ = A(B(), keyPath: \.arr) // expected-error {{cannot convert value of type 'KeyPath<B, [Int]>' to expected argument type 'KeyPath<B, [Int]?>'}}
140+
// expected-note@-1 {{arguments to generic parameter 'Value' ('[Int]' and '[Int]?') are expected to be equal}}
141141
}
142142

143143
// https://github.com/apple/swift/issues/53581
@@ -180,6 +180,20 @@ func key_path_root_mismatch<T>(_ base: KeyPathBase?, subBase: KeyPathBaseSubtype
180180

181181
}
182182

183+
func key_path_value_mismatch() {
184+
struct S {
185+
var member: Int
186+
}
187+
188+
func test(_: KeyPath<S, String>) {}
189+
// expected-note@-1 {{found candidate with type 'KeyPath<S, Int>'}}
190+
func test(_: KeyPath<S, Float>) {}
191+
// expected-note@-1 {{found candidate with type 'KeyPath<S, Int>'}}
192+
193+
test(\.member)
194+
// expected-error@-1 {{no exact matches in call to local function 'test'}}
195+
}
196+
183197
// https://github.com/apple/swift/issues/55884
184198
func f_55884() {
185199
func f<T>(_ x: KeyPath<String?, T>) -> T { "1"[keyPath: x] }

test/Sema/keypath_subscript_nolabel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ struct S4 {
2828
subscript(v: KeyPath<S4, String>) -> Int { get { 0 } set(newValue) {} }
2929
}
3030
var s4 = S4()
31-
s4[\.x] = 10 // expected-error {{key path value type 'Int' cannot be converted to contextual type 'String'}}
31+
s4[\.x] = 10 // expected-error {{cannot convert value of type 'KeyPath<S4, Int>' to expected argument type 'KeyPath<S4, String>'}}
32+
// expected-note@-1 {{arguments to generic parameter 'Value' ('Int' and 'String') are expected to be equal}}

test/expr/unary/keypath/keypath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func testNoComponents() {
228228
let _: KeyPath<A, A> = \A // expected-error{{must have at least one component}}
229229
let _: KeyPath<C, A> = \C // expected-error{{must have at least one component}}
230230
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
231-
let _: KeyPath<A, C> = \A // expected-error{{must have at least one component}}
231+
let _: KeyPath<A, C> = \A // expected-error{{must have at least one component}}
232232
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
233233
_ = \A // expected-error {{key path must have at least one component}}
234234
}

0 commit comments

Comments
 (0)