Skip to content

Allow the @concurrent function attribute without the concurrency flag #35959

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 2 commits into from
Feb 13, 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: 2 additions & 2 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(async, Async,
106)

SIMPLE_DECL_ATTR(concurrent, Concurrent,
OnFunc | OnConstructor | OnAccessor | ConcurrencyOnly |
ABIStableToAdd | ABIStableToRemove |
OnFunc | OnConstructor | OnAccessor |
ABIBreakingToAdd | ABIBreakingToRemove |
APIBreakingToAdd | APIBreakingToRemove,
107)

Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ LANGUAGE_FEATURE(StaticAssert, 0, "#assert", langOpts.EnableExperimentalStaticAs
LANGUAGE_FEATURE(AsyncAwait, 296, "async/await", true)
LANGUAGE_FEATURE(MarkerProtocol, 0, "@_marker protocol", true)
LANGUAGE_FEATURE(Actors, 0, "actors", langOpts.EnableExperimentalConcurrency)
LANGUAGE_FEATURE(ConcurrentFunctions, 0, "@concurrent functions", true)

#undef LANGUAGE_FEATURE
26 changes: 26 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,32 @@ static bool usesFeatureActors(Decl *decl) {
return false;
}

static bool usesFeatureConcurrentFunctions(Decl *decl) {
if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
if (func->isConcurrent())
return true;
}

// Check for concurrent functions in the types of declarations.
if (auto value = dyn_cast<ValueDecl>(decl)) {
if (Type type = value->getInterfaceType()) {
bool hasConcurrent = type.findIf([](Type type) {
if (auto fnType = type->getAs<AnyFunctionType>()) {
if (fnType->isConcurrent())
return true;
}

return false;
});

if (hasConcurrent)
return true;
}
}

return false;
}

/// Determine the set of "new" features used on a given declaration.
///
/// Note: right now, all features we check for are "new". At some point, we'll
Expand Down
12 changes: 1 addition & 11 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2339,17 +2339,7 @@ TypeResolver::resolveAttributedType(TypeAttributes &attrs, TypeRepr *repr,
}
}

bool concurrent = false;
if (attrs.has(TAK_concurrent)) {
if (getASTContext().LangOpts.EnableExperimentalConcurrency) {
concurrent = true;
} else {
diagnoseInvalid(repr, attrs.getLoc(TAK_concurrent),
diag::attr_requires_concurrency,
TypeAttributes::getAttrName(TAK_concurrent),
false);
}
}
bool concurrent = attrs.has(TAK_concurrent);

ty = resolveASTFunctionType(fnRepr, options, rep, /*noescape=*/false,
concurrent, parsedClangFunctionType,
Expand Down
4 changes: 4 additions & 0 deletions test/IDE/complete_decl_attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct MyStruct {}
// KEYWORD2-NEXT: Keyword/None: derivative[#Func Attribute#]; name=derivative
// KEYWORD2-NEXT: Keyword/None: transpose[#Func Attribute#]; name=transpose
// KEYWORD2-NEXT: Keyword/None: noDerivative[#Func Attribute#]; name=noDerivative
// KEYWORD2-NEXT: Keyword/None: concurrent[#Func Attribute#]; name=concurrent
// KEYWORD2-NOT: Keyword
// KEYWORD2: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// KEYWORD2: End completions
Expand Down Expand Up @@ -190,6 +191,7 @@ struct _S {
// ON_METHOD-DAG: Keyword/None: differentiable[#Func Attribute#]; name=differentiable
// ON_METHOD-DAG: Keyword/None: derivative[#Func Attribute#]; name=derivative
// ON_METHOD-DAG: Keyword/None: transpose[#Func Attribute#]; name=transpose
// ON_METHOD-DAG: Keyword/None: concurrent[#Func Attribute#]; name=concurrent
// ON_METHOD-DAG: Keyword/None: noDerivative[#Func Attribute#]; name=noDerivative
// ON_METHOD-NOT: Keyword
// ON_METHOD: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
Expand Down Expand Up @@ -251,6 +253,7 @@ struct _S {
// ON_MEMBER_LAST-DAG: Keyword/None: derivative[#Declaration Attribute#]; name=derivative
// ON_MEMBER_LAST-DAG: Keyword/None: transpose[#Declaration Attribute#]; name=transpose
// ON_MEMBER_LAST-DAG: Keyword/None: noDerivative[#Declaration Attribute#]; name=noDerivative
// ON_MEMBER_LAST-DAG: Keyword/None: concurrent[#Declaration Attribute#]; name=concurrent
// ON_MEMBER_LAST-NOT: Keyword
// ON_MEMBER_LAST: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// ON_MEMBER_LAST-NOT: Decl[PrecedenceGroup]
Expand Down Expand Up @@ -300,6 +303,7 @@ func dummy2() {}
// KEYWORD_LAST-NEXT: Keyword/None: derivative[#Declaration Attribute#]; name=derivative
// KEYWORD_LAST-NEXT: Keyword/None: transpose[#Declaration Attribute#]; name=transpose
// KEYWORD_LAST-NEXT: Keyword/None: noDerivative[#Declaration Attribute#]; name=noDerivative
// KEYWORD_LAST-NEXT: Keyword/None: concurrent[#Declaration Attribute#]; name=concurrent
// KEYWORD_LAST-NOT: Keyword
// KEYWORD_LAST: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// KEYWORD_LAST: End completions
5 changes: 5 additions & 0 deletions test/ModuleInterface/features.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ extension OldSchool: UnsafeConcurrentValue { }
// CHECK-NEXT: #endif
public func runSomethingSomewhere(body: () async -> Void) { }

// CHECK: #if compiler(>=5.3) && $ConcurrentFunctions
// CHECK-NEXT: func runSomethingConcurrently(body: @concurrent () ->
// CHECK-NEXT: #endif
public func runSomethingConcurrently(body: @concurrent () -> Void) { }

// CHECK: #if compiler(>=5.3) && $Actors
// CHECK-NEXT: func stage
// CHECK-NEXT: #endif
Expand Down
2 changes: 1 addition & 1 deletion test/attr/attr_concurrent.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -typecheck -verify %s -enable-experimental-concurrency -enable-experimental-flow-sensitive-concurrent-captures
// RUN: %target-swift-frontend -typecheck -verify %s -enable-experimental-flow-sensitive-concurrent-captures
// REQUIRES: concurrency

// Concurrent attribute on a function type.
Expand Down