Skip to content

Commit 18a9987

Browse files
authored
Merge pull request swiftlang#79497 from xedin/execution-attr-diagnostics
[Sema] Either variant of `@execution` is incompatible with other isol…
2 parents 3a72221 + 613fbaf commit 18a9987

File tree

5 files changed

+70
-84
lines changed

5 files changed

+70
-84
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8300,32 +8300,27 @@ ERROR(attr_execution_type_attr_only_on_async,none,
83008300
"cannot use '@execution' on non-async function type",
83018301
())
83028302

8303-
ERROR(attr_execution_concurrent_incompatible_isolated_parameter,none,
8304-
"cannot use '@execution(concurrent)' on %kind0 because it has "
8303+
ERROR(attr_execution_incompatible_isolated_parameter,none,
8304+
"cannot use '@execution' on %kind0 because it has "
83058305
"an isolated parameter: %1",
83068306
(ValueDecl *, ValueDecl *))
83078307

8308-
ERROR(attr_execution_concurrent_incompatible_dynamically_isolated_parameter,none,
8309-
"cannot use '@execution(concurrent)' on %kind0 because it has "
8308+
ERROR(attr_execution_incompatible_dynamically_isolated_parameter,none,
8309+
"cannot use '@execution' on %kind0 because it has "
83108310
"a dynamically isolated parameter: %1",
83118311
(ValueDecl *, ValueDecl *))
83128312

8313-
ERROR(attr_execution_concurrent_incompatible_with_nonisolated,none,
8314-
"cannot use '@execution(concurrent)' and 'nonisolated' on the same %0 "
8315-
"because they serve the same purpose",
8316-
(ValueDecl *))
8317-
8318-
ERROR(attr_execution_concurrent_type_attr_incompatible_with_global_isolation,none,
8319-
"cannot use '@execution(concurrent)' because function type is "
8320-
"isolated to global actor %0",
8313+
ERROR(attr_execution_type_attr_incompatible_with_global_isolation,none,
8314+
"cannot use '@execution' because function type is "
8315+
"isolated to a global actor %0",
83218316
(Type))
83228317

8323-
ERROR(attr_execution_concurrent_type_attr_incompatible_with_isolated_param,none,
8324-
"cannot use '@execution(concurrent)' together with isolated parameter",
8318+
ERROR(attr_execution_type_attr_incompatible_with_isolated_param,none,
8319+
"cannot use '@execution' together with an isolated parameter",
83258320
())
83268321

8327-
ERROR(attr_execution_concurrent_type_attr_incompatible_with_isolated_any,none,
8328-
"cannot use '@execution(concurrent)' together with @isolated(any)",
8322+
ERROR(attr_execution_type_attr_incompatible_with_isolated_any,none,
8323+
"cannot use '@execution' together with @isolated(any)",
83298324
())
83308325

83318326

lib/Sema/TypeCheckAttr.cpp

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -263,45 +263,31 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
263263
return;
264264
}
265265

266-
switch (attr->getBehavior()) {
267-
case ExecutionKind::Concurrent: {
268-
auto parameters = F->getParameters();
269-
if (!parameters)
270-
return;
266+
auto parameters = F->getParameters();
267+
if (!parameters)
268+
return;
271269

272-
for (auto *P : *parameters) {
273-
auto *repr = P->getTypeRepr();
274-
if (!repr)
275-
continue;
270+
for (auto *P : *parameters) {
271+
auto *repr = P->getTypeRepr();
272+
if (!repr)
273+
continue;
276274

277-
// isolated parameters affect isolation of the function itself
278-
if (isa<IsolatedTypeRepr>(repr)) {
275+
// isolated parameters affect isolation of the function itself
276+
if (isa<IsolatedTypeRepr>(repr)) {
277+
diagnoseAndRemoveAttr(
278+
attr, diag::attr_execution_incompatible_isolated_parameter, F, P);
279+
return;
280+
}
281+
282+
if (auto *attrType = dyn_cast<AttributedTypeRepr>(repr)) {
283+
if (attrType->has(TypeAttrKind::Isolated)) {
279284
diagnoseAndRemoveAttr(
280285
attr,
281-
diag::attr_execution_concurrent_incompatible_isolated_parameter,
286+
diag::attr_execution_incompatible_dynamically_isolated_parameter,
282287
F, P);
283288
return;
284289
}
285-
286-
if (auto *attrType = dyn_cast<AttributedTypeRepr>(repr)) {
287-
if (attrType->has(TypeAttrKind::Isolated)) {
288-
diagnoseAndRemoveAttr(
289-
attr,
290-
diag::
291-
attr_execution_concurrent_incompatible_dynamically_isolated_parameter,
292-
F, P);
293-
return;
294-
}
295-
}
296290
}
297-
298-
break;
299-
}
300-
301-
case ExecutionKind::Caller: {
302-
// no restrictions for now.
303-
break;
304-
}
305291
}
306292
}
307293

lib/Sema/TypeCheckType.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4179,37 +4179,32 @@ NeverNullType TypeResolver::resolveASTFunctionType(
41794179
diag::attr_execution_type_attr_only_on_async);
41804180
}
41814181

4182-
if (executionAttr->getBehavior() == ExecutionKind::Concurrent) {
4183-
switch (isolation.getKind()) {
4184-
case FunctionTypeIsolation::Kind::NonIsolated:
4185-
break;
4182+
switch (isolation.getKind()) {
4183+
case FunctionTypeIsolation::Kind::NonIsolated:
4184+
break;
41864185

4187-
case FunctionTypeIsolation::Kind::GlobalActor:
4188-
diagnoseInvalid(
4189-
repr, executionAttr->getAtLoc(),
4190-
diag::
4191-
attr_execution_concurrent_type_attr_incompatible_with_global_isolation,
4192-
isolation.getGlobalActorType());
4193-
break;
4186+
case FunctionTypeIsolation::Kind::GlobalActor:
4187+
diagnoseInvalid(
4188+
repr, executionAttr->getAtLoc(),
4189+
diag::attr_execution_type_attr_incompatible_with_global_isolation,
4190+
isolation.getGlobalActorType());
4191+
break;
41944192

4195-
case FunctionTypeIsolation::Kind::Parameter:
4196-
diagnoseInvalid(
4197-
repr, executionAttr->getAtLoc(),
4198-
diag::
4199-
attr_execution_concurrent_type_attr_incompatible_with_isolated_param);
4200-
break;
4193+
case FunctionTypeIsolation::Kind::Parameter:
4194+
diagnoseInvalid(
4195+
repr, executionAttr->getAtLoc(),
4196+
diag::attr_execution_type_attr_incompatible_with_isolated_param);
4197+
break;
42014198

4202-
case FunctionTypeIsolation::Kind::Erased:
4203-
diagnoseInvalid(
4204-
repr, executionAttr->getAtLoc(),
4205-
diag::
4206-
attr_execution_concurrent_type_attr_incompatible_with_isolated_any);
4207-
break;
4199+
case FunctionTypeIsolation::Kind::Erased:
4200+
diagnoseInvalid(
4201+
repr, executionAttr->getAtLoc(),
4202+
diag::attr_execution_type_attr_incompatible_with_isolated_any);
4203+
break;
42084204

4209-
case FunctionTypeIsolation::Kind::NonIsolatedCaller:
4210-
llvm_unreachable("cannot happen because multiple @execution attributes "
4211-
"aren't allowed.");
4212-
}
4205+
case FunctionTypeIsolation::Kind::NonIsolatedCaller:
4206+
llvm_unreachable("cannot happen because multiple @execution attributes "
4207+
"aren't allowed.");
42134208
}
42144209

42154210
if (!repr->isInvalid()) {

test/Parse/execution.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,23 @@ typealias E = @execution(concurrent) () -> Void
88
func test1(_: @execution(caller) (Int...) async -> Void) {}
99
func test2(_: @execution(concurrent) (Int...) async -> Void) {}
1010

11-
func test_err1(_: @execution(concurrent) @MainActor () async -> Void) {}
12-
// expected-error@-1 {{cannot use '@execution(concurrent)' because function type is isolated to global actor 'MainActor'}}
11+
func test_err1_concurrent(_: @execution(concurrent) @MainActor () async -> Void) {}
12+
// expected-error@-1 {{cannot use '@execution' because function type is isolated to a global actor 'MainActor'}}
1313

14-
func test_err2(_: @execution(concurrent) @isolated(any) () async -> Void) {}
15-
// expected-error@-1 {{cannot use '@execution(concurrent)' together with @isolated(any)}}
14+
func test_err1_caller(_: @execution(caller) @MainActor () async -> Void) {}
15+
// expected-error@-1 {{cannot use '@execution' because function type is isolated to a global actor 'MainActor'}}
1616

17-
func test_err3(_: @execution(concurrent) (isolated (any Actor)?) async -> Void) {}
18-
// expected-error@-1 {{cannot use '@execution(concurrent)' together with isolated parameter}}
17+
func test_err2_concurrent(_: @execution(concurrent) @isolated(any) () async -> Void) {}
18+
// expected-error@-1 {{cannot use '@execution' together with @isolated(any)}}
19+
20+
func test_err2_caller(_: @execution(caller) @isolated(any) () async -> Void) {}
21+
// expected-error@-1 {{cannot use '@execution' together with @isolated(any)}}
22+
23+
func test_err3_concurrent(_: @execution(concurrent) (isolated (any Actor)?) async -> Void) {}
24+
// expected-error@-1 {{cannot use '@execution' together with an isolated parameter}}
25+
26+
func test_err3_caller(_: @execution(caller) (isolated (any Actor)?) async -> Void) {}
27+
// expected-error@-1 {{cannot use '@execution' together with an isolated parameter}}
1928

2029
func test_err4(_: @execution (Int) -> Void) {}
2130
// expected-error@-1 {{expected 'concurrent' or 'caller' as the execution behavior}}

test/attr/attr_execution.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,20 @@ struct TestAttributeCollisions {
4444
@execution(concurrent) nonisolated func testNonIsolated() async {}
4545

4646
@execution(concurrent) func test(arg: isolated MainActor) async {}
47-
// expected-error@-1 {{cannot use '@execution(concurrent)' on instance method 'test(arg:)' because it has an isolated parameter: 'arg'}}
47+
// expected-error@-1 {{cannot use '@execution' on instance method 'test(arg:)' because it has an isolated parameter: 'arg'}}
4848

4949
@execution(concurrent) func testIsolationAny(arg: @isolated(any) () -> Void) async {}
50-
// expected-error@-1 {{cannot use '@execution(concurrent)' on instance method 'testIsolationAny(arg:)' because it has a dynamically isolated parameter: 'arg'}}
50+
// expected-error@-1 {{cannot use '@execution' on instance method 'testIsolationAny(arg:)' because it has a dynamically isolated parameter: 'arg'}}
5151

5252
@MainActor @execution(concurrent) func testGlobalActor() async {}
5353
// expected-warning @-1 {{instance method 'testGlobalActor()' has multiple actor-isolation attributes ('MainActor' and 'execution(concurrent)')}}
5454

5555
@execution(caller) nonisolated func testNonIsolatedCaller() async {} // Ok
5656
@MainActor @execution(caller) func testGlobalActorCaller() async {}
5757
// expected-warning@-1 {{instance method 'testGlobalActorCaller()' has multiple actor-isolation attributes ('MainActor' and 'execution(caller)')}}
58-
@execution(caller) func testCaller(arg: isolated MainActor) async {} // Ok
59-
58+
@execution(caller) func testCaller(arg: isolated MainActor) async {}
59+
// expected-error@-1 {{cannot use '@execution' on instance method 'testCaller(arg:)' because it has an isolated parameter: 'arg'}}
60+
6061
@execution(concurrent) @Sendable func test(_: @Sendable () -> Void, _: sending Int) async {} // Ok
6162
@execution(caller) @Sendable func testWithSendableCaller(_: @Sendable () -> Void, _: sending Int) async {} // Ok
6263
}

0 commit comments

Comments
 (0)