Skip to content

Commit 7c946da

Browse files
committed
[Sema] InitAccessors: Make sure that init accessor default subsumes initialization of stored properties
Default expression associated with an init accessor property should always subsume that of any property listed in "initializes".
1 parent 6a32af1 commit 7c946da

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,6 +2483,20 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24832483
}
24842484
}
24852485
}
2486+
2487+
// If this is an init accessor property with a default initializer,
2488+
// make sure that it subsumes initializers of all of its "initializes"
2489+
// stored properties.
2490+
if (auto *var = PBD->getSingleVar()) {
2491+
auto *initAccessor = var->getAccessor(AccessorKind::Init);
2492+
if (initAccessor && PBD->isInitialized(0)) {
2493+
for (auto *property : initAccessor->getInitializedProperties()) {
2494+
auto *propertyBinding = property->getParentPatternBinding();
2495+
if (propertyBinding->isInitialized(0))
2496+
propertyBinding->setInitializerSubsumed(0);
2497+
}
2498+
}
2499+
}
24862500
}
24872501

24882502
void visitSubscriptDecl(SubscriptDecl *SD) {

test/Interpreter/init_accessors.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ func test_memberwise_with_default_args() {
479479
test_memberwise_with_default_args()
480480
// CHECK: test-memberwise_with_default-1: TestWithoutDefault(_a: -1, _b: 42)
481481
// CHECK-NEXT: test-memberwise_with_default-2: TestWithoutDefault(_a: 42, _b: -1)
482-
// CHECK-NEXT: test-defaulted-1: TestDefaulted(_a: 0, _b: 0)
482+
// CHECK-NEXT: test-defaulted-1: TestDefaulted(_a: 1, _b: 2)
483483
// CHECK-NEXT: test-defaulted-2: TestDefaulted(_a: 3, _b: 4)
484-
// CHECK-NEXT: test-defaulted-class: ("<<default>>", 1)
484+
// CHECK-NEXT: test-defaulted-class: ("", 42)
485485

486486
func test_init_accessors_without_setters() {
487487
struct TestStruct<T> {

test/SILOptimizer/init_accessor_raw_sil_lowering.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,31 @@ func test_default_inits() {
206206
self.x = x
207207
}
208208
}
209+
210+
class Test3 {
211+
var _x: Int = 42
212+
var x: Int = 42 {
213+
@storageRestrictions(initializes: _x)
214+
init {
215+
_x = newValue
216+
}
217+
get { _x }
218+
}
219+
220+
var _y: String = ""
221+
var y: String = "" {
222+
@storageRestrictions(initializes: _y)
223+
init {
224+
_y = newValue
225+
}
226+
get { _y }
227+
}
228+
229+
// CHECK-LABEL: sil private [ossa] @$s23assign_or_init_lowering18test_default_initsyyF5Test3L_CADycfc : $@convention(method) (@owned Test3) -> @owned Test3
230+
// CHECK: function_ref variable initialization expression of x in Test3 #1 in test_default_inits()
231+
// CHECK-NOT: function_ref variable initialization expression of _x in Test3 #1 in test_default_inits()
232+
//
233+
// CHECK: function_ref variable initialization expression of y in Test3 #1 in test_default_inits()
234+
// CHECK-NOT: function_ref variable initialization expression of _y in Test3 #1 in test_default_inits()
235+
}
209236
}

0 commit comments

Comments
 (0)