Skip to content

Commit 18aa7d8

Browse files
committed
[ast] Add a helper method ActorIsolation::getActorOrNullPtr().
ActorIsolation::getActor() asserts if an actor cannot be found. This new helper method just returns nullptr instead.
1 parent 465bb23 commit 18aa7d8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

include/swift/AST/ActorIsolation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class ActorIsolation {
192192
}
193193

194194
NominalTypeDecl *getActor() const;
195+
NominalTypeDecl *getActorOrNullPtr() const;
195196

196197
VarDecl *getActorInstance() const;
197198

lib/AST/Decl.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11348,6 +11348,31 @@ NominalTypeDecl *ActorIsolation::getActor() const {
1134811348
return actorInstance.get<NominalTypeDecl *>();
1134911349
}
1135011350

11351+
NominalTypeDecl *ActorIsolation::getActorOrNullPtr() const {
11352+
if (getKind() != ActorInstance || getKind() != GlobalActor)
11353+
return nullptr;
11354+
11355+
if (silParsed)
11356+
return nullptr;
11357+
11358+
Type actorType;
11359+
11360+
if (auto *instance = actorInstance.dyn_cast<VarDecl *>()) {
11361+
actorType = instance->getTypeInContext();
11362+
} else if (auto *instance = actorInstance.dyn_cast<Expr *>()) {
11363+
actorType = instance->getType();
11364+
}
11365+
11366+
if (actorType) {
11367+
if (auto wrapped = actorType->getOptionalObjectType()) {
11368+
actorType = wrapped;
11369+
}
11370+
return actorType->getReferenceStorageReferent()->getAnyActor();
11371+
}
11372+
11373+
return actorInstance.get<NominalTypeDecl *>();
11374+
}
11375+
1135111376
VarDecl *ActorIsolation::getActorInstance() const {
1135211377
assert(getKind() == ActorInstance);
1135311378

0 commit comments

Comments
 (0)