Skip to content

Commit 27fd53f

Browse files
authored
Merge pull request #76198 from hborla/isolation-macro-mismatch
[Concurrency] Don't crash if a `#isolation` default argument has a type mismatch.
2 parents f26b7ee + 6f5a64c commit 27fd53f

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,7 +3986,7 @@ namespace {
39863986
// do not cause implicit 'self' capture diagnostics, etc.
39873987

39883988
Expr *actorExpr = nullptr;
3989-
Type optionalAnyActorType = isolationExpr->getType();
3989+
Type isolationType = isolationExpr->getType();
39903990
switch (isolation) {
39913991
case ActorIsolation::ActorInstance: {
39923992
if (auto *instance = isolation.getActorInstanceExpr()) {
@@ -4040,10 +4040,16 @@ namespace {
40404040

40414041

40424042
// Convert the actor argument to the appropriate type.
4043-
(void)TypeChecker::typeCheckExpression(
4043+
auto result = TypeChecker::typeCheckExpression(
40444044
actorExpr, dc,
40454045
constraints::ContextualTypeInfo(
4046-
optionalAnyActorType, CTP_CallArgument));
4046+
isolationType, CTP_CallArgument));
4047+
4048+
// Don't set the actor if there's a type mismatch. The isolation
4049+
// checker will treat calls using this #isolation value for an
4050+
// isolated argument as crossing an isolation boundary.
4051+
if (!result)
4052+
return;
40474053

40484054
isolationExpr->setActor(actorExpr);
40494055
}

test/Concurrency/isolation_macro.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ extension A {
100100
}
101101

102102
#if TEST_DIAGNOSTICS
103+
@available(SwiftStdlib 5.1, *)
104+
actor ConcreteActor {}
105+
106+
@available(SwiftStdlib 5.1, *)
107+
func concreteActorIsolation(
108+
actor: isolated ConcreteActor = #isolation
109+
) async {}
110+
103111
@available(SwiftStdlib 5.1, *)
104112
@MainActor
105113
func testContextualType() {
@@ -111,5 +119,10 @@ func testContextualType() {
111119
// CHECK-DIAGS: note: in expansion of macro 'isolation' here
112120
// CHECK-DIAGS: let _: Int = #isolation
113121
let _: Int = #isolation
122+
123+
// CHECK-DIAGS: error: cannot convert value of type 'MainActor' to expected argument type 'ConcreteActor'
124+
// CHECK-DIAGS: note: in expansion of macro 'isolation' here
125+
// CHECK-DIAGS: await concreteActorIsolation()
126+
await concreteActorIsolation()
114127
}
115128
#endif

0 commit comments

Comments
 (0)