Skip to content

Commit 4f9a35e

Browse files
authored
Merge pull request #67228 from xedin/diag-init-accessor-without-getter
[Parse] InitAccessors: Diagnose an attempt to use init accessor witho…
2 parents e36e316 + 0bbf340 commit 4f9a35e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7187,6 +7187,7 @@ struct Parser::ParsedAccessors {
71877187

71887188
/// Find the first accessor that can be used to perform mutation.
71897189
AccessorDecl *findFirstMutator() const {
7190+
if (Init) return Init;
71907191
if (Set) return Set;
71917192
if (Modify) return Modify;
71927193
if (MutableAddress) return MutableAddress;

test/decl/var/init_accessors.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,16 @@ func test_invalid_refs_in_init_attrs() {
8686
// expected-error@-2 {{find type 'b' in scope}}
8787
// expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
8888
init(initialValue) {}
89+
90+
get { 0 }
8991
}
9092

9193
var y: String {
9294
@storageRestrictions(initializes: test)
9395
// expected-error@-1 {{ambiguous reference to member 'test'}}
9496
init(initialValue) {}
97+
98+
get { "" }
9599
}
96100

97101
func test(_: Int) {} // expected-note {{'test' declared here}}
@@ -110,6 +114,8 @@ func test_assignment_to_let_properties() {
110114
self.x = initialValue // Ok
111115
self.y = 42 // expected-error {{cannot assign to property: 'y' is a 'let' constant}}
112116
}
117+
118+
get { x }
113119
}
114120

115121
var point: (Int, Int) {
@@ -157,6 +163,8 @@ func test_duplicate_and_computed_lazy_properties() {
157163
// expected-error@-2 {{init accessor cannot refer to property 'b'; init accessors can refer only to stored properties}}
158164
// expected-error@-3 {{init accessor cannot refer to property 'c'; init accessors can refer only to stored properties}}
159165
init(initialValue) {}
166+
167+
get { _a }
160168
}
161169

162170
var b: Int {
@@ -475,11 +483,15 @@ func test_default_arguments_are_analyzed() {
475483
struct Test {
476484
var pair: (Int, Int) = (0, 1) { // Ok
477485
init {}
486+
487+
get { (0, 1) }
478488
}
479489

480490
var other: (Int, String) = ("", 42) {
481491
// expected-error@-1 {{cannot convert value of type '(String, Int)' to specified type '(Int, String)'}}
482492
init(initialValue) {}
493+
494+
get { (0, "") }
483495
}
484496

485497
var otherPair = (0, 1) {
@@ -558,36 +570,47 @@ func test_invalid_storage_restrictions() {
558570
@storageRestrictions()
559571
// expected-error@-1 {{missing label in @storageRestrictions attribute}}
560572
init {}
573+
574+
get { _a }
561575
}
562576

563577
var b: Int {
564578
@storageRestrictions(initializes:)
565579
// expected-error@-1 {{expected property name in @storageRestrictions list}}
566580
init {}
581+
582+
get { _b }
567583
}
568584

569585
var c: Int {
570586
@storageRestrictions(initializes: a, initializes: b)
571587
// expected-error@-1 {{duplicate label 'initializes' in @storageRestrictions attribute}}
572588
init {}
589+
590+
get { 0 }
573591
}
574592

575593
var d: Int {
576594
@storageRestrictions(accesses: a, accesses: c)
577595
// expected-error@-1 {{duplicate label 'accesses' in @storageRestrictions attribute}}
578596
init {}
597+
598+
get { 0 }
579599
}
580600

581601
var e: Int {
582602
@storageRestrictions(initialize: a, b, accesses: c, d)
583603
// expected-error@-1 {{unexpected label 'initialize' in @storageRestrictions attribute}}
584604
init {}
605+
606+
get { 0 }
585607
}
586608

587609
var f: Int {
588610
@storageRestrictions(initializes: _a, accesses: _b, _a)
589611
// expected-error@-1 {{property '_a' cannot be both initialized and accessed}}
590612
init {}
613+
// expected-error@-1 {{variable with an init accessor must also have a getter}}
591614
}
592615

593616
var g: Int {

0 commit comments

Comments
 (0)