Skip to content

Commit 6d086d4

Browse files
committed
Add support for @Concurrent on function declarations
1 parent 5129d82 commit 6d086d4

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

include/swift/AST/Attr.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(async, Async,
600600
APIBreakingToAdd | APIBreakingToRemove,
601601
106)
602602

603+
SIMPLE_DECL_ATTR(concurrent, Concurrent,
604+
OnFunc | OnConstructor | OnAccessor | ConcurrencyOnly |
605+
ABIStableToAdd | ABIStableToRemove |
606+
APIBreakingToAdd | APIBreakingToRemove,
607+
107)
608+
603609
#undef TYPE_ATTR
604610
#undef DECL_ATTR_ALIAS
605611
#undef CONTEXTUAL_DECL_ATTR_ALIAS

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
121121
IGNORED_ATTR(OriginallyDefinedIn)
122122
IGNORED_ATTR(NoDerivative)
123123
IGNORED_ATTR(SpecializeExtension)
124+
IGNORED_ATTR(Concurrent)
124125
#undef IGNORED_ATTR
125126

126127
void visitAlignmentAttr(AlignmentAttr *attr) {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,8 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25242524
AFD->getParameters()->getParams(argTy);
25252525

25262526
infoBuilder = infoBuilder.withAsync(AFD->hasAsync());
2527+
infoBuilder = infoBuilder.withConcurrent(
2528+
AFD->getAttrs().hasAttribute<ConcurrentAttr>());
25272529
// 'throws' only applies to the innermost function.
25282530
infoBuilder = infoBuilder.withThrows(AFD->hasThrows());
25292531
// Defer bodies must not escape.

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,8 @@ namespace {
15311531
UNINTERESTING_ATTR(ActorIndependent)
15321532
UNINTERESTING_ATTR(GlobalActor)
15331533
UNINTERESTING_ATTR(Async)
1534-
1534+
UNINTERESTING_ATTR(Concurrent)
1535+
15351536
UNINTERESTING_ATTR(AtRethrows)
15361537
#undef UNINTERESTING_ATTR
15371538

test/attr/attr_concurrent.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func onEscapingAutoclosure2(_ fn: @escaping @autoclosure @concurrent () -> Int)
1818
func acceptsConcurrent(_ fn: @concurrent (Int) -> Int) { }
1919
func acceptsNonConcurrent(_ fn: (Int) -> Int) { }
2020

21+
@concurrent func negate(_ x: Int) -> Int { -x }
22+
2123
func passingConcurrentOrNot(
2224
_ cfn: @concurrent (Int) -> Int,
2325
ncfn: (Int) -> Int // expected-note{{parameter 'ncfn' is implicitly non-concurrent}}{{9-9=@concurrent }}
@@ -30,6 +32,11 @@ func passingConcurrentOrNot(
3032
acceptsConcurrent(ncfn) // expected-error{{passing non-concurrent parameter 'ncfn' to function expecting a @concurrent closure}}
3133
acceptsNonConcurrent(cfn) // okay
3234
acceptsNonConcurrent(ncfn) // okay
35+
36+
acceptsConcurrent(negate)
37+
acceptsNonConcurrent(negate)
38+
39+
let _: Int = negate // expected-error{{cannot convert value of type '@concurrent (Int) -> Int' to specified type 'Int'}}
3340
}
3441

3542
func closures() {
@@ -45,3 +52,4 @@ func closures() {
4552
let closure1 = { $0 + 1 } // inferred to be non-concurrent
4653
acceptsConcurrent(closure1) // expected-error{{converting non-concurrent function value to '@concurrent (Int) -> Int' may introduce data races}}
4754
}
55+

0 commit comments

Comments
 (0)