@@ -178,6 +178,35 @@ static void emitIdentityInit(SILGenFunction &SGF, ConstructorDecl *ctor,
178178 initializeProperty (SGF, loc, borrowedSelfArg.getValue (), var, temp);
179179}
180180
181+ namespace {
182+ // / Cleanup to resign the identity of a distributed actor if an abnormal exit happens.
183+ class ResignIdentity : public Cleanup {
184+ ClassDecl *actorDecl;
185+ SILValue self;
186+ public:
187+ ResignIdentity (ClassDecl *actorDecl, SILValue self)
188+ : actorDecl(actorDecl), self(self) {
189+ assert (actorDecl->isDistributedActor ());
190+ }
191+
192+ void emit (SILGenFunction &SGF, CleanupLocation l, ForUnwind_t forUnwind) override {
193+ if (forUnwind == IsForUnwind) {
194+ l.markAutoGenerated ();
195+ SGF.emitResignIdentityCall (l, actorDecl,
196+ ManagedValue::forUnmanaged (self));
197+ }
198+ }
199+
200+ void dump (SILGenFunction &SGF) const override {
201+ #ifndef NDEBUG
202+ llvm::errs () << " ResignIdentity "
203+ << " State:" << getState () << " "
204+ << " Self: " << self << " \n " ;
205+ #endif
206+ }
207+ };
208+ } // end anonymous namespace
209+
181210void SILGenFunction::emitDistActorImplicitPropertyInits (
182211 ConstructorDecl *ctor, ManagedValue selfArg) {
183212 // Only designated initializers should perform this initialization.
@@ -189,6 +218,10 @@ void SILGenFunction::emitDistActorImplicitPropertyInits(
189218 selfArg = selfArg.borrow (*this , loc);
190219 emitTransportInit (*this , ctor, loc, selfArg);
191220 emitIdentityInit (*this , ctor, loc, selfArg);
221+
222+ // register a clean-up to resign the identity upon abnormal exit
223+ auto *actorDecl = cast<ClassDecl>(ctor->getParent ()->getAsDecl ());
224+ Cleanups.pushCleanup <ResignIdentity>(actorDecl, selfArg.getValue ());
192225}
193226
194227void SILGenFunction::emitDistributedActorReady (
0 commit comments