Skip to content

Commit 8a84d87

Browse files
authored
Merge pull request swiftlang#37936 from hborla/reabstractable-wrapped-value
[SILGen] Fix a crash when a wrapped property initial value has a re-abstractable type.
2 parents 53fc177 + e494019 commit 8a84d87

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/SILGen/SILGenConstructor.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,10 +1142,22 @@ void SILGenFunction::emitMemberInitializers(DeclContext *dc,
11421142
// abstraction level. To undo this, we use a converting
11431143
// initialization and rely on the peephole that optimizes
11441144
// out the redundant conversion.
1145-
auto loweredResultTy = getLoweredType(origType, substType);
1146-
auto loweredSubstTy = getLoweredType(substType);
1145+
SILType loweredResultTy;
1146+
SILType loweredSubstTy;
1147+
1148+
// A converting initialization isn't necessary if the member is
1149+
// a property wrapper. Though the initial value can have a
1150+
// reabstractable type, the result of the initialization is
1151+
// always the property wrapper type, which is never reabstractable.
1152+
bool needsConvertingInit = false;
1153+
auto *singleVar = varPattern->getSingleVar();
1154+
if (!(singleVar && singleVar->getOriginalWrappedProperty())) {
1155+
loweredResultTy = getLoweredType(origType, substType);
1156+
loweredSubstTy = getLoweredType(substType);
1157+
needsConvertingInit = loweredResultTy != loweredSubstTy;
1158+
}
11471159

1148-
if (loweredResultTy != loweredSubstTy) {
1160+
if (needsConvertingInit) {
11491161
Conversion conversion = Conversion::getSubstToOrig(
11501162
origType, substType,
11511163
loweredResultTy);

test/SILGen/property_wrappers.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,3 +967,18 @@ struct TestAutoclosureComposition {
967967

968968
// CHECK-LABEL: sil_vtable [serialized] TestMyWrapper
969969
// CHECK: #TestMyWrapper.$useMyWrapper!getter
970+
971+
@propertyWrapper
972+
struct AutoclosureWrapper<T> {
973+
init(wrappedValue: @autoclosure () -> T) {
974+
self.wrappedValue = wrappedValue()
975+
}
976+
var wrappedValue: T
977+
}
978+
979+
struct TestReabstractableWrappedValue<T1> {
980+
struct S<T2> { }
981+
982+
@AutoclosureWrapper var v: S<T1> = S()
983+
init() where T1 == Int { }
984+
}

0 commit comments

Comments
 (0)