Skip to content

Commit 5700ddc

Browse files
committed
SILGen: fix a crash when assigning a wrapped property in a convenience initializer
Convenience initializers only contain assignments to self, but not initializations. This case was not handled when deciding between a regular assignment and assign_by_wrapper. https://bugs.swift.org/browse/SR-14675 rdar://78892507
1 parent f92f2fe commit 5700ddc

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,9 +1324,12 @@ namespace {
13241324
if (!initInfo.hasInitFromWrappedValue())
13251325
return false;
13261326

1327+
auto *fnDecl = SGF.FunctionDC->getAsDecl();
13271328
bool isAssignmentToSelfParamInInit =
1328-
IsOnSelfParameter &&
1329-
isa<ConstructorDecl>(SGF.FunctionDC->getAsDecl());
1329+
IsOnSelfParameter && isa<ConstructorDecl>(fnDecl) &&
1330+
// Convenience initializers only contain assignments and not
1331+
// initializations.
1332+
!(cast<ConstructorDecl>(fnDecl)->isConvenienceInit());
13301333

13311334
// Assignment to a wrapped property can only be re-written to initialization for
13321335
// members of `self` in an initializer, and for local variables.

test/SILOptimizer/di_property_wrappers.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ final class IntClass {
9292
}
9393
wrapped = 27
9494
}
95+
96+
convenience init(withConvenienceInit x: Int) {
97+
self.init()
98+
self.wrapped = x
99+
}
95100
}
96101

97102
struct RefStruct {
@@ -210,6 +215,13 @@ func testIntClass() {
210215
let t5 = IntClass(dynamic: true)
211216
// CHECK-NEXT: 27
212217
print(t5.wrapped)
218+
219+
// CHECK-NEXT: .. init 42
220+
// CHECK-NEXT: .. set 27
221+
// CHECK-NEXT: .. set 123
222+
let t6 = IntClass(withConvenienceInit: 123)
223+
// CHECK-NEXT: 123
224+
print(t6.wrapped)
213225
}
214226

215227
func testRefStruct() {

0 commit comments

Comments
 (0)