Skip to content

Commit b32a1ca

Browse files
authored
Merge pull request #80032 from xedin/expand-scope-of-execution-attr
[AST/Sema] Allow `@execution(...)` attribute to be used on initializers and accessors
2 parents 7693508 + bb6326b commit b32a1ca

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

include/swift/AST/DeclAttr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ DECL_ATTR(abi, ABI,
862862
DECL_ATTR_FEATURE_REQUIREMENT(ABI, ABIAttribute)
863863

864864
DECL_ATTR(execution, Execution,
865-
OnFunc,
865+
OnAbstractFunction,
866866
ABIBreakingToAdd | ABIBreakingToRemove | APIBreakingToAdd | APIBreakingToRemove,
867867
166)
868868
DECL_ATTR_FEATURE_REQUIREMENT(Execution, ExecutionAttribute)

lib/AST/FeatureSet.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ static bool usesFeatureBuiltinEmplaceTypedThrows(Decl *decl) {
466466
}
467467

468468
static bool usesFeatureExecutionAttribute(Decl *decl) {
469+
if (auto *ASD = dyn_cast<AbstractStorageDecl>(decl)) {
470+
if (auto *getter = ASD->getAccessor(AccessorKind::Get))
471+
return usesFeatureExecutionAttribute(getter);
472+
return false;
473+
}
474+
469475
if (decl->getAttrs().hasAttribute<ExecutionAttr>())
470476
return true;
471477

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
257257

258258
public:
259259
void visitExecutionAttr(ExecutionAttr *attr) {
260-
auto *F = dyn_cast<FuncDecl>(D);
260+
auto *F = dyn_cast<AbstractFunctionDecl>(D);
261261
if (!F)
262262
return;
263263

test/IDE/complete_decl_attribute_feature_requirement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct _S {
6363
@#^ON_INIT^# init()
6464
// ON_INIT: Begin completions
6565
// ON_INIT_ENABLED-DAG: Keyword/None: abi[#Constructor Attribute#]; name=abi
66-
// ON_INIT_ENABLED-NOT: Keyword/None: execution[#{{.*}} Attribute#]; name=execution
66+
// ON_INIT_ENABLED-DAG: Keyword/None: execution[#{{.*}} Attribute#]; name=execution
6767
// ON_INIT_DISABLED-NOT: Keyword/None: abi[#{{.*}} Attribute#]; name=abi
6868
// ON_INIT_DISABLED-NOT: Keyword/None: execution[#{{.*}} Attribute#]; name=execution
6969
// ON_INIT: End completions

test/ModuleInterface/execution_attr.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
// REQUIRES: swift_feature_ExecutionAttribute
77

88
public struct Test {
9+
// CHECK: #if compiler(>=5.3) && $ExecutionAttribute
10+
// CHECK-NEXT: @execution(caller) public init() async
11+
// CHECK-NEXT: #else
12+
// CHECK-NEXT: public init() async
13+
// CHECK-NEXT: #endif
14+
@execution(caller)
15+
public init() async {
16+
}
17+
918
// CHECK: #if compiler(>=5.3) && $ExecutionAttribute
1019
// CHECK-NEXT: @execution(concurrent) public func test() async
1120
// CHECK-NEXT: #else
@@ -21,5 +30,37 @@ public struct Test {
2130
// CHECK-NEXT: public func other(_: () async -> Swift.Void)
2231
// CHECK-NEXT: #endif
2332
public func other(_: @execution(caller) () async -> Void) {}
33+
34+
// CHECK: #if compiler(>=5.3) && $ExecutionAttribute
35+
// CHECK-NEXT: public var test: Swift.Int {
36+
// CHECK-NEXT: @execution(caller) get async
37+
// CHECK-NEXT: }
38+
// CHECK-NEXT: #else
39+
// CHECK-NEXT: public var test: Swift.Int {
40+
// CHECK-NEXT: get async
41+
// CHECK-NEXT: }
42+
// CHECK-NEXT: #endif
43+
public var test: Int {
44+
@execution(caller)
45+
get async {
46+
42
47+
}
48+
}
49+
50+
// CHECK: #if compiler(>=5.3) && $ExecutionAttribute
51+
// CHECK-NEXT: public subscript(x: Swift.Int) -> Swift.Bool {
52+
// CHECK-NEXT: @execution(caller) get async
53+
// CHECK-NEXT: }
54+
// CHECK-NEXT: #else
55+
// CHECK-NEXT: public subscript(x: Swift.Int) -> Swift.Bool {
56+
// CHECK-NEXT: get async
57+
// CHECK-NEXT: }
58+
// CHECK-NEXT: #endif
59+
public subscript(x: Int) -> Bool {
60+
@execution(caller)
61+
get async {
62+
false
63+
}
64+
}
2465
}
2566

test/attr/attr_execution.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
1818

1919
struct Test {
2020
@execution(concurrent) init() {}
21-
// expected-error@-1 {{@execution(concurrent) may only be used on 'func' declarations}}
21+
// expected-error@-1 {{cannot use '@execution' on non-async initializer 'init()'}}
2222

2323
@execution(concurrent) func member() {}
2424
// expected-error@-1 {{cannot use '@execution' on non-async instance method 'member()'}}
2525

2626
@execution(concurrent) func member() async {} // Ok
2727

28-
// expected-error@+1 {{@execution(caller) may only be used on 'func' declarations}}
28+
// expected-error@+1 {{'@execution(caller)' attribute cannot be applied to this declaration}}
2929
@execution(caller) subscript(a: Int) -> Bool {
3030
get { false }
3131
@execution(concurrent) set { }
32-
// expected-error@-1 {{@execution(concurrent) may only be used on 'func' declarations}}
32+
// expected-error@-1 {{cannot use '@execution' on non-async setter for subscript 'subscript(_:)'}}
3333
}
3434

3535
@execution(caller) var x: Int
36-
// expected-error@-1 {{@execution(caller) may only be used on 'func' declarations}}
36+
// expected-error@-1 {{'@execution(caller)' attribute cannot be applied to this declaration}}
3737
}
3838

3939
do {
@@ -93,3 +93,21 @@ _ = { @execution(concurrent) () -> Int in
9393
_ = { @execution(caller) (x: isolated (any Actor)?) in
9494
// expected-error@-1 {{cannot use '@execution' together with an isolated parameter}}
9595
}
96+
97+
struct TestDifferentPositions {
98+
@execution(caller)
99+
init() async {
100+
}
101+
102+
var x: Int {
103+
@execution(concurrent)
104+
get async {
105+
}
106+
}
107+
108+
subscript(x: Int) -> Bool {
109+
@execution(concurrent)
110+
get async {
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)