Skip to content

Commit b4303cd

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". (cherry picked from commit fd2a7d5)
1 parent bf8bb73 commit b4303cd

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
@@ -2448,6 +2448,20 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
24482448
}
24492449
}
24502450
}
2451+
2452+
// If this is an init accessor property with a default initializer,
2453+
// make sure that it subsumes initializers of all of its "initializes"
2454+
// stored properties.
2455+
if (auto *var = PBD->getSingleVar()) {
2456+
auto *initAccessor = var->getAccessor(AccessorKind::Init);
2457+
if (initAccessor && PBD->isInitialized(0)) {
2458+
for (auto *property : initAccessor->getInitializedProperties()) {
2459+
auto *propertyBinding = property->getParentPatternBinding();
2460+
if (propertyBinding->isInitialized(0))
2461+
propertyBinding->setInitializerSubsumed(0);
2462+
}
2463+
}
2464+
}
24512465
}
24522466

24532467
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)