1
1
// RUN: %target-typecheck-verify-swift -parse-as-library -module-name MyModule
2
2
3
- struct Value {
4
- static let defaultValue = Value ( a: Nested ( b: 1 ) )
5
-
6
- struct Nested {
7
- var b : Int
8
-
9
- @discardableResult
10
- mutating func setToZero( ) -> Int {
11
- let prev = b
12
- b = 0
13
- return prev
14
- }
3
+ struct Nested {
4
+ var b : Int
5
+
6
+ @discardableResult
7
+ mutating func setToZero( ) -> Int {
8
+ let prev = b
9
+ b = 0
10
+ return prev
15
11
}
12
+ }
13
+
14
+ protocol ValueProto {
15
+ static var defaultValue : Self { get }
16
+
17
+ var a : Nested { get set }
18
+
19
+ subscript( _ i: Int ) -> Nested { get set }
20
+
21
+ @discardableResult mutating func setToZero( ) -> Int
22
+ }
23
+
24
+ struct StructValue : ValueProto {
25
+ static var defaultValue : Self { . init( a: Nested ( b: 1 ) ) }
16
26
17
27
var a : Nested
18
28
@@ -29,22 +39,22 @@ struct Value {
29
39
}
30
40
}
31
41
32
- struct BaseStruct {
33
- var available : Value = . defaultValue
42
+ struct BaseStruct < T : ValueProto > {
43
+ var available : T = . defaultValue
34
44
35
- var unavailableGetter : Value {
45
+ var unavailableGetter : T {
36
46
@available ( * , unavailable)
37
47
get { fatalError ( ) } // expected-note 28 {{getter for 'unavailableGetter' has been explicitly marked unavailable here}}
38
48
set { }
39
49
}
40
50
41
- var unavailableSetter : Value {
51
+ var unavailableSetter : T {
42
52
get { . defaultValue }
43
53
@available ( * , unavailable)
44
54
set { fatalError ( ) } // expected-note 21 {{setter for 'unavailableSetter' has been explicitly marked unavailable here}}
45
55
}
46
56
47
- var unavailableGetterAndSetter : Value {
57
+ var unavailableGetterAndSetter : T {
48
58
@available ( * , unavailable)
49
59
get { fatalError ( ) } // expected-note 28 {{getter for 'unavailableGetterAndSetter' has been explicitly marked unavailable here}}
50
60
@available ( * , unavailable)
@@ -56,10 +66,8 @@ func takesIntInOut(_ i: inout Int) -> Int {
56
66
return 0
57
67
}
58
68
59
- let someValue = Value . defaultValue
60
-
61
69
func testRValueLoads( ) {
62
- var x = BaseStruct ( ) // expected-warning {{variable 'x' was never mutated; consider changing to 'let' constant}}
70
+ var x = BaseStruct < StructValue > ( ) // expected-warning {{variable 'x' was never mutated; consider changing to 'let' constant}}
63
71
64
72
_ = x. available
65
73
_ = x. available. a
@@ -82,8 +90,8 @@ func testRValueLoads() {
82
90
_ = x. unavailableGetterAndSetter [ 0 ] . b // expected-error {{getter for 'unavailableGetterAndSetter' is unavailable}}
83
91
}
84
92
85
- func testLValueAssignments( ) {
86
- var x = BaseStruct ( )
93
+ func testLValueAssignments( _ someValue : StructValue ) {
94
+ var x = BaseStruct < StructValue > ( )
87
95
88
96
x. available = someValue
89
97
x. available. a = someValue. a
@@ -108,7 +116,7 @@ func testLValueAssignments() {
108
116
109
117
func testKeyPathLoads( ) {
110
118
let a = [ 0 ]
111
- var x = BaseStruct ( )
119
+ var x = BaseStruct < StructValue > ( )
112
120
113
121
_ = x [ keyPath: \. available]
114
122
_ = x [ keyPath: \. available. a]
@@ -139,9 +147,9 @@ func testKeyPathLoads() {
139
147
_ = a [ keyPath: \. [ takesIntInOut ( & x. unavailableGetterAndSetter [ 0 ] . b) ] ] // expected-error {{getter for 'unavailableGetterAndSetter' is unavailable}} expected-error {{setter for 'unavailableGetterAndSetter' is unavailable}}
140
148
}
141
149
142
- func testKeyPathAssignments( ) {
150
+ func testKeyPathAssignments( _ someValue : StructValue ) {
143
151
var a = [ 0 ]
144
- var x = BaseStruct ( )
152
+ var x = BaseStruct < StructValue > ( )
145
153
146
154
x [ keyPath: \. available] = someValue
147
155
x [ keyPath: \. available. a] = someValue. a
@@ -174,7 +182,7 @@ func testKeyPathAssignments() {
174
182
}
175
183
176
184
func testMutatingMember( ) {
177
- var x = BaseStruct ( )
185
+ var x = BaseStruct < StructValue > ( )
178
186
let a = [ 0 ]
179
187
180
188
x. available. setToZero ( )
@@ -205,7 +213,7 @@ func testMutatingMember() {
205
213
func testPassAsInOutParameter( ) {
206
214
func takesInOut< T> ( _ t: inout T ) { }
207
215
208
- var x = BaseStruct ( )
216
+ var x = BaseStruct < StructValue > ( )
209
217
210
218
takesInOut ( & x. available)
211
219
takesInOut ( & x. available. a)
@@ -228,7 +236,7 @@ func testPassAsInOutParameter() {
228
236
takesInOut ( & x. unavailableGetterAndSetter [ 0 ] . b) // expected-error {{getter for 'unavailableGetterAndSetter' is unavailable}} expected-error {{setter for 'unavailableGetterAndSetter' is unavailable}}
229
237
}
230
238
231
- var global = BaseStruct ( )
239
+ var global = BaseStruct < StructValue > ( )
232
240
233
241
struct TestPatternBindingInitExprs {
234
242
var available = global. available
0 commit comments