Skip to content

Commit 55dfdea

Browse files
committed
[AST] InitAccessors: Switch from init accessor effects (initializes/accesses) to @storageRestrictions
(cherry picked from commit fe17491)
1 parent 96828e6 commit 55dfdea

File tree

8 files changed

+172
-88
lines changed

8 files changed

+172
-88
lines changed

lib/AST/Decl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9529,15 +9529,15 @@ void AccessorDecl::printUserFacingName(raw_ostream &out) const {
95299529

95309530
ArrayRef<VarDecl *> AccessorDecl::getInitializedProperties() const {
95319531
assert(isInitAccessor());
9532-
if (auto *initAttr = getAttrs().getAttribute<InitializesAttr>())
9533-
return initAttr->getPropertyDecls(const_cast<AccessorDecl *>(this));
9532+
if (auto *SR = getAttrs().getAttribute<StorageRestrictionsAttr>())
9533+
return SR->getInitializesProperties(const_cast<AccessorDecl *>(this));
95349534
return {};
95359535
}
95369536

95379537
ArrayRef<VarDecl *> AccessorDecl::getAccessedProperties() const {
95389538
assert(isInitAccessor());
9539-
if (auto *accessAttr = getAttrs().getAttribute<AccessesAttr>())
9540-
return accessAttr->getPropertyDecls(const_cast<AccessorDecl *>(this));
9539+
if (auto *SR = getAttrs().getAttribute<StorageRestrictionsAttr>())
9540+
return SR->getAccessesProperties(const_cast<AccessorDecl *>(this));
95419541
return {};
95429542
}
95439543

lib/Macros/Sources/ObservationMacros/ObservableMacro.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ public struct ObservationTrackedMacro: AccessorMacro {
312312

313313
let initAccessor: AccessorDeclSyntax =
314314
"""
315-
init(initialValue) initializes(_\(identifier)) {
315+
@storageRestrictions(initializes: _\(identifier))
316+
init(initialValue) {
316317
_\(identifier) = initialValue
317318
}
318319
"""

test/IDE/complete_decl_attribute.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ struct _S {
324324
// ON_MEMBER_LAST-DAG: Keyword/None: runtimeMetadata[#Declaration Attribute#]; name=runtimeMetadata
325325
// ON_MEMBER_LAST-DAG: Keyword/None: attached[#Declaration Attribute#]; name=attached
326326
// ON_MEMBER_LAST-DAG: Keyword/None: freestanding[#Declaration Attribute#]; name=freestanding
327+
// ON_MEMBER_LAST-DAG: Keyword/None: storageRestrictions[#Declaration Attribute#]; name=storageRestrictions
327328
// ON_MEMBER_LAST-NOT: Keyword
328329
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
329330
// ON_MEMBER_LAST-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyPropertyWrapper[#MyPropertyWrapper#]; name=MyPropertyWrapper
@@ -395,6 +396,7 @@ func dummy2() {}
395396
// KEYWORD_LAST-DAG: Keyword/None: preconcurrency[#Declaration Attribute#]; name=preconcurrency
396397
// KEYWORD_LAST-DAG: Keyword/None: runtimeMetadata[#Declaration Attribute#]; name=runtimeMetadata
397398
// KEYWORD_LAST-DAG: Keyword/None: attached[#Declaration Attribute#]; name=attached
399+
// KEYWORD_LAST-DAG: Keyword/None: storageRestrictions[#Declaration Attribute#]; name=storageRestrictions
398400
// KEYWORD_LAST-NOT: Keyword
399401
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
400402
// KEYWORD_LAST-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyGenericPropertyWrapper[#MyGenericPropertyWrapper#]; name=MyGenericPropertyWrapper

test/Interpreter/init_accessors.swift

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ struct TestInit {
1010
var full: (Int, Int)
1111

1212
var point: (Int, Int) {
13-
init(initialValue) initializes(y, full) accesses(x) {
13+
@storageRestrictions(initializes: y, full, accesses: x)
14+
init(initialValue) {
1415
self.y = initialValue.1
1516
self.full = (self.x, self.y)
1617
}
@@ -36,7 +37,8 @@ struct TestSetter {
3637
var y: Int
3738

3839
var point: (Int, Int) {
39-
init(initialValue) accesses(x, y) {
40+
@storageRestrictions(accesses: x, y)
41+
init(initialValue) {
4042
}
4143

4244
get { (x, y) }
@@ -61,7 +63,8 @@ struct TestInitThenSetter {
6163
var y: Int
6264

6365
var point: (Int, Int) {
64-
init(initialValue) initializes(x, y) {
66+
@storageRestrictions(initializes: x, y)
67+
init(initialValue) {
6568
self.x = initialValue.0
6669
self.y = initialValue.1
6770
}
@@ -94,7 +97,8 @@ struct TestPartialInt {
9497
var y: Int
9598

9699
var pointX: Int {
97-
init(newValue) initializes(x) {
100+
@storageRestrictions(initializes: x)
101+
init(newValue) {
98102
self.x = newValue
99103
}
100104

@@ -103,7 +107,8 @@ struct TestPartialInt {
103107
}
104108

105109
var pointY: Int {
106-
init(newValue) initializes(y) {
110+
@storageRestrictions(initializes: y)
111+
init(newValue) {
107112
self.y = newValue
108113
}
109114

@@ -135,15 +140,17 @@ struct TestNoInitAndInit {
135140
var y: Int
136141

137142
var pointX: Int {
138-
init(initalValue) accesses(x) {
143+
@storageRestrictions(accesses: x)
144+
init(initalValue) {
139145
}
140146

141147
get { x }
142148
set { }
143149
}
144150

145151
var pointY: Int {
146-
init(initialValue) initializes(y) {
152+
@storageRestrictions(initializes: y)
153+
init(initialValue) {
147154
self.y = initialValue
148155
}
149156

@@ -169,7 +176,8 @@ class TestClass {
169176
var y: (Int, [String])
170177

171178
var data: (Int, (Int, [String])) {
172-
init(initialValue) initializes(x, y) {
179+
@storageRestrictions(initializes: x, y)
180+
init(initialValue) {
173181
x = initialValue.0
174182
y = initialValue.1
175183
}
@@ -198,7 +206,8 @@ struct TestGeneric<T, U> {
198206
var c: U
199207

200208
var data: (T, T) {
201-
init(initialValue) initializes(a, b) accesses(c) {
209+
@storageRestrictions(initializes: a, b, accesses: c)
210+
init(initialValue) {
202211
a = initialValue.0
203212
b = initialValue.1
204213
print("TestGeneric(c: \(c))")
@@ -230,7 +239,8 @@ func test_local_with_memberwise() {
230239
var b: String
231240

232241
var pair: (Int, String) {
233-
init(initialValue) initializes(a, b) {
242+
@storageRestrictions(initializes: a, b)
243+
init(initialValue) {
234244
a = initialValue.0
235245
b = initialValue.1
236246
}
@@ -251,7 +261,8 @@ func test_local_with_memberwise() {
251261
var _c: C
252262

253263
var a: T {
254-
init(initialValue) initializes(_a) {
264+
@storageRestrictions(initializes: _a)
265+
init(initialValue) {
255266
_a = initialValue
256267
}
257268

@@ -260,7 +271,8 @@ func test_local_with_memberwise() {
260271
}
261272

262273
var pair: (String, C) {
263-
init(initialValue) initializes(_b, _c) accesses(_a) {
274+
@storageRestrictions(initializes: _b, _c, accesses: _a)
275+
init(initialValue) {
264276
_b = initialValue.0
265277
_c = initialValue.1
266278
_c.append(_a)
@@ -285,7 +297,8 @@ func test_assignments() {
285297
var _b: Int
286298

287299
var a: Int {
288-
init(initialValue) initializes(_a) {
300+
@storageRestrictions(initializes: _a)
301+
init(initialValue) {
289302
self._a = initialValue
290303
print("a-init-accessor: \(self._a)")
291304
}
@@ -294,7 +307,8 @@ func test_assignments() {
294307
}
295308

296309
var pair: (Int, Int) {
297-
init(initialValue) initializes(_a, _b) {
310+
@storageRestrictions(initializes: _a, _b)
311+
init(initialValue) {
298312
_a = initialValue.0
299313
_b = initialValue.1
300314
}
@@ -339,7 +353,8 @@ func test_memberwise_ordering() {
339353
var _b: Int
340354

341355
var a: Int {
342-
init(initialValue) initializes(_a) accesses(_b) {
356+
@storageRestrictions(initializes: _a, accesses: _b)
357+
init(initialValue) {
343358
_a = initialValue
344359
}
345360

@@ -355,7 +370,8 @@ func test_memberwise_ordering() {
355370
var _a: Int
356371

357372
var pair: (Int, Int) {
358-
init(initialValue) initializes(_a, _b) {
373+
@storageRestrictions(initializes: _a, _b)
374+
init(initialValue) {
359375
_a = initialValue.0
360376
_b = initialValue.1
361377
}
@@ -375,7 +391,8 @@ func test_memberwise_ordering() {
375391
var _b: Int
376392

377393
var pair: (Int, Int) {
378-
init(initialValue) accesses(_a, _b) {
394+
@storageRestrictions(accesses: _a, _b)
395+
init(initialValue) {
379396
}
380397

381398
get { (_a, _b) }
@@ -400,7 +417,8 @@ func test_memberwise_with_default_args() {
400417
var _b: Int
401418

402419
var pair: (Int, Int) = (-1, 42) {
403-
init(initialValue) initializes(_a, _b) {
420+
@storageRestrictions(initializes: _a, _b)
421+
init(initialValue) {
404422
_a = initialValue.0
405423
_b = initialValue.1
406424
}
@@ -421,7 +439,8 @@ func test_memberwise_with_default_args() {
421439
var _b: Int = 0
422440

423441
var pair: (Int, Int) = (1, 2) {
424-
init(initialValue) initializes(_a, _b) {
442+
@storageRestrictions(initializes: _a, _b)
443+
init(initialValue) {
425444
_a = initialValue.0
426445
_b = initialValue.1
427446
}
@@ -442,7 +461,8 @@ func test_memberwise_with_default_args() {
442461
var _a: Int = 1
443462

444463
var pair: (String, Int) = ("", 42) {
445-
init(initialValue) initializes(_q, _a) {
464+
@storageRestrictions(initializes: _q, _a)
465+
init(initialValue) {
446466
_q = initialValue.0
447467
_a = initialValue.1
448468
}
@@ -468,7 +488,8 @@ func test_init_accessors_without_setters() {
468488
var _x: T
469489

470490
var x: T {
471-
init(initialValue) initializes(_x) {
491+
@storageRestrictions(initializes: _x)
492+
init(initialValue) {
472493
_x = initialValue
473494
}
474495

@@ -487,7 +508,8 @@ func test_init_accessors_without_setters() {
487508
private var _v: T
488509

489510
var data: T {
490-
init(initialValue) initializes(_v) {
511+
@storageRestrictions(initializes: _v)
512+
init(initialValue) {
491513
_v = initialValue
492514
}
493515

test/SILOptimizer/init_accessor_definite_init_diagnostics.swift

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ struct Test1 {
88
var full: (Int, Int)
99

1010
var test1: (Int, Int) {
11-
init(initialValue) initializes(y, full) accesses(x) {
11+
@storageRestrictions(initializes: y, full, accesses: x)
12+
init(initialValue) {
1213
self.full = (self.x, self.y) // expected-error {{variable 'y' used before being initialized}}
1314
}
1415

@@ -17,7 +18,8 @@ struct Test1 {
1718
}
1819

1920
var pointY: Int {
20-
init(initialValue) initializes(y) {
21+
@storageRestrictions(initializes: y)
22+
init(initialValue) {
2123
self.y = initialValue // Ok
2224
}
2325

@@ -26,7 +28,8 @@ struct Test1 {
2628
}
2729

2830
var errorPoint1: (Int, Int) {
29-
init(initialValue) initializes(x, y) {
31+
@storageRestrictions(initializes: x, y)
32+
init(initialValue) {
3033
// expected-error@-1 {{property 'x' not initialized by init accessor}}
3134
// expected-error@-2 {{property 'y' not initialized by init accessor}}
3235
}
@@ -36,7 +39,8 @@ struct Test1 {
3639
}
3740

3841
var errorPoint2: (Int, Int) {
39-
init(initialValue) initializes(x, y) {
42+
@storageRestrictions(initializes: x, y)
43+
init(initialValue) {
4044
// expected-error@-1 {{property 'y' not initialized by init accessor}}
4145
self.x = initialValue.0
4246
}
@@ -46,7 +50,8 @@ struct Test1 {
4650
}
4751

4852
var errorPoint3: (Int, Int) {
49-
init(initialValue) initializes(x, y) {
53+
@storageRestrictions(initializes: x, y)
54+
init(initialValue) {
5055
self.y = initialValue.1
5156
print(y) // Ok
5257
print(x) // expected-error {{variable 'x' used before being initialized}}
@@ -68,7 +73,8 @@ struct TestPartial {
6873
var y: Int
6974

7075
var point: (Int, Int) {
71-
init(initialValue) initializes(x, y) {
76+
@storageRestrictions(initializes: x, y)
77+
init(initialValue) {
7278
self.x = initialValue.0
7379
self.y = initialValue.1
7480
}
@@ -93,7 +99,8 @@ struct TestDoubleInit1 {
9399
let x: Int // expected-note {{change 'let' to 'var' to make it mutable}}
94100

95101
var invalidPointX: Int {
96-
init(initialValue) initializes(x) {
102+
@storageRestrictions(initializes: x)
103+
init(initialValue) {
97104
self.x = initialValue
98105
self.x = 42 // expected-error {{immutable value 'x' may only be initialized once}}
99106
}
@@ -107,7 +114,8 @@ struct TestDoubleInit2 {
107114
let x: Int // expected-note {{change 'let' to 'var' to make it mutable}}
108115

109116
var pointX: Int {
110-
init(initialValue) initializes(x) {
117+
@storageRestrictions(initializes: x)
118+
init(initialValue) {
111119
self.x = initialValue
112120
}
113121

@@ -124,7 +132,8 @@ struct TestDoubleInit2 {
124132
struct TestAccessBeforeInit {
125133
var _x: Int
126134
var x: Int {
127-
init(initialValue) initializes(_x) accesses(y) {
135+
@storageRestrictions(initializes: _x, accesses: y)
136+
init(initialValue) {
128137
_x = initialValue
129138
}
130139

@@ -145,7 +154,8 @@ class TestInitWithGuard {
145154
var _b: Int
146155

147156
var pair1: (Int, Int) {
148-
init(initialValue) initializes(_a, _b) { // expected-error {{property '_b' not initialized by init accessor}}
157+
@storageRestrictions(initializes: _a, _b)
158+
init(initialValue) { // expected-error {{property '_b' not initialized by init accessor}}
149159
_a = initialValue.0
150160

151161
if _a > 0 {
@@ -160,7 +170,8 @@ class TestInitWithGuard {
160170
}
161171

162172
var pair2: (Int, Int) {
163-
init(initialValue) initializes(_a, _b) { // Ok
173+
@storageRestrictions(initializes: _a, _b)
174+
init(initialValue) { // Ok
164175
_a = initialValue.0
165176

166177
if _a > 0 {
@@ -185,7 +196,8 @@ do {
185196
private var _v: T
186197

187198
var data: T {
188-
init(initialValue) initializes(_v) {
199+
@storageRestrictions(initializes: _v)
200+
init(initialValue) {
189201
_v = initialValue
190202
}
191203

@@ -218,7 +230,8 @@ do {
218230
var _a: Int
219231

220232
var a: Int {
221-
init(initialValue) initializes(_a) {
233+
@storageRestrictions(initializes: _a)
234+
init(initialValue) {
222235
self._a = initialValue
223236
}
224237

0 commit comments

Comments
 (0)