Skip to content

Commit db2b9f0

Browse files
committed
[Tests] NFC: Update and add a few test-cases related to noncopyable types in key path context
1 parent 6c2b125 commit db2b9f0

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

test/Constraints/keypath.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,24 @@ extension Collection {
318318
func keypathToFunctionWithOptional() {
319319
_ = Array("").prefix(1...4, while: \.isNumber) // Ok
320320
}
321+
322+
func test_new_key_path_type_requirements() {
323+
struct V: ~Copyable {
324+
}
325+
326+
struct S: ~Copyable {
327+
var x: Int
328+
var v: V
329+
}
330+
331+
_ = \S.x // expected-error {{key path cannot refer to noncopyable type 'S'}}
332+
_ = \S.v
333+
// expected-error@-1 {{key path cannot refer to noncopyable type 'S'}}
334+
// expected-error@-2 {{key path cannot refer to noncopyable type 'V'}}
335+
336+
func test<R>(_: KeyPath<R, Int>) {} // expected-note {{'where R: Copyable' is implicit here}}
337+
338+
test(\S.x)
339+
// expected-error@-1 {{key path cannot refer to noncopyable type 'S'}}
340+
// expected-error@-2 {{local function 'test' requires that 'S' conform to 'Copyable'}}
341+
}

test/Sema/keypaths_noncopyable.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,18 @@ struct C {
3939

4040
// rdar://109287447
4141
public func testKeypath<V: ~Copyable>(m: consuming M<V>) {
42-
_ = m[keyPath: \.nc] // expected-error {{key path cannot refer to noncopyable type 'NC'}}
43-
_ = m[keyPath: \.nc.data] // expected-error {{key path cannot refer to noncopyable type 'NC'}}
44-
_ = m[keyPath: \.ncg] // expected-error {{key path cannot refer to noncopyable type 'V'}}
45-
_ = m[keyPath: \.ncg.protocolProp] // expected-error {{key path cannot refer to noncopyable type 'V'}}
42+
_ = m[keyPath: \.nc]
43+
// expected-error@-1 {{key path cannot refer to noncopyable type 'M<V>'}}
44+
// expected-error@-2 {{key path cannot refer to noncopyable type 'NC'}}
45+
_ = m[keyPath: \.nc.data]
46+
// expected-error@-1 {{key path cannot refer to noncopyable type 'M<V>'}}
47+
_ = m[keyPath: \.ncg]
48+
// expected-error@-1 {{key path cannot refer to noncopyable type 'M<V>'}}
49+
// expected-error@-2 {{key path cannot refer to noncopyable type 'V'}}
50+
_ = m[keyPath: \.ncg.protocolProp]
51+
// expected-error@-1 {{key path cannot refer to noncopyable type 'M<V>'}}
4652
_ = m[keyPath: \.string]
53+
// expected-error@-1 {{key path cannot refer to noncopyable type 'M<V>'}}
4754

4855
let b = Box(NC())
4956
_ = b.with(\.data)

test/expr/unary/keypath/keypath.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,9 @@ func testKeyPathHole() {
945945

946946
func f(_ i: Int) {}
947947
f(\.x) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}} {{6-6=<#Root#>}}
948+
// expected-error@-1 {{cannot convert value of type 'KeyPath<Root, Value>' to expected argument type 'Int'}}
948949
f(\.x.y) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}} {{6-6=<#Root#>}}
950+
// expected-error@-1 {{cannot convert value of type 'KeyPath<Root, Value>' to expected argument type 'Int'}}
949951

950952
func provideValueButNotRoot<T>(_ fn: (T) -> String) {} // expected-note 2 {{in call to function 'provideValueButNotRoot'}}
951953
provideValueButNotRoot(\.x) // expected-error {{cannot infer key path type from context; consider explicitly specifying a root type}}

0 commit comments

Comments
 (0)