-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Look through defer bodies in actor isolation checking #41067
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// RUN: %target-swift-frontend -parse-as-library -emit-sil -DNEGATIVES -verify %s | ||
// RUN: %target-swift-frontend -parse-as-library -emit-sil -enable-actor-data-race-checks -o - %s | %FileCheck %s | ||
|
||
// REQUIRES: concurrency | ||
|
||
func doSomething() {} | ||
|
||
// expected-note @+1 4 {{calls to global function 'requiresMainActor()' from outside of its actor context are implicitly asynchronous}} | ||
@MainActor func requiresMainActor() {} | ||
|
||
@MainActor func testNonDefer_positive() { | ||
requiresMainActor() | ||
} | ||
|
||
#if NEGATIVES | ||
// expected-note @+1 {{add '@MainActor' to make global function 'testNonDefer_negative()' part of global actor 'MainActor'}} | ||
func testNonDefer_negative() { | ||
// expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}} | ||
requiresMainActor() | ||
} | ||
#endif | ||
|
||
@MainActor func testGlobalActor_positive() { | ||
defer { | ||
requiresMainActor() | ||
} | ||
doSomething() | ||
} | ||
// Don't include a data race check at the start of the defer | ||
// CHECK-LABEL: sil private @$s11actor_defer24testGlobalActor_positiveyyF6$deferL_yyF | ||
// CHECK-NEXT: bb0: | ||
// CHECK-NEXT: // function_ref | ||
// CHECK-NEXT: function_ref | ||
// CHECK-NEXT: apply | ||
|
||
#if NEGATIVES | ||
// expected-note @+1 {{add '@MainActor' to make global function 'testGlobalActor_negative()' part of global actor 'MainActor'}} | ||
func testGlobalActor_negative() { | ||
defer { | ||
// expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}} | ||
requiresMainActor() | ||
} | ||
doSomething() | ||
} | ||
#endif | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
@MainActor func testGlobalActorAsync_positive() async { | ||
defer { | ||
requiresMainActor() | ||
} | ||
doSomething() | ||
} | ||
|
||
#if NEGATIVES | ||
// expected-note @+2 {{add '@MainActor' to make global function 'testGlobalActorAsync_negative()' part of global actor 'MainActor'}} | ||
@available(SwiftStdlib 5.1, *) | ||
func testGlobalActorAsync_negative() async { | ||
defer { | ||
// expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous nonisolated context}} | ||
requiresMainActor() | ||
} | ||
doSomething() | ||
} | ||
#endif | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
actor Actor { | ||
// expected-note @+1 3 {{mutation of this property is only permitted within the actor}} | ||
var actorProperty = 0 | ||
|
||
func testActor_positive() { | ||
defer { | ||
actorProperty += 1 | ||
} | ||
doSomething() | ||
} | ||
|
||
#if NEGATIVES | ||
nonisolated func testActor_negative() { | ||
defer { | ||
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from a non-isolated context}} | ||
actorProperty += 1 | ||
} | ||
doSomething() | ||
} | ||
@MainActor func testActor_negative_globalActor() { | ||
defer { | ||
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from the main actor}} | ||
actorProperty += 1 | ||
} | ||
doSomething() | ||
} | ||
#endif | ||
|
||
@MainActor func testGlobalActor_positive() { | ||
defer { | ||
requiresMainActor() | ||
} | ||
doSomething() | ||
} | ||
|
||
#if NEGATIVES | ||
func testGlobalActor_negative() { | ||
defer { | ||
// expected-error @+1 {{call to main actor-isolated global function 'requiresMainActor()' in a synchronous actor-isolated context}} | ||
requiresMainActor() | ||
} | ||
doSomething() | ||
} | ||
#endif | ||
} | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
func testIsolatedActor_positive(actor: isolated Actor) { | ||
actor.actorProperty += 1 | ||
defer { | ||
actor.actorProperty += 1 | ||
} | ||
doSomething() | ||
} | ||
|
||
#if NEGATIVES | ||
@available(SwiftStdlib 5.1, *) | ||
func testIsolatedActor_negative(actor: Actor) { | ||
defer { | ||
// expected-error @+1 {{actor-isolated property 'actorProperty' can not be mutated from a non-isolated context}} | ||
actor.actorProperty += 1 | ||
} | ||
doSomething() | ||
} | ||
#endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm good question. I think making it explicit is a good idea regardless.