-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Make top-level code variables @MainActor @preconcurrency
#40998
Merged
etcwilde
merged 6 commits into
swiftlang:main
from
etcwilde:ewilde/mainactor-top-level-vars
Jan 28, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4e93bd
Top-level code is executed on the main actor
etcwilde 2c3bef8
Top-level variables are on the MainActor
etcwilde 089b78f
Add @preconcurrency to toplevel vars
etcwilde 5a04344
Update toplevel global actor isolation test
etcwilde 83dbd62
Test predatesConcurrency is only applied in Swift 5
etcwilde 8d509ad
Test disallowing global actors on top-level vars
etcwilde 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 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 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 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 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 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,3 @@ | ||
func foo() -> Int { | ||
a + 10 | ||
} |
This file contains 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,44 @@ | ||
// RUN: not %target-swift-frontend -enable-experimental-async-top-level -swift-version 6 -typecheck %s %S/Inputs/foo.swift 2>&1 | %FileCheck %s --check-prefixes='Swift6-CHECK,CHECK' | ||
// RUN: not %target-swift-frontend -enable-experimental-async-top-level -swift-version 5 -typecheck %s %S/Inputs/foo.swift 2>&1 | %FileCheck %s --check-prefixes='Swift5-CHECK,CHECK' | ||
|
||
var a = 10 | ||
|
||
@MainActor | ||
var b = 14 | ||
// CHECK: top-level code variables cannot have a global actor | ||
|
||
func nonIsolatedSynchronous() { | ||
print(a) | ||
// Swift6-CHECK: var 'a' isolated to global actor 'MainActor' | ||
// Swift6-CHECK: add '@MainActor' to make global function 'nonIsolatedSynchronous()' part of global actor 'MainActor' | ||
|
||
// Swift5-CHECK-NOT: var 'a' isolated to global actor 'MainActor' | ||
// Swift5-CHECK-NOT: add '@MainActor' to make global function 'nonIsolatedSynchronous()' part of global actor 'MainActor' | ||
} | ||
|
||
func nonIsolatedAsync() async { | ||
print(a) | ||
// CHECK: expression is 'async' but is not marked with 'await' | ||
// CHECK: property access is 'async' | ||
} | ||
|
||
await nonIsolatedAsync() | ||
|
||
// Swift6-CHECK: foo.swift{{.*}}var 'a' isolated to global actor 'MainActor' can not be referenced from this synchronous context | ||
// Swift6-CHECK: foo.swift{{.*}}add '@MainActor' to make global function 'foo()' part of global actor 'MainActor' | ||
// Swift6-CHECK: var declared here | ||
|
||
// Swift5-CHECK-NOT: foo.swift{{.*}}var 'a' isolated to global actor 'MainActor' can not be referenced from this synchronous context | ||
// Swift5-CHECK-NOT: foo.swift{{.*}}add '@MainActor' to make global function 'foo()' part of global actor 'MainActor' | ||
// Swift5-CHECK-NOT: var declared here | ||
|
||
@MainActor | ||
func isolated() { | ||
print(a) | ||
} | ||
|
||
func asyncFun() async { | ||
await print(a) | ||
} | ||
|
||
await asyncFun() |
This file contains 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 |
---|---|---|
@@ -1,111 +1,126 @@ | ||
// RUN: %target-swift-emit-silgen -Xllvm -sil-full-demangle -enable-experimental-async-top-level %s | %FileCheck %s | ||
// RUN: %target-swift-emit-silgen -Xllvm -sil-full-demangle -disable-availability-checking -enable-experimental-async-top-level %s | %FileCheck %s | ||
|
||
// a | ||
// CHECK-LABEL: sil_global hidden @$s24toplevel_globalactorvars1aSivp : $Int | ||
// b | ||
// CHECK-LABEL: sil_global hidden @$s24toplevel_globalactorvars1bSivp : $Int | ||
|
||
// CHECK-LABEL: sil hidden [ossa] @async_Main | ||
// CHECK: bb0: | ||
// CHECK-NEXT: // function_ref | ||
// CHECK-NEXT: [[GET_MAIN:%.*]] = function_ref @swift_task_getMainExecutor | ||
// CHECK-NEXT: [[MAIN:%.*]] = apply [[GET_MAIN]]() | ||
// CHECK-NEXT: alloc_global @$s24toplevel_globalactorvars1aSivp | ||
// CHECK-NEXT: [[AREF:%[0-9]+]] = global_addr @$s24toplevel_globalactorvars1aSivp : $*Int | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
actor MyActorImpl {} | ||
|
||
@available(SwiftStdlib 5.1, *) | ||
@globalActor | ||
struct MyActor { | ||
static let shared = MyActorImpl() | ||
} | ||
|
||
@MyActor | ||
var a = 10 | ||
|
||
// a initialization | ||
// CHECK: alloc_global @$s24toplevel_globalactorvars1aSivp | ||
// CHECK: [[AREF:%[0-9]+]] = global_addr @$s24toplevel_globalactorvars1aSivp | ||
// CHECK: [[TEN_LIT:%[0-9]+]] = integer_literal $Builtin.IntLiteral, 10 | ||
// CHECK: [[INT_TYPE:%[0-9]+]] = metatype $@thin Int.Type | ||
// CHECK: [[INT_INIT:%[0-9]+]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC | ||
// CHECK: [[TEN:%[0-9]+]] = apply [[INT_INIT]]([[TEN_LIT]], [[INT_TYPE]]) | ||
// CHECK: store [[TEN]] to [trivial] [[AREF]] | ||
|
||
@MyActor | ||
func incrementA() { | ||
a += 1 | ||
func printFromMyActor(value : Int) { | ||
print(value) | ||
} | ||
|
||
await print(a) | ||
print(a) | ||
|
||
// CHECK-NOT: hop_to_executor | ||
|
||
// CHECK: [[ACTORREF:%[0-9]+]] = begin_borrow {{%[0-9]+}} : $MyActorImpl | ||
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl | ||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int | ||
// CHECK: [[AGLOBAL:%[0-9]+]] = load [trivial] [[AACCESS]] : $*Int | ||
// CHECK: end_access [[AACCESS]] | ||
// CHECK: hop_to_executor [[MAIN]] | ||
// CHECK: end_borrow [[ACTORREF]] | ||
// CHECK-NOT: hop_to_executor | ||
|
||
a += 1 | ||
|
||
// CHECK: [[ONE_LIT:%[0-9]+]] = integer_literal $Builtin.IntLiteral, 1 | ||
// CHECK: [[INT_TYPE:%[0-9]+]] = metatype $@thin Int.Type | ||
// CHECK: [[INT_INIT:%[0-9]+]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC | ||
// CHECK: [[ONE:%[0-9]+]] = apply [[INT_INIT]]([[ONE_LIT]], [[INT_TYPE]]) | ||
// CHECK-NOT: hop_to_executor | ||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [modify] [dynamic] [[AREF]] : $*Int | ||
// static Int.+= infix(_:_:) | ||
// CHECK: [[PE_INT_FUNC:%[0-9]+]] = function_ref @$sSi2peoiyySiz_SitFZ | ||
// CHECK: [[INCREMENTED:%[0-9]+]] = apply [[PE_INT_FUNC]]([[AACCESS]], [[ONE]], {{%[0-9]+}}) | ||
// CHECK: end_access [[AACCESS]] | ||
// CHECK-NOT: hop_to_executor | ||
|
||
|
||
await incrementA() | ||
await printFromMyActor(value: a) | ||
|
||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int | ||
// CHECK: [[AGLOBAL:%[0-9]+]] = load [trivial] [[AACCESS]] : $*Int | ||
// CHECK: end_access [[AACCESS]] | ||
|
||
// CHECK: [[INCREMENTA:%[0-9]+]] = function_ref @$s24toplevel_globalactorvars10incrementAyyF | ||
// CHECK: [[PRINTFROMMYACTOR_FUNC:%[0-9]+]] = function_ref @$s24toplevel_globalactorvars16printFromMyActor5valueySi_tF | ||
// CHECK: [[ACTORREF:%[0-9]+]] = begin_borrow {{%[0-9]+}} : $MyActorImpl | ||
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl | ||
// CHECK: {{%[0-9]+}} = apply [[INCREMENTA]]() | ||
// CHECK: {{%[0-9]+}} = apply [[PRINTFROMMYACTOR_FUNC]]([[AGLOBAL]]) | ||
// CHECK: hop_to_executor [[MAIN]] | ||
// CHECK: end_borrow [[ACTORREF]] | ||
|
||
await print(a) | ||
|
||
// CHECK: [[ACTORREF:%[0-9]+]] = begin_borrow {{%[0-9]+}} : $MyActorImpl | ||
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl | ||
if a < 10 { | ||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int | ||
// CHECK: [[AGLOBAL:%[0-9]+]] = load [trivial] [[AACCESS]] : $*Int | ||
// CHECK: end_access [[AACCESS]] | ||
// CHECK: hop_to_executor [[MAIN]] | ||
// CHECK: end_borrow [[ACTORREF]] | ||
|
||
var b = 11 | ||
// CHECK: [[TEN_LIT:%[0-9]+]] = integer_literal $Builtin.IntLiteral, 10 | ||
// CHECK: [[INT_TYPE:%[0-9]+]] = metatype $@thin Int.Type | ||
// CHECK: [[INT_INIT:%[0-9]+]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC | ||
// CHECK: [[TEN:%[0-9]+]] = apply [[INT_INIT]]([[TEN_LIT]], [[INT_TYPE]]) | ||
// function_ref static Swift.Int.< infix(Swift.Int, Swift.Int) -> Swift.Bool | ||
// CHECK: [[LESS_FUNC:%[0-9]+]] = function_ref @$sSi1loiySbSi_SitFZ | ||
// CHECK: [[WRAPPED_COND:%[0-9]+]] = apply [[LESS_FUNC]]([[AGLOBAL]], [[TEN]], {{%[0-9]+}}) | ||
// CHECK: [[COND:%[0-9]+]] = struct_extract [[WRAPPED_COND]] | ||
// CHECK: cond_br [[COND]], bb1, bb2 | ||
// CHECK: bb1: | ||
|
||
print(a) | ||
|
||
// CHECK: alloc_global @$s24toplevel_globalactorvars1bSivp | ||
// CHECK: [[BGLOBAL_ADDR:%[0-9]+]] = global_addr @$s24toplevel_globalactorvars1bSivp | ||
// Int.init(_builtinIntegerLiteral:) | ||
// CHECK: [[INT_INIT_FUNC:%[0-9]+]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC | ||
// CHECK: [[INITD_INT:%[0-9]+]] = apply [[INT_INIT_FUNC]]({{%[0-9]+}}, {{%[0-9]+}}) | ||
// CHECK: store [[INITD_INT]] to [trivial] [[BGLOBAL_ADDR]] | ||
// CHECK-NOT: hop_to_executor | ||
|
||
b += 1 | ||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int | ||
// CHECK: [[AGLOBAL:%[0-9]+]] = load [trivial] [[AACCESS]] : $*Int | ||
// CHECK: end_access [[AACCESS]] | ||
// CHECK-NOT: hop_to_executor | ||
|
||
// CHECK-NOT: hop_to_executor | ||
// CHECK: [[BACCESS:%[0-9]+]] = begin_access [modify] [dynamic] [[BGLOBAL_ADDR]] | ||
// static Int.+= infix(_:_:) | ||
// CHECK: [[PE_INT_FUNC:%[0-9]+]] = function_ref @$sSi2peoiyySiz_SitFZ | ||
// CHECK: [[INCREMENTED:%[0-9]+]] = apply [[PE_INT_FUNC]]([[BACCESS]], {{%[0-9]+}}, {{%[0-9]+}}) | ||
// CHECK: end_access [[BACCESS]] | ||
a += 1 | ||
|
||
// CHECK: [[ONE_LIT:%[0-9]+]] = integer_literal $Builtin.IntLiteral, 1 | ||
// CHECK: [[INT_TYPE:%[0-9]+]] = metatype $@thin Int.Type | ||
// CHECK: [[INT_INIT:%[0-9]+]] = function_ref @$sSi22_builtinIntegerLiteralSiBI_tcfC | ||
// CHECK: [[ONE:%[0-9]+]] = apply [[INT_INIT]]([[ONE_LIT]], [[INT_TYPE]]) | ||
// CHECK-NOT: hop_to_executor | ||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [modify] [dynamic] [[AREF]] : $*Int | ||
// static Int.+= infix(_:_:) | ||
// CHECK: [[PE_INT_FUNC:%[0-9]+]] = function_ref @$sSi2peoiyySiz_SitFZ | ||
// CHECK: [[INCREMENTED:%[0-9]+]] = apply [[PE_INT_FUNC]]([[AACCESS]], [[ONE]], {{%[0-9]+}}) | ||
// CHECK: end_access [[AACCESS]] | ||
// CHECK-NOT: hop_to_executor | ||
|
||
|
||
// CHECK: bb1: | ||
if #available(SwiftStdlib 5.1, *) { | ||
await print(a) | ||
await printFromMyActor(value: a) | ||
|
||
// CHECK: [[ACTORREF:%[0-9]+]] = begin_borrow {{%[0-9]+}} : $MyActorImpl | ||
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl | ||
// CHECK: [[AACCESS:%[0-9]+]] = begin_access [read] [dynamic] [[AREF]] : $*Int | ||
// CHECK: [[AGLOBAL:%[0-9]+]] = load [trivial] [[AACCESS]] : $*Int | ||
// CHECK: end_access [[AACCESS]] | ||
// CHECK: hop_to_executor [[MAIN]] | ||
// CHECK: end_borrow [[ACTORREF]] | ||
|
||
await incrementA() | ||
|
||
// CHECK: [[INCREMENTA:%[0-9]+]] = function_ref @$s24toplevel_globalactorvars10incrementAyyF | ||
// CHECK: [[PRINTFROMMYACTOR_FUNC:%[0-9]+]] = function_ref @$s24toplevel_globalactorvars16printFromMyActor5valueySi_tF | ||
// CHECK: [[ACTORREF:%[0-9]+]] = begin_borrow {{%[0-9]+}} : $MyActorImpl | ||
// CHECK: hop_to_executor [[ACTORREF]] : $MyActorImpl | ||
// CHECK: {{%[0-9]+}} = apply [[INCREMENTA]]() | ||
// CHECK: {{%[0-9]+}} = apply [[PRINTFROMMYACTOR_FUNC]]([[AGLOBAL]]) | ||
// CHECK: hop_to_executor [[MAIN]] | ||
// CHECK: end_borrow [[ACTORREF]] | ||
|
||
|
||
b += 1 | ||
|
||
// CHECK-NOT: hop_to_executor | ||
// CHECK: [[BACCESS:%[0-9]+]] = begin_access [modify] [dynamic] [[BGLOBAL_ADDR]] | ||
// static Int.+= infix(_:_:) | ||
// CHECK: [[PE_INT_FUNC:%[0-9]+]] = function_ref @$sSi2peoiyySiz_SitFZ | ||
// CHECK: [[INCREMENTED:%[0-9]+]] = apply [[PE_INT_FUNC]]([[BACCESS]], {{%[0-9]+}}, {{%[0-9]+}}) | ||
// CHECK: end_access [[BACCESS]] | ||
} |
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.
add a test to cover this? there doesn't seem to be one
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.
Done
https://github.com/apple/swift/pull/40998/files#diff-0b3cf9b84143abf54688a2441cb7d2425cb6819e229078be58f610ba2f09c1c7R6-R8