1+ // RUN: %target-typecheck-verify-swift -enable-experimental-concurrency
2+ // REQUIRES: concurrency
3+
4+ class Box {
5+ let size : Int = 0
6+ }
7+
8+ actor Door {
9+ let immutable : Int = 0
10+ let letBox : Box ? = nil
11+ let letDict : [ Int : Box ] = [ : ]
12+ let immutableNeighbor : Door ? = nil
13+
14+
15+ var mutableNeighbor : Door ? = nil
16+ var varDict : [ Int : Box ] = [ : ]
17+ var mutable : Int = 0
18+ var varBox : Box = Box ( )
19+ var getOnlyInt : Int {
20+ get { 0 }
21+ }
22+
23+ @actorIndependent ( unsafe) var unsafeIndependent : Int = 0
24+
25+ @MainActor var globActor_mutable : Int = 0
26+ @MainActor let globActor_immutable : Int = 0
27+
28+ @MainActor ( unsafe) var unsafeGlobActor_mutable : Int = 0
29+ @MainActor ( unsafe) let unsafeGlobActor_immutable : Int = 0
30+
31+ subscript( byIndex: Int ) -> Int { 0 }
32+
33+ @MainActor subscript( byName: String ) -> Int { 0 }
34+
35+ @actorIndependent subscript( byIEEE754: Double ) -> Int { 0 }
36+ }
37+
38+ func attemptAccess< T, V> ( _ t : T , _ f : ( T ) -> V ) -> V {
39+ return f ( t)
40+ }
41+
42+ func tryKeyPathsMisc( d : Door ) {
43+ // as a func
44+ _ = attemptAccess ( d, \Door . mutable) // expected-error {{cannot form key path to actor-isolated property 'mutable'}}
45+ _ = attemptAccess ( d, \Door . immutable)
46+ _ = attemptAccess ( d, \Door . immutableNeighbor? . immutableNeighbor)
47+
48+ // in combination with other key paths
49+
50+ _ = ( \Door . letBox) . appending ( path: // expected-warning {{cannot form key path that accesses non-concurrent-value type 'Box?'}}
51+ \Box ? .?. size)
52+
53+ _ = ( \Door . varBox) . appending ( path: // expected-error {{cannot form key path to actor-isolated property 'varBox'}}
54+ \Box . size)
55+
56+ }
57+
58+ func tryKeyPathsFromAsync( ) async {
59+ _ = \Door . unsafeGlobActor_immutable
60+ _ = \Door . unsafeGlobActor_mutable // expected-error{{cannot form key path to actor-isolated property 'unsafeGlobActor_mutable'}}
61+ }
62+
63+ func tryNonConcurrentValue( ) {
64+ _ = \Door . letDict [ 0 ] // expected-warning {{cannot form key path that accesses non-concurrent-value type '[Int : Box]'}}
65+ _ = \Door . varDict [ 0 ] // expected-error {{cannot form key path to actor-isolated property 'varDict'}}
66+ _ = \Door . letBox!. size // expected-warning {{cannot form key path that accesses non-concurrent-value type 'Box?'}}
67+ }
68+
69+ func tryKeypaths( ) {
70+ _ = \Door . unsafeGlobActor_immutable
71+ _ = \Door . unsafeGlobActor_mutable
72+
73+ _ = \Door . immutable
74+ _ = \Door . unsafeIndependent
75+ _ = \Door . globActor_immutable
76+ _ = \Door . [ 4.2 ]
77+ _ = \Door . immutableNeighbor? . immutableNeighbor? . immutableNeighbor
78+
79+ _ = \Door . varBox // expected-error{{cannot form key path to actor-isolated property 'varBox'}}
80+ _ = \Door . mutable // expected-error{{cannot form key path to actor-isolated property 'mutable'}}
81+ _ = \Door . getOnlyInt // expected-error{{cannot form key path to actor-isolated property 'getOnlyInt'}}
82+ _ = \Door . mutableNeighbor? . mutableNeighbor? . mutableNeighbor // expected-error 3 {{cannot form key path to actor-isolated property 'mutableNeighbor'}}
83+
84+ let _ : PartialKeyPath < Door > = \. mutable // expected-error{{cannot form key path to actor-isolated property 'mutable'}}
85+ let _ : AnyKeyPath = \Door . mutable // expected-error{{cannot form key path to actor-isolated property 'mutable'}}
86+
87+ _ = \Door . globActor_mutable // expected-error{{cannot form key path to actor-isolated property 'globActor_mutable'}}
88+ _ = \Door . [ 0 ] // expected-error{{cannot form key path to actor-isolated subscript 'subscript(_:)'}}
89+ _ = \Door . [ " hello " ] // expected-error{{cannot form key path to actor-isolated subscript 'subscript(_:)'}}
90+ }
0 commit comments