Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1792,31 +1792,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
});
}

/// If the given pattern binding has a property wrapper, check the
/// isolation and effects of the backing storage initializer.
void checkPropertyWrapperBackingInitializer(PatternBindingDecl *PBD) {
auto singleVar = PBD->getSingleVar();
if (!singleVar)
return;

if (!singleVar->hasAttachedPropertyWrapper())
return;

auto *backingVar = singleVar->getPropertyWrapperBackingProperty();
if (!backingVar)
return;

auto backingPBD = backingVar->getParentPatternBinding();
if (!backingPBD)
return;

auto initInfo = singleVar->getPropertyWrapperInitializerInfo();
if (auto initializer = initInfo.getInitFromWrappedValue()) {
checkPropertyWrapperActorIsolation(backingPBD, initializer);
TypeChecker::checkPropertyWrapperEffects(backingPBD, initializer);
}
}

void visitPatternBindingDecl(PatternBindingDecl *PBD) {
DeclContext *DC = PBD->getDeclContext();

Expand Down Expand Up @@ -1963,8 +1938,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}
}
}

checkPropertyWrapperBackingInitializer(PBD);
}

void visitSubscriptDecl(SubscriptDecl *SD) {
Expand Down
17 changes: 14 additions & 3 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2584,6 +2584,11 @@ static void typeCheckSynthesizedWrapperInitializer(VarDecl *wrappedVar,
dyn_cast_or_null<Initializer>(parentPBD->getInitContext(i))) {
TypeChecker::contextualizeInitializer(initializerContext, initializer);
}

auto *backingVar = wrappedVar->getPropertyWrapperBackingProperty();
auto *backingPBD = backingVar->getParentPatternBinding();
checkPropertyWrapperActorIsolation(backingPBD, initializer);
TypeChecker::checkPropertyWrapperEffects(backingPBD, initializer);
}

static PropertyWrapperMutability::Value
Expand Down Expand Up @@ -2871,10 +2876,16 @@ PropertyWrapperInitializerInfoRequest::evaluate(Evaluator &evaluator,
if (!parentPBD->isInitialized(patternNumber) && wrapperInfo.defaultInit) {
// FIXME: Record this expression somewhere so that DI can perform the
// initialization itself.
Expr *initializer = nullptr;
typeCheckSynthesizedWrapperInitializer(var, initializer);
pbd->setInit(0, initializer);
Expr *defaultInit = nullptr;
typeCheckSynthesizedWrapperInitializer(var, defaultInit);
pbd->setInit(0, defaultInit);
pbd->setInitializerChecked(0);

// If a static, global, or local wrapped property has a default
// initializer, this is the only initializer that will be used.
if (var->isStatic() || !dc->isTypeContext()) {
initializer = defaultInit;
}
} else if (var->hasObservers() && !dc->isTypeContext()) {
var->diagnose(diag::observingprop_requires_initializer);
}
Expand Down
9 changes: 9 additions & 0 deletions test/Concurrency/global_actor_inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,22 @@ actor WrapperActorBad2<Wrapped> {
}
}

@propertyWrapper
struct WrapperWithMainActorDefaultInit {
var wrappedValue: Int { fatalError() }

@MainActor init() {} // expected-note {{calls to initializer 'init()' from outside of its actor context are implicitly asynchronous}}
}

actor ActorWithWrapper {
@WrapperOnActor var synced: Int = 0
// expected-note@-1 3{{property declared here}}
func f() {
_ = synced // expected-error{{'synced' isolated to global actor}}
_ = $synced // expected-error{{'$synced' isolated to global actor}}
_ = _synced // expected-error{{'_synced' isolated to global actor}}

@WrapperWithMainActorDefaultInit var value: Int // expected-error {{call to main actor-isolated initializer 'init()' in a synchronous actor-isolated context}}
}
}

Expand Down
36 changes: 36 additions & 0 deletions test/SILGen/property_wrapper_local.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,39 @@ func testCaptures() {
// closure #1 in testCaptures()
// CHECK-LABEL: sil private [ossa] @$s22property_wrapper_local12testCapturesyyFyycfU_ : $@convention(thin) (@guaranteed { var Wrapper<Int> }) -> ()
}

@propertyWrapper
struct DefaultInit {
var wrappedValue: Int

// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local11DefaultInitVACycfC : $@convention(method) (@thin DefaultInit.Type) -> DefaultInit
init() {
self.wrappedValue = 0
}

// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local11DefaultInitV5valueACSi_tcfC : $@convention(method) (Int, @thin DefaultInit.Type) -> DefaultInit
init(value: Int) {
self.wrappedValue = value
}
}

@propertyWrapper
struct DefaultWrappedValue {
// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local19DefaultWrappedValueVACycfC : $@convention(method) (@thin DefaultWrappedValue.Type) -> DefaultWrappedValue
var wrappedValue: Int = 10
}

// CHECK-LABEL: sil hidden [ossa] @$s22property_wrapper_local20testLocalDefaultInityyF : $@convention(thin) () -> ()
func testLocalDefaultInit() {
// CHECK: function_ref @$s22property_wrapper_local11DefaultInitVACycfC : $@convention(method) (@thin DefaultInit.Type) -> DefaultInit
@DefaultInit var x: Int

// CHECK: function_ref @$s22property_wrapper_local11DefaultInitV5valueACSi_tcfC : $@convention(method) (Int, @thin DefaultInit.Type) -> DefaultInit
@DefaultInit(value: 10) var z: Int

// CHECK: function_ref @$s22property_wrapper_local11DefaultInitVACycfC : $@convention(method) (@thin DefaultInit.Type) -> DefaultInit
@DefaultInit() var y: Int

// CHECK: function_ref @$s22property_wrapper_local19DefaultWrappedValueVACycfC : $@convention(method) (@thin DefaultWrappedValue.Type) -> DefaultWrappedValue
@DefaultWrappedValue var w: Int
}