Skip to content

Commit 9adbd4c

Browse files
authored
Merge pull request swiftlang#37639 from hborla/5.5-local-default-init-wrapper
[5.5][Property Wrappers] Always check actor isolation and initializer effects in `PropertyWrapperInitializerInfoRequest`.
2 parents bb9588e + e26a400 commit 9adbd4c

File tree

4 files changed

+59
-30
lines changed

4 files changed

+59
-30
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,31 +1792,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
17921792
});
17931793
}
17941794

1795-
/// If the given pattern binding has a property wrapper, check the
1796-
/// isolation and effects of the backing storage initializer.
1797-
void checkPropertyWrapperBackingInitializer(PatternBindingDecl *PBD) {
1798-
auto singleVar = PBD->getSingleVar();
1799-
if (!singleVar)
1800-
return;
1801-
1802-
if (!singleVar->hasAttachedPropertyWrapper())
1803-
return;
1804-
1805-
auto *backingVar = singleVar->getPropertyWrapperBackingProperty();
1806-
if (!backingVar)
1807-
return;
1808-
1809-
auto backingPBD = backingVar->getParentPatternBinding();
1810-
if (!backingPBD)
1811-
return;
1812-
1813-
auto initInfo = singleVar->getPropertyWrapperInitializerInfo();
1814-
if (auto initializer = initInfo.getInitFromWrappedValue()) {
1815-
checkPropertyWrapperActorIsolation(backingPBD, initializer);
1816-
TypeChecker::checkPropertyWrapperEffects(backingPBD, initializer);
1817-
}
1818-
}
1819-
18201795
void visitPatternBindingDecl(PatternBindingDecl *PBD) {
18211796
DeclContext *DC = PBD->getDeclContext();
18221797

@@ -1963,8 +1938,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
19631938
}
19641939
}
19651940
}
1966-
1967-
checkPropertyWrapperBackingInitializer(PBD);
19681941
}
19691942

19701943
void visitSubscriptDecl(SubscriptDecl *SD) {

lib/Sema/TypeCheckStorage.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,11 @@ static void typeCheckSynthesizedWrapperInitializer(VarDecl *wrappedVar,
25832583
dyn_cast_or_null<Initializer>(parentPBD->getInitContext(i))) {
25842584
TypeChecker::contextualizeInitializer(initializerContext, initializer);
25852585
}
2586+
2587+
auto *backingVar = wrappedVar->getPropertyWrapperBackingProperty();
2588+
auto *backingPBD = backingVar->getParentPatternBinding();
2589+
checkPropertyWrapperActorIsolation(backingPBD, initializer);
2590+
TypeChecker::checkPropertyWrapperEffects(backingPBD, initializer);
25862591
}
25872592

25882593
static PropertyWrapperMutability::Value
@@ -2870,10 +2875,16 @@ PropertyWrapperInitializerInfoRequest::evaluate(Evaluator &evaluator,
28702875
if (!parentPBD->isInitialized(patternNumber) && wrapperInfo.defaultInit) {
28712876
// FIXME: Record this expression somewhere so that DI can perform the
28722877
// initialization itself.
2873-
Expr *initializer = nullptr;
2874-
typeCheckSynthesizedWrapperInitializer(var, initializer);
2875-
pbd->setInit(0, initializer);
2878+
Expr *defaultInit = nullptr;
2879+
typeCheckSynthesizedWrapperInitializer(var, defaultInit);
2880+
pbd->setInit(0, defaultInit);
28762881
pbd->setInitializerChecked(0);
2882+
2883+
// If a static, global, or local wrapped property has a default
2884+
// initializer, this is the only initializer that will be used.
2885+
if (var->isStatic() || !dc->isTypeContext()) {
2886+
initializer = defaultInit;
2887+
}
28772888
} else if (var->hasObservers() && !dc->isTypeContext()) {
28782889
var->diagnose(diag::observingprop_requires_initializer);
28792890
}

test/Concurrency/global_actor_inference.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,22 @@ actor WrapperActorBad2<Wrapped> {
374374
}
375375
}
376376

377+
@propertyWrapper
378+
struct WrapperWithMainActorDefaultInit {
379+
var wrappedValue: Int { fatalError() }
380+
381+
@MainActor init() {} // expected-note {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
382+
}
383+
377384
actor ActorWithWrapper {
378385
@WrapperOnActor var synced: Int = 0
379386
// expected-note@-1 3{{property declared here}}
380387
func f() {
381388
_ = synced // expected-error{{'synced' isolated to global actor}}
382389
_ = $synced // expected-error{{'$synced' isolated to global actor}}
383390
_ = _synced // expected-error{{'_synced' isolated to global actor}}
391+
392+
@WrapperWithMainActorDefaultInit var value: Int // expected-error {{call to main actor-isolated initializer 'init()' in a synchronous actor-isolated context}}
384393
}
385394
}
386395

test/SILGen/property_wrapper_local.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,39 @@ func testCaptures() {
190190
// closure #1 in testCaptures()
191191
// CHECK-LABEL: sil private [ossa] @$s22property_wrapper_local12testCapturesyyFyycfU_ : $@convention(thin) (@guaranteed { var Wrapper<Int> }) -> ()
192192
}
193+
194+
@propertyWrapper
195+
struct DefaultInit {
196+
var wrappedValue: Int
197+
198+
// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local11DefaultInitVACycfC : $@convention(method) (@thin DefaultInit.Type) -> DefaultInit
199+
init() {
200+
self.wrappedValue = 0
201+
}
202+
203+
// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local11DefaultInitV5valueACSi_tcfC : $@convention(method) (Int, @thin DefaultInit.Type) -> DefaultInit
204+
init(value: Int) {
205+
self.wrappedValue = value
206+
}
207+
}
208+
209+
@propertyWrapper
210+
struct DefaultWrappedValue {
211+
// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local19DefaultWrappedValueVACycfC : $@convention(method) (@thin DefaultWrappedValue.Type) -> DefaultWrappedValue
212+
var wrappedValue: Int = 10
213+
}
214+
215+
// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local20testLocalDefaultInityyF : $@convention(thin) () -> ()
216+
func testLocalDefaultInit() {
217+
// CHECK: function_ref @$s22property_wrapper_local11DefaultInitVACycfC : $@convention(method) (@thin DefaultInit.Type) -> DefaultInit
218+
@DefaultInit var x: Int
219+
220+
// CHECK: function_ref @$s22property_wrapper_local11DefaultInitV5valueACSi_tcfC : $@convention(method) (Int, @thin DefaultInit.Type) -> DefaultInit
221+
@DefaultInit(value: 10) var z: Int
222+
223+
// CHECK: function_ref @$s22property_wrapper_local11DefaultInitVACycfC : $@convention(method) (@thin DefaultInit.Type) -> DefaultInit
224+
@DefaultInit() var y: Int
225+
226+
// CHECK: function_ref @$s22property_wrapper_local19DefaultWrappedValueVACycfC : $@convention(method) (@thin DefaultWrappedValue.Type) -> DefaultWrappedValue
227+
@DefaultWrappedValue var w: Int
228+
}

0 commit comments

Comments
 (0)