Skip to content

Commit e494019

Browse files
committed
[SILGen] Never use a converting initialization when emitting member
initializers if the member is a property wrapper. This code assumes that the initial value of the member and the result of initialization are the same type (but differing in abstraction level). This isn't true for property wrappers because the initial value can be the wrapped value type, but the result is always the property wrapper type. Property wrapper types are never reabstractable types anyway, so the converting initialization isn't necessary.
1 parent 0fccbcb commit e494019

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)