@@ -126,31 +126,202 @@ struct WrapperContext {
126
126
}
127
127
128
128
// Class-constrained extension where protocol does not impose class requirement
129
+ // SR-11298
129
130
130
131
protocol DoesNotImposeClassReq_1 { }
131
-
132
+
132
133
class JustAClass : DoesNotImposeClassReq_1 {
133
134
var property : String = " "
134
135
}
135
136
136
137
extension DoesNotImposeClassReq_1 where Self: JustAClass {
137
- var wrappingProperty : String {
138
+ var wrappingProperty1 : String {
139
+ get { return property }
140
+ set { property = newValue } // Okay
141
+ }
142
+
143
+ var wrappingProperty2 : String {
138
144
get { return property }
139
- set { property = newValue }
145
+ nonmutating set { property = newValue } // Okay
146
+ }
147
+
148
+ var wrappingProperty3 : String {
149
+ get { return property }
150
+ mutating set { property = newValue } // Okay
151
+ }
152
+
153
+ mutating func foo( ) {
154
+ property = " " // Okay
155
+ wrappingProperty1 = " " // Okay
156
+ wrappingProperty2 = " " // Okay
157
+ wrappingProperty3 = " " // Okay
158
+ }
159
+
160
+ func bar( ) { // expected-note {{mark method 'mutating' to make 'self' mutable}}{{3-3=mutating }}
161
+ property = " " // Okay
162
+ wrappingProperty1 = " " // Okay
163
+ wrappingProperty2 = " " // Okay
164
+ wrappingProperty3 = " " // expected-error {{cannot assign to property: 'self' is immutable}}
165
+ }
166
+
167
+ nonmutating func baz( ) { // expected-note {{mark method 'mutating' to make 'self' mutable}}{{3-14=mutating}}
168
+ property = " " // Okay
169
+ wrappingProperty1 = " " // Okay
170
+ wrappingProperty2 = " " // Okay
171
+ wrappingProperty3 = " " // expected-error {{cannot assign to property: 'self' is immutable}}
140
172
}
141
173
}
142
-
143
- let instanceOfJustAClass = JustAClass ( ) // expected-note {{change 'let' to 'var' to make it mutable}}
144
- instanceOfJustAClass. wrappingProperty = " " // expected-error {{cannot assign to property: 'instanceOfJustAClass' is a 'let' constant}}
174
+
175
+ let instanceOfJustAClass1 = JustAClass ( ) // expected-note 2{{change 'let' to 'var' to make it mutable}}
176
+ instanceOfJustAClass1. wrappingProperty1 = " " // Okay
177
+ instanceOfJustAClass1. wrappingProperty2 = " " // Okay
178
+ instanceOfJustAClass1. wrappingProperty3 = " " // expected-error {{cannot assign to property: 'instanceOfJustAClass1' is a 'let' constant}}
179
+ instanceOfJustAClass1. foo ( ) // expected-error {{cannot use mutating member on immutable value: 'instanceOfJustAClass1' is a 'let' constant}}
180
+ instanceOfJustAClass1. bar ( ) // Okay
181
+ instanceOfJustAClass1. baz ( ) // Okay
182
+
183
+ var instanceOfJustAClass2 = JustAClass ( )
184
+ instanceOfJustAClass2. foo ( ) // Okay
145
185
146
186
protocol DoesNotImposeClassReq_2 {
147
187
var property : String { get set }
148
188
}
149
189
150
190
extension DoesNotImposeClassReq_2 where Self : AnyObject {
151
- var wrappingProperty : String {
191
+ var wrappingProperty1 : String {
152
192
get { property }
153
- set { property = newValue } // Okay
193
+ set { property = newValue } // expected-error {{cannot assign to property: 'self' is immutable}}
194
+ // expected-note@-1 {{mark accessor 'mutating' to make 'self' mutable}}{{5-5=mutating }}
195
+ }
196
+
197
+ var wrappingProperty2 : String {
198
+ get { property }
199
+ nonmutating set { property = newValue } // expected-error {{cannot assign to property: 'self' is immutable}}
200
+ // expected-note@-1 {{mark accessor 'mutating' to make 'self' mutable}}{{5-16=mutating}}
201
+ }
202
+
203
+ var wrappingProperty3 : String {
204
+ get { property }
205
+ mutating set { property = newValue } // Okay
206
+ }
207
+
208
+ mutating func foo( ) {
209
+ property = " " // Okay
210
+ wrappingProperty1 = " " // Okay (the error is on the setter declaration above)
211
+ wrappingProperty2 = " " // Okay (the error is on the setter declaration above)
212
+ wrappingProperty3 = " " // Okay
213
+ }
214
+
215
+ func bar( ) { // expected-note 2{{mark method 'mutating' to make 'self' mutable}}{{3-3=mutating }}
216
+ property = " " // expected-error {{cannot assign to property: 'self' is immutable}}
217
+ wrappingProperty1 = " " // Okay (the error is on the setter declaration above)
218
+ wrappingProperty2 = " " // Okay (the error is on the setter declaration above)
219
+ wrappingProperty3 = " " // expected-error {{cannot assign to property: 'self' is immutable}}
220
+ }
221
+
222
+ nonmutating func baz( ) { // expected-note 2{{mark method 'mutating' to make 'self' mutable}}{{3-14=mutating}}
223
+ property = " " // expected-error {{cannot assign to property: 'self' is immutable}}
224
+ wrappingProperty1 = " " // Okay (the error is on the setter declaration above)
225
+ wrappingProperty2 = " " // Okay (the error is on the setter declaration above)
226
+ wrappingProperty3 = " " // expected-error {{cannot assign to property: 'self' is immutable}}
227
+ }
228
+ }
229
+
230
+ protocol DoesNotImposeClassReq_3 {
231
+ var someProperty : Int { get set }
232
+ }
233
+
234
+ class JustAClass1 : DoesNotImposeClassReq_3 {
235
+ var someProperty = 0
236
+ }
237
+
238
+ extension DoesNotImposeClassReq_3 where Self: JustAClass1 {
239
+ var anotherProperty1 : Int {
240
+ get { return someProperty }
241
+ set { someProperty = newValue } // Okay
242
+ }
243
+
244
+ var anotherProperty2 : Int {
245
+ get { return someProperty }
246
+ mutating set { someProperty = newValue } // Okay
247
+ }
248
+ }
249
+
250
+ let justAClass1 = JustAClass1 ( ) // expected-note {{change 'let' to 'var' to make it mutable}}
251
+ justAClass1. anotherProperty1 = 1234 // Okay
252
+ justAClass1. anotherProperty2 = 4321 // expected-error {{cannot assign to property: 'justAClass1' is a 'let' constant}}
253
+
254
+ protocol ImposeClassReq1 : AnyObject {
255
+ var someProperty : Int { get set }
256
+ }
257
+
258
+ class JustAClass2 : ImposeClassReq1 {
259
+ var someProperty = 0
260
+ }
261
+
262
+ extension ImposeClassReq1 where Self: AnyObject {
263
+ var wrappingProperty1 : Int {
264
+ get { return someProperty }
265
+ set { someProperty = newValue }
266
+ }
267
+
268
+ var wrappingProperty2 : Int {
269
+ get { return someProperty }
270
+ mutating set { someProperty = newValue } // expected-error {{'mutating' isn't valid on methods in classes or class-bound protocols}}
271
+ }
272
+
273
+ mutating func foo( ) { // expected-error {{mutating' isn't valid on methods in classes or class-bound protocols}}
274
+ someProperty = 1
275
+ }
276
+
277
+ nonmutating func bar( ) { // expected-error {{'nonmutating' isn't valid on methods in classes or class-bound protocols}}
278
+ someProperty = 2
279
+ }
280
+
281
+ func baz( ) { // Okay
282
+ someProperty = 3
283
+ }
284
+ }
285
+
286
+ extension ImposeClassReq1 {
287
+ var wrappingProperty3 : Int {
288
+ get { return someProperty }
289
+ set { someProperty = newValue }
290
+ }
291
+ }
292
+
293
+ let justAClass2 = JustAClass2 ( ) // expected-note {{change 'let' to 'var' to make it mutable}}
294
+ justAClass2. wrappingProperty1 = 9876 // Okay
295
+ justAClass2. wrappingProperty3 = 0987 // Okay
296
+ justAClass2. foo ( ) // expected-error {{cannot use mutating member on immutable value: 'justAClass2' is a 'let' constant}}
297
+ justAClass2. bar ( ) // Okay as well (complains about explicit nonmutating on decl)
298
+ justAClass2. baz ( ) // Okay
299
+
300
+ protocol ImposeClassReq2 : AnyObject {
301
+ var someProperty : Int { get set }
302
+ }
303
+
304
+ extension ImposeClassReq2 {
305
+ var wrappingProperty1 : Int {
306
+ get { return someProperty }
307
+ set { someProperty = newValue }
308
+ }
309
+
310
+ var wrappingProperty2 : Int {
311
+ get { return someProperty }
312
+ mutating set { someProperty = newValue } // expected-error {{'mutating' isn't valid on methods in classes or class-bound protocols}}
313
+ }
314
+
315
+ mutating func foo( ) { // expected-error {{mutating' isn't valid on methods in classes or class-bound protocols}}
316
+ someProperty = 1
317
+ }
318
+
319
+ nonmutating func bar( ) { // expected-error {{'nonmutating' isn't valid on methods in classes or class-bound protocols}}
320
+ someProperty = 2
321
+ }
322
+
323
+ func baz( ) { // Okay
324
+ someProperty = 3
154
325
}
155
326
}
156
327
0 commit comments