Skip to content

Commit 5cb418a

Browse files
authored
Merge pull request #71511 from hborla/explicit-isolation-expansion
[Concurrency] Make `#isolation` macro expansions explicit.
2 parents 8ac0d1f + 1875817 commit 5cb418a

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3512,6 +3512,12 @@ namespace {
35123512
auto isolation = getActorIsolationOfContext(
35133513
const_cast<DeclContext *>(getDeclContext()),
35143514
getClosureActorIsolation);
3515+
auto *dc = const_cast<DeclContext *>(getDeclContext());
3516+
3517+
// Note that macro expansions are never implicit. They have
3518+
// valid source locations in their macro expansion buffer, they
3519+
// do not cause implicit 'self' capture diagnostics, etc.
3520+
35153521
Expr *actorExpr = nullptr;
35163522
Type optionalAnyActorType = isolationExpr->getType();
35173523
switch (isolation) {
@@ -3533,7 +3539,7 @@ namespace {
35333539
}
35343540
actorExpr = new (ctx) DeclRefExpr(
35353541
const_cast<VarDecl *>(var), DeclNameLoc(loc),
3536-
/*Implicit=*/true);
3542+
/*implicit=*/false);
35373543

35383544
// For a distributed actor, we need to retrieve the local
35393545
// actor.
@@ -3547,10 +3553,11 @@ namespace {
35473553
// Form a <global actor type>.shared reference.
35483554
Type globalActorType = getDeclContext()->mapTypeIntoContext(
35493555
isolation.getGlobalActor());
3550-
auto typeExpr = TypeExpr::createImplicit(globalActorType, ctx);
3556+
auto typeExpr = TypeExpr::createForDecl(
3557+
DeclNameLoc(loc), globalActorType->getAnyNominal(), dc);
35513558
actorExpr = new (ctx) UnresolvedDotExpr(
3552-
typeExpr, loc, DeclNameRef(ctx.Id_shared), DeclNameLoc(),
3553-
/*implicit=*/true);
3559+
typeExpr, loc, DeclNameRef(ctx.Id_shared), DeclNameLoc(loc),
3560+
/*implicit=*/false);
35543561
break;
35553562
}
35563563

@@ -3560,14 +3567,14 @@ namespace {
35603567
case ActorIsolation::Unspecified:
35613568
case ActorIsolation::Nonisolated:
35623569
case ActorIsolation::NonisolatedUnsafe:
3563-
actorExpr = new (ctx) NilLiteralExpr(loc, /*implicit=*/true);
3570+
actorExpr = new (ctx) NilLiteralExpr(loc, /*implicit=*/false);
35643571
break;
35653572
}
35663573

35673574

35683575
// Convert the actor argument to the appropriate type.
35693576
(void)TypeChecker::typeCheckExpression(
3570-
actorExpr, const_cast<DeclContext *>(getDeclContext()),
3577+
actorExpr, dc,
35713578
constraints::ContextualTypeInfo(
35723579
optionalAnyActorType, CTP_CallArgument));
35733580

test/Concurrency/isolation_macro.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension A {
3434
// CHECK: rewritten=current_context_isolation_expr
3535
// CHECK-NEXT: inject_into_optional
3636
// CHECK-NEXT: erasure_expr
37-
// CHECK: declref_expr implicit type="A"{{.*}}self@
37+
// CHECK: declref_expr type="A"{{.*}}self@
3838
_ = #isolation
3939
}
4040
}
@@ -46,7 +46,7 @@ func actorIsolationToParam(_ isolatedParam: isolated A) {
4646
// CHECK: rewritten=current_context_isolation_expr
4747
// CHECK-NEXT: inject_into_optional
4848
// CHECK-NEXT: erasure_expr
49-
// CHECK: declref_expr implicit type="A"{{.*}}isolatedParam@
49+
// CHECK: declref_expr type="A"{{.*}}isolatedParam@
5050
_ = #isolation
5151
}
5252

@@ -58,8 +58,8 @@ func mainActorIsolated() {
5858
// CHECK: rewritten=current_context_isolation_expr
5959
// CHECK-NEXT: inject_into_optional
6060
// CHECK-NEXT: erasure_expr
61-
// CHECK: member_ref_expr implicit type="MainActor" decl="_Concurrency.(file).MainActor.shared"
62-
// CHECK-NEXT: type_expr implicit type="MainActor.Type"
61+
// CHECK: member_ref_expr type="MainActor" location=@__swiftmacro_{{.*}} decl="_Concurrency.(file).MainActor.shared"
62+
// CHECK-NEXT: type_expr type="MainActor.Type"
6363
_ = #isolation
6464
}
6565

@@ -73,9 +73,24 @@ func closureIsolatedToOuterParam(_ isolatedParam: isolated A) {
7373
// CHECK: rewritten=current_context_isolation_expr
7474
// CHECK-NEXT: inject_into_optional
7575
// CHECK-NEXT: erasure_expr
76-
// CHECK: declref_expr implicit type="A"{{.*}}isolatedParam@
76+
// CHECK: declref_expr type="A"{{.*}}isolatedParam@
7777
acceptClosure {
7878
_ = #isolation
7979
print(isolatedParam)
8080
}
8181
}
82+
83+
func acceptEscapingClosure(_ fn: @escaping () -> Void) { }
84+
85+
@available(SwiftStdlib 5.1, *)
86+
extension A {
87+
func f() {
88+
// Make sure this doesn't diagnose a use of implicit 'self'
89+
acceptEscapingClosure {
90+
_ = #isolation
91+
self.g()
92+
}
93+
}
94+
95+
func g() {}
96+
}

test/Distributed/isolation_macro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extension DistributedActor {
1717
// CHECK: rewritten=current_context_isolation_expr
1818
// CHECK-NEXT: inject_into_optional
1919
// CHECK: member_ref_expr{{.*}}asLocalActor
20-
// CHECK: declref_expr implicit type="Self"
20+
// CHECK: declref_expr type="Self"
2121
_ = #isolation
2222
}
2323
}

0 commit comments

Comments
 (0)