Skip to content

Commit 34cc7ea

Browse files
committed
[Concurrency] Emit a better note when an isolated stored property initializer
cannot be used in an init with mismatching isolation.
1 parent 7dbec84 commit 34cc7ea

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ ERROR(use_of_self_before_fully_init,none,
237237

238238
NOTE(stored_property_not_initialized,none,
239239
"'%0' not initialized", (StringRef))
240+
NOTE(isolated_property_initializer,none,
241+
"%1 default value of '%0' cannot be used in a %2 initalizer",
242+
(StringRef, ActorIsolation, ActorIsolation))
240243

241244
ERROR(selfinit_multiple_times,none,
242245
"'%select{super|self}0.init' called multiple times in initializer",

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,13 @@ void LifetimeChecker::noteUninitializedMembers(const DIMemoryUse &Use) {
725725
std::string Name;
726726
auto *Decl = TheMemory.getPathStringToElement(i, Name);
727727
SILLocation Loc = Use.Inst->getLoc();
728+
auto propertyInitIsolation = ActorIsolation::forUnspecified();
728729

729730
if (Decl) {
731+
if (auto *var = dyn_cast<VarDecl>(Decl)) {
732+
propertyInitIsolation = var->getInitializerIsolation();
733+
}
734+
730735
// If we found a non-implicit declaration, use its source location.
731736
if (!Decl->isImplicit())
732737
Loc = SILLocation(Decl);
@@ -741,8 +746,17 @@ void LifetimeChecker::noteUninitializedMembers(const DIMemoryUse &Use) {
741746
}
742747
}
743748

744-
diagnose(Module, Loc, diag::stored_property_not_initialized,
745-
StringRef(Name));
749+
if (propertyInitIsolation.isGlobalActor()) {
750+
auto *init =
751+
dyn_cast<ConstructorDecl>(F.getDeclContext()->getAsDecl());
752+
diagnose(Module, Loc, diag::isolated_property_initializer,
753+
StringRef(Name), propertyInitIsolation,
754+
getActorIsolation(init));
755+
} else {
756+
diagnose(Module, Loc, diag::stored_property_not_initialized,
757+
StringRef(Name));
758+
}
759+
746760
emittedNote = true;
747761
}
748762

test/Concurrency/isolated_default_property_inits.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ func requiresMainActor() -> Int { 0 }
2020
func requiresSomeGlobalActor() -> Int { 0 }
2121

2222
class C1 {
23-
// expected-note@+1 2 {{'self.x' not initialized}}
23+
// expected-note@+2 {{main actor-isolated default value of 'self.x' cannot be used in a nonisolated initalizer}}
24+
// expected-note@+1 {{main actor-isolated default value of 'self.x' cannot be used in a global actor 'SomeGlobalActor'-isolated initalizer}}
2425
@MainActor var x = requiresMainActor()
25-
// expected-note@+1 2 {{'self.y' not initialized}}
26+
// expected-note@+2 {{global actor 'SomeGlobalActor'-isolated default value of 'self.y' cannot be used in a nonisolated initalizer}}
27+
// expected-note@+1 {{global actor 'SomeGlobalActor'-isolated default value of 'self.y' cannot be used in a main actor-isolated initalizer}}
2628
@SomeGlobalActor var y = requiresSomeGlobalActor()
2729
var z = 10
2830

0 commit comments

Comments
 (0)