Skip to content

Commit a82d33f

Browse files
committed
[SE-0258] Fix crash with implicitly-initialized backing storage.
Fixes SR-10830 / rdar://problem/51443591
1 parent 7fca845 commit a82d33f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lib/AST/Decl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5478,6 +5478,11 @@ bool VarDecl::isPropertyWrapperInitializedWithInitialValue() const {
54785478
// If there is no initializer, the initialization form depends on
54795479
// whether the property wrapper type has an init(initialValue:).
54805480
if (!isParentInitialized()) {
5481+
// If it's default-initializable, that doesn't use an initial value.
5482+
if (auto *PBD = getParentPatternBinding())
5483+
if (PBD->isDefaultInitializable())
5484+
return false;
5485+
54815486
auto wrapperTypeInfo = getAttachedPropertyWrapperTypeInfo();
54825487
return wrapperTypeInfo.initialValueInit != nullptr;
54835488
}

test/SILGen/property_wrappers.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,34 @@ extension ClassUsingWrapper {
269269
}
270270
}
271271

272+
//
273+
@_propertyWrapper
274+
struct WrapperWithDefaultInit<T> {
275+
private var storage: T?
276+
277+
init() {
278+
self.storage = nil
279+
}
280+
281+
init(initialValue: T) {
282+
self.storage = initialValue
283+
}
284+
285+
var value: T {
286+
get { return storage! }
287+
set { storage = newValue }
288+
}
289+
}
290+
291+
class UseWrapperWithDefaultInit {
292+
@WrapperWithDefaultInit var name: String
293+
}
294+
295+
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s17property_wrappers25UseWrapperWithDefaultInitC5$name33_F728088E0028E14D18C6A10CF68512E8LLAA0defG0VySSGvpfi : $@convention(thin) () -> @owned WrapperWithDefaultInit<String>
296+
// CHECK: function_ref @$s17property_wrappers22WrapperWithDefaultInitVACyxGycfC
297+
// CHECK: return {{%.*}} : $WrapperWithDefaultInit<String>
298+
299+
272300
// CHECK-LABEL: sil_vtable ClassUsingWrapper {
273301
// CHECK: #ClassUsingWrapper.x!getter.1: (ClassUsingWrapper) -> () -> Int : @$s17property_wrappers17ClassUsingWrapperC1xSivg // ClassUsingWrapper.x.getter
274302
// CHECK: #ClassUsingWrapper.x!setter.1: (ClassUsingWrapper) -> (Int) -> () : @$s17property_wrappers17ClassUsingWrapperC1xSivs // ClassUsingWrapper.x.setter

0 commit comments

Comments
 (0)