Skip to content

prevent nonisolated mutation of isolated properties through an existential #59655

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 23, 2022
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
5 changes: 5 additions & 0 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,11 @@ namespace {
return recordMutableVarParent(parent, inout->getSubExpr());
}

// Look through an expression that opens an existential
if (auto openExist = dyn_cast<OpenExistentialExpr>(subExpr)) {
return recordMutableVarParent(parent, openExist->getSubExpr());
}

return false;
}

Expand Down
69 changes: 69 additions & 0 deletions test/Concurrency/actor_existentials.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// RUN: %target-typecheck-verify-swift -disable-availability-checking
// REQUIRES: concurrency

protocol P: Actor {
func f()
var prop: Int { get set } // expected-note 2 {{mutation of this property is only permitted within the actor}}
}

actor A: P {
var prop: Int = 0 // expected-note 2 {{mutation of this property is only permitted within the actor}}
func f() {}
}

func from_isolated_existential1(_ x: isolated any P) {
x.f()
x.prop += 1
x.prop = 100
}

func from_isolated_existential2(_ x: isolated any P) async {
x.f()
x.prop += 1
x.prop = 100
}

func from_nonisolated(_ x: any P) async {
await x.f()
x.prop += 1 // expected-error {{actor-isolated property 'prop' can not be mutated from a non-isolated context}}
x.prop = 100 // expected-error {{actor-isolated property 'prop' can not be mutated from a non-isolated context}}
}

func from_concrete(_ x: A) async {
x.prop += 1 // expected-error {{actor-isolated property 'prop' can not be mutated from a non-isolated context}}
x.prop = 100 // expected-error {{actor-isolated property 'prop' can not be mutated from a non-isolated context}}
}

func from_isolated_concrete(_ x: isolated A) async {
x.prop += 1
x.prop = 100
}


// from https://github.com/apple/swift/issues/59573
actor Act {
var i = 0 // expected-note {{mutation of this property is only permitted within the actor}}
}
let act = Act()

func bad() async {
// expected-warning@+2 {{no 'async' operations occur within 'await' expression}}
// expected-error@+1 {{actor-isolated property 'i' can not be mutated from a non-isolated context}}
await act.i = 666
}

protocol Proto: Actor {
var i: Int { get set } // expected-note 2 {{mutation of this property is only permitted within the actor}}
}
extension Act: Proto {}

func good() async {
// expected-warning@+2 {{no 'async' operations occur within 'await' expression}}
// expected-error@+1 {{actor-isolated property 'i' can not be mutated from a non-isolated context}}
await (act as any Proto).i = 42
let aIndirect: any Proto = act

// expected-warning@+2 {{no 'async' operations occur within 'await' expression}}
// expected-error@+1 {{actor-isolated property 'i' can not be mutated from a non-isolated context}}
await aIndirect.i = 777
}