Skip to content

Commit f9afa5a

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 be6181b commit f9afa5a

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
@@ -1117,10 +1117,22 @@ void SILGenFunction::emitMemberInitializers(DeclContext *dc,
11171117
// abstraction level. To undo this, we use a converting
11181118
// initialization and rely on the peephole that optimizes
11191119
// out the redundant conversion.
1120-
auto loweredResultTy = getLoweredType(origType, substType);
1121-
auto loweredSubstTy = getLoweredType(substType);
1120+
SILType loweredResultTy;
1121+
SILType loweredSubstTy;
1122+
1123+
// A converting initialization isn't necessary if the member is
1124+
// a property wrapper. Though the initial value can have a
1125+
// reabstractable type, the result of the initialization is
1126+
// always the property wrapper type, which is never reabstractable.
1127+
bool needsConvertingInit = false;
1128+
auto *singleVar = varPattern->getSingleVar();
1129+
if (!(singleVar && singleVar->getOriginalWrappedProperty())) {
1130+
loweredResultTy = getLoweredType(origType, substType);
1131+
loweredSubstTy = getLoweredType(substType);
1132+
needsConvertingInit = loweredResultTy != loweredSubstTy;
1133+
}
11221134

1123-
if (loweredResultTy != loweredSubstTy) {
1135+
if (needsConvertingInit) {
11241136
Conversion conversion = Conversion::getSubstToOrig(
11251137
origType, substType,
11261138
loweredResultTy);

test/SILGen/property_wrappers.swift

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

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

0 commit comments

Comments
 (0)