Skip to content

add a note about using actor convenience inits #38096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2021
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ ERROR(self_disallowed_actor_init,none,
"actor 'self' %select{can only|cannot}0 %1 from "
"%select{an async|a global-actor isolated}0 initializer",
(bool, StringRef))
NOTE(actor_convenience_init,none,
"convenience initializers allow non-isolated use of 'self' once "
"initialized",
())



Expand Down
22 changes: 18 additions & 4 deletions lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ namespace {

void reportIllegalUseForActorInit(const DIMemoryUse &Use,
ActorInitKind ActorKind,
StringRef ProblemDesc) const;
StringRef ProblemDesc,
bool suggestConvenienceInit) const;

void handleLoadUseFailureForActorInit(const DIMemoryUse &Use,
ActorInitKind ActorKind) const;
Expand Down Expand Up @@ -1223,7 +1224,8 @@ void LifetimeChecker::handleInOutUse(const DIMemoryUse &Use) {

// 'self' cannot be passed 'inout' from some kinds of actor initializers.
if (isRestrictedActorInitSelf(Use, &ActorKind))
reportIllegalUseForActorInit(Use, ActorKind, "be passed 'inout'");
reportIllegalUseForActorInit(Use, ActorKind, "be passed 'inout'",
/*suggestConvenienceInit=*/false);

// One additional check: 'let' properties may never be passed inout, because
// they are only allowed to have their initial value set, not a subsequent
Expand Down Expand Up @@ -1398,7 +1400,8 @@ void LifetimeChecker::handleEscapeUse(const DIMemoryUse &Use) {

// no escaping uses of 'self' are allowed in restricted actor inits.
if (isRestrictedActorInitSelf(Use, &ActorKind))
reportIllegalUseForActorInit(Use, ActorKind, "be captured by a closure");
reportIllegalUseForActorInit(Use, ActorKind, "be captured by a closure",
/*suggestConvenienceInit=*/true);

return;
}
Expand Down Expand Up @@ -1809,7 +1812,8 @@ bool LifetimeChecker::isRestrictedActorInitSelf(const DIMemoryUse& Use,
void LifetimeChecker::reportIllegalUseForActorInit(
const DIMemoryUse &Use,
ActorInitKind ActorKind,
StringRef ProblemDesc) const {
StringRef ProblemDesc,
bool suggestConvenienceInit) const {
switch(ActorKind) {
case ActorInitKind::None:
case ActorInitKind::PlainAsync:
Expand All @@ -1825,6 +1829,9 @@ void LifetimeChecker::reportIllegalUseForActorInit(
true, ProblemDesc);
break;
}

if (suggestConvenienceInit)
diagnose(Module, Use.Inst->getLoc(), diag::actor_convenience_init);
}

void LifetimeChecker::handleLoadUseFailureForActorInit(
Expand Down Expand Up @@ -1864,6 +1871,13 @@ void LifetimeChecker::handleLoadUseFailureForActorInit(
diagnose(Module, Use.Inst->getLoc(), diag::self_use_actor_init, true);
break;
}

// We cannot easily determine which argument in the call the use of 'self'
// appears in. If we could, then we could determine whether the callee
// is 'isolated' to that parameter, in order to avoid suggesting a convenience
// init in those cases. Thus, the phrasing of the note should be informative.
if (isa<ApplyInst>(Inst))
diagnose(Module, Use.Inst->getLoc(), diag::actor_convenience_init);
}

/// Check and diagnose various failures when a load use is not fully
Expand Down
58 changes: 58 additions & 0 deletions test/Concurrency/actor_definite_init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ actor Convenient {
guard val > 0 else { throw BogusError.blah }
self.x = 10
say(msg: "hello?") // expected-error {{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

Task { self } // expected-error {{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
}

init(asyncThrowyDesignated val: Int) async throws {
Expand Down Expand Up @@ -95,15 +98,26 @@ actor MyActor {

func helloWorld() {}

convenience init(ci1 c: Bool) {
self.init(i1: c)
Task { self }
callMethod(self)
}

init(i1 c: Bool) {
self.x = 0
_ = self.x
self.y = self.x

Task { self } // expected-error{{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

self.helloWorld() // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

callMethod(self) // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

passInout(&self.x) // expected-error{{actor 'self' can only be passed 'inout' from an async initializer}}

self.x = self.y
Expand All @@ -115,9 +129,13 @@ actor MyActor {
_ = self.hax

_ = computedProp // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

computedProp = 1 // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

Task { // expected-error {{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
_ = await self.hax
await self.helloWorld()
}
Expand All @@ -129,9 +147,14 @@ actor MyActor {
self.y = self.x

Task { self } // expected-error{{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

self.helloWorld() // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

callMethod(self) // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

passInout(&self.x) // expected-error{{actor 'self' can only be passed 'inout' from an async initializer}}

self.x = self.y
Expand All @@ -140,9 +163,13 @@ actor MyActor {
_ = self.hax

_ = computedProp // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

computedProp = 1 // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

Task { // expected-error {{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
_ = await self.hax
await self.helloWorld()
}
Expand All @@ -154,9 +181,14 @@ actor MyActor {
self.y = self.x

Task { self } // expected-error{{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

self.helloWorld() // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

callMethod(self) // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

passInout(&self.x) // expected-error{{actor 'self' can only be passed 'inout' from an async initializer}}

self.x = self.y
Expand All @@ -165,9 +197,13 @@ actor MyActor {
_ = self.hax

_ = computedProp // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

computedProp = 1 // expected-error{{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

Task { // expected-error {{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
_ = await self.hax
await self.helloWorld()
}
Expand All @@ -179,9 +215,14 @@ actor MyActor {
self.y = self.x

Task { self } // expected-error{{actor 'self' cannot be captured by a closure from a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

self.helloWorld() // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

callMethod(self) // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

passInout(&self.x) // expected-error{{actor 'self' cannot be passed 'inout' from a global-actor isolated initializer}}

self.x = self.y
Expand All @@ -190,9 +231,13 @@ actor MyActor {
_ = self.hax

_ = computedProp // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

computedProp = 1 // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

Task { // expected-error {{actor 'self' cannot be captured by a closure from a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
_ = await self.hax
await self.helloWorld()
}
Expand Down Expand Up @@ -228,9 +273,14 @@ actor MyActor {
self.y = self.x

Task { self } // expected-error{{actor 'self' cannot be captured by a closure from a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

self.helloWorld() // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

callMethod(self) // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

passInout(&self.x) // expected-error{{actor 'self' cannot be passed 'inout' from a global-actor isolated initializer}}

self.x = self.y
Expand All @@ -239,9 +289,13 @@ actor MyActor {
_ = self.hax

_ = computedProp // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

computedProp = 1 // expected-error{{this use of actor 'self' cannot appear in a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

Task { // expected-error {{actor 'self' cannot be captured by a closure from a global-actor isolated initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
_ = await self.hax
await self.helloWorld()
}
Expand All @@ -257,6 +311,7 @@ actor X {
init(v1 start: Int) {
self.counter = start
Task { await self.setCounter(start + 1) } // expected-error {{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

if self.counter != start {
fatalError("where's my protection?")
Expand Down Expand Up @@ -319,8 +374,10 @@ actor EscapeArtist {
let unchainedSelf = self

unchainedSelf.nonisolated() // expected-error {{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}

let _ = { unchainedSelf.nonisolated() } // expected-error {{actor 'self' can only be captured by a closure from an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
}

init(attempt5: Bool) {
Expand All @@ -336,6 +393,7 @@ actor EscapeArtist {
self.nonisolated()
}
fn() // expected-error {{this use of actor 'self' can only appear in an async initializer}}
// expected-note@-1 {{convenience initializers allow non-isolated use of 'self' once initialized}}
}

func isolatedMethod() { x += 1 }
Expand Down