Skip to content
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
merged 6 commits into from
Jan 28, 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
4 changes: 3 additions & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4771,7 +4771,9 @@ ERROR(global_actor_non_unsafe_init,none,
"global actor attribute %0 argument can only be '(unsafe)'", (Type))
ERROR(global_actor_non_final_class,none,
"non-final class %0 cannot be a global actor", (DeclName))

ERROR(global_actor_top_level_var,none,
"top-level code variables cannot have a global actor", ())
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


ERROR(actor_isolation_multiple_attr,none,
"%0 %1 has multiple actor-isolation attributes ('%2' and '%3')",
(DescriptiveDeclKind, DeclName, StringRef, StringRef))
Expand Down
16 changes: 15 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,10 +733,16 @@ bool Decl::preconcurrency() const {
if (isa<ClangModuleUnit>(getDeclContext()->getModuleScopeContext()))
return true;

// Variables declared in top-level code are @_predatesConcurrency
if (const VarDecl *var = dyn_cast<VarDecl>(this)) {
const LangOptions &langOpts = getASTContext().LangOpts;
return !langOpts.isSwiftVersionAtLeast(6) &&
langOpts.EnableExperimentalAsyncTopLevel && var->isTopLevelGlobal();
}

return false;
}


Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
assert(hasSingleExpressionBody() && "Not a single-expression body");
auto braceStmt = getBody();
Expand Down Expand Up @@ -8949,6 +8955,14 @@ ActorIsolation swift::getActorIsolationOfContext(DeclContext *dc) {
}
}

if (auto *tld = dyn_cast<TopLevelCodeDecl>(dc)) {
ASTContext &ctx = dc->getASTContext();
if (ctx.LangOpts.EnableExperimentalAsyncTopLevel) {
if (Type mainActor = ctx.getMainActorType())
return ActorIsolation::forGlobalActor(mainActor, /*unsafe=*/false);
}
}

return ActorIsolation::forUnspecified();
}

Expand Down
15 changes: 14 additions & 1 deletion lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,12 @@ GlobalActorAttributeRequest::evaluate(
} else if (auto storage = dyn_cast<AbstractStorageDecl>(decl)) {
// Subscripts and properties are fine...
if (auto var = dyn_cast<VarDecl>(storage)) {
if (var->getDeclContext()->isLocalContext() && !var->isTopLevelGlobal()) {
if (var->isTopLevelGlobal() &&
var->getASTContext().LangOpts.EnableExperimentalAsyncTopLevel) {
var->diagnose(diag::global_actor_top_level_var)
.highlight(globalActorAttr->getRangeWithAt());
return None;
} else if (var->getDeclContext()->isLocalContext()) {
var->diagnose(diag::global_actor_on_local_variable, var->getName())
.highlight(globalActorAttr->getRangeWithAt());
return None;
Expand Down Expand Up @@ -3505,6 +3510,14 @@ ActorIsolation ActorIsolationRequest::evaluate(
}

if (auto var = dyn_cast<VarDecl>(value)) {
ASTContext &ctx = var->getASTContext();
if (var->isTopLevelGlobal() &&
ctx.LangOpts.EnableExperimentalAsyncTopLevel) {
if (Type mainActor = ctx.getMainActorType())
return inferredIsolation(
ActorIsolation::forGlobalActor(mainActor,
/*unsafe=*/var->preconcurrency()));
}
if (auto isolation = getActorIsolationFromWrappedProperty(var))
return inferredIsolation(isolation);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/TypeCheckConcurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ class ActorIsolationRestriction {
}

/// Determine the isolation rules for a given declaration.
/// The isolation restriction represents the isolation requirement of the
/// using context.
///
/// \param fromExpression Indicates that the reference is coming from an
/// expression.
Expand Down
3 changes: 3 additions & 0 deletions test/Concurrency/toplevel/Inputs/foo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func foo() -> Int {
a + 10
}
44 changes: 44 additions & 0 deletions test/Concurrency/toplevel/main.swift
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()
133 changes: 74 additions & 59 deletions test/SILGen/toplevel_globalactorvars.swift
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)

// print
// 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]]
// print
// 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]]
}