Skip to content

Commit a767c7c

Browse files
committed
[Parser] InitAccessors: Properties in initializes/accesses should be separated by comma
1 parent cdd4a37 commit a767c7c

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7204,9 +7204,10 @@ static ParserStatus parseInitAccessorEffect(Parser &P,
72047204
// Consume '('
72057205
P.consumeToken();
72067206

7207+
bool hasNextProperty = false;
72077208
// Consume the identifier list
72087209
SmallVector<Identifier, 4> properties;
7209-
while (!P.Tok.is(tok::r_paren)) {
7210+
do {
72107211
Identifier propertyName;
72117212
SourceLoc propertyNameLoc;
72127213
if (P.parseIdentifier(propertyName, propertyNameLoc,
@@ -7217,7 +7218,10 @@ static ParserStatus parseInitAccessorEffect(Parser &P,
72177218
}
72187219

72197220
properties.push_back(propertyName);
7220-
}
7221+
7222+
// Parse the comma, if the list continues.
7223+
hasNextProperty = P.consumeIf(tok::comma);
7224+
} while (hasNextProperty);
72217225

72227226
if (!P.Tok.is(tok::r_paren)) {
72237227
P.diagnose(P.Tok.getLoc(), diag::attr_expected_rparen,

test/SIL/init_accessors.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct TestInit {
2727
// CHECK-NEXT: [[FULL_ELT_1:%.*]] = tuple_element_addr [[FULL_ACCESS]] : $*(Int, Int), 1
2828
// CHECK-NEXT: store [[Y_VAL]] to [[FULL_ELT_1]] : $*Int
2929
// CHECK-NEXT: end_access [[FULL_ACCESS]] : $*(Int, Int)
30-
init(initialValue) initializes(y full) accesses(x) {
30+
init(initialValue) initializes(y, full) accesses(x) {
3131
self.y = initialValue.1
3232
self.full = (self.x, self.y)
3333
}
@@ -56,7 +56,7 @@ struct TestSetter {
5656
var y: Int
5757

5858
var point: (Int, Int) {
59-
init(initialValue) accesses(x y) {
59+
init(initialValue) accesses(x, y) {
6060
}
6161

6262
get { (x, y) }
@@ -79,7 +79,7 @@ struct TestInitThenSetter {
7979
var y: Int
8080

8181
var point: (Int, Int) {
82-
init(initialValue) initializes(x y) {
82+
init(initialValue) initializes(x, y) {
8383
self.x = initialValue.0
8484
self.y = initialValue.1
8585
}
@@ -230,7 +230,7 @@ class TestClass {
230230
// CHECK-NEXT: [[Y_ELT_1:%.*]] = tuple_element_addr [[Y_ACCESS]] : $*(Int, Array<String>), 1
231231
// CHECK-NEXT: store [[Y_VAL_1]] to [[Y_ELT_1]] : $*Array<String>
232232
// CHECK-NEXT: end_access [[Y_ACCESS]] : $*(Int, Array<String>)
233-
init(initialValue) initializes(x y) {
233+
init(initialValue) initializes(x, y) {
234234
x = initialValue.0
235235
y = initialValue.1
236236
}
@@ -278,7 +278,7 @@ struct TestGeneric<T, U> {
278278
// CHECK-NEXT: copy_addr [[C_ACCESS]] to [init] [[C_AS_ANY]] : $*U
279279
// CHECK-NEXT: end_access [[C_ACCESS]] : $*U
280280
var data: (T, T) {
281-
init(initialValue) initializes(a b) accesses(c) {
281+
init(initialValue) initializes(a, b) accesses(c) {
282282
a = initialValue.0
283283
b = initialValue.1
284284
print(c)
@@ -312,7 +312,7 @@ func test_local_with_memberwise() {
312312
var b: String
313313

314314
var pair: (Int, String) {
315-
init(initialValue) initializes(a b) {
315+
init(initialValue) initializes(a, b) {
316316
a = initialValue.0
317317
b = initialValue.1
318318
}
@@ -353,7 +353,7 @@ func test_local_with_memberwise() {
353353
}
354354

355355
var pair: (String, C) {
356-
init(initialValue) initializes(_b _c) accesses(_a) {
356+
init(initialValue) initializes(_b, _c) accesses(_a) {
357357
_b = initialValue.0
358358
_c = initialValue.1
359359
_c.append(_a)

test/SILOptimizer/init_accessor_definite_init_diagnostics.swift

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

1010
var test1: (Int, Int) {
11-
init(initialValue) initializes(y full) accesses(x) {
11+
init(initialValue) initializes(y, full) accesses(x) {
1212
self.full = (self.x, self.y) // expected-error {{variable 'y' used before being initialized}}
1313
}
1414

@@ -26,7 +26,7 @@ struct Test1 {
2626
}
2727

2828
var errorPoint1: (Int, Int) {
29-
init(initialValue) initializes(x y) {
29+
init(initialValue) initializes(x, y) {
3030
// expected-error@-1 {{property 'x' not initialized by init accessor}}
3131
// expected-error@-2 {{property 'y' not initialized by init accessor}}
3232
}
@@ -36,7 +36,7 @@ struct Test1 {
3636
}
3737

3838
var errorPoint2: (Int, Int) {
39-
init(initialValue) initializes(x y) {
39+
init(initialValue) initializes(x, y) {
4040
// expected-error@-1 {{property 'y' not initialized by init accessor}}
4141
self.x = initialValue.0
4242
}
@@ -46,7 +46,7 @@ struct Test1 {
4646
}
4747

4848
var errorPoint3: (Int, Int) {
49-
init(initialValue) initializes(x y) {
49+
init(initialValue) initializes(x, y) {
5050
self.y = initialValue.1
5151
print(y) // Ok
5252
print(x) // expected-error {{variable 'x' used before being initialized}}
@@ -68,7 +68,7 @@ struct TestPartial {
6868
var y: Int
6969

7070
var point: (Int, Int) {
71-
init(initialValue) initializes(x y) {
71+
init(initialValue) initializes(x, y) {
7272
self.x = initialValue.0
7373
self.y = initialValue.1
7474
}

test/decl/var/init_accessors.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
// RUN: %target-typecheck-verify-swift -enable-experimental-feature InitAccessors
22

3+
func test_empty_init_accessor() {
4+
struct Test {
5+
var empty: Int {
6+
init { // Ok
7+
}
8+
9+
get { 42 }
10+
set { }
11+
}
12+
13+
var noArgs: Int {
14+
init(initialValue) { // Ok
15+
}
16+
17+
get { 42 }
18+
set { }
19+
}
20+
}
21+
}
22+
323
func test_invalid_init_accessor_use() {
424
var other: String = "" // expected-warning {{}}
525
var x: Int {
@@ -48,7 +68,7 @@ func test_invalid_refs_in_init_attrs() {
4868
struct Test {
4969
var c: Int { get { 42 } }
5070
var x: Int {
51-
init(initialValue) initializes(a) accesses(b c) {}
71+
init(initialValue) initializes(a) accesses(b, c) {}
5272
// expected-error@-1 {{find type 'a' in scope}}
5373
// expected-error@-2 {{find type 'b' in scope}}
5474
// expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
@@ -77,7 +97,7 @@ func test_assignment_to_let_properties() {
7797
}
7898

7999
var point: (Int, Int) {
80-
init(initialValue) initializes(x y) {
100+
init(initialValue) initializes(x, y) {
81101
self.x = initialValue.0 // Ok
82102
self.y = initialValue.1 // Ok
83103
}
@@ -91,7 +111,7 @@ func test_duplicate_and_computed_lazy_properties() {
91111
var _b: Int
92112

93113
var a: Int {
94-
init(initialValue) initializes(_b _a) accesses(_a) {
114+
init(initialValue) initializes(_b, _a) accesses(_a) {
95115
// expected-error@-1 {{property '_a' cannot be both initialized and accessed}}
96116
}
97117
}
@@ -101,7 +121,7 @@ func test_duplicate_and_computed_lazy_properties() {
101121
var _a: Int
102122

103123
var a: Int {
104-
init(initialValue) initializes(a c) accesses(_a b) {}
124+
init(initialValue) initializes(a, c) accesses(_a, b) {}
105125
// expected-error@-1 {{init accessor cannot refer to property 'a'; init accessors can refer only to stored properties}}
106126
// expected-error@-2 {{init accessor cannot refer to property 'b'; init accessors can refer only to stored properties}}
107127
// expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}

0 commit comments

Comments
 (0)