@@ -4154,11 +4154,21 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
41544154 DynamicSelfType *capturesDynamicSelf = nullptr ;
41554155 OpaqueValueExpr *capturesOpaqueValue = nullptr ;
41564156
4157- std::function<void (CaptureInfo captureInfo, DeclContext *dc )> collectCaptures;
4157+ std::function<void (CaptureInfo captureInfo)> collectCaptures;
41584158 std::function<void (AnyFunctionRef)> collectFunctionCaptures;
41594159 std::function<void (SILDeclRef)> collectConstantCaptures;
41604160
4161- collectCaptures = [&](CaptureInfo captureInfo, DeclContext *dc) {
4161+ auto recordCapture = [&](CapturedValue capture) {
4162+ ValueDecl *value = capture.getDecl ();
4163+ auto existing = captures.find (value);
4164+ if (existing != captures.end ()) {
4165+ existing->second = existing->second .mergeFlags (capture);
4166+ } else {
4167+ captures.insert (std::pair<ValueDecl *, CapturedValue>(value, capture));
4168+ }
4169+ };
4170+
4171+ collectCaptures = [&](CaptureInfo captureInfo) {
41624172 assert (captureInfo.hasBeenComputed ());
41634173
41644174 if (captureInfo.hasGenericParamCaptures ())
@@ -4168,9 +4178,10 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
41684178 if (captureInfo.hasOpaqueValueCapture ())
41694179 capturesOpaqueValue = captureInfo.getOpaqueValue ();
41704180
4171- SmallVector<CapturedValue, 4 > localCaptures;
4172- captureInfo.getLocalCaptures (localCaptures);
4173- for (auto capture : localCaptures) {
4181+ for (auto capture : captureInfo.getCaptures ()) {
4182+ if (!capture.isLocalCapture ())
4183+ continue ;
4184+
41744185 // If the capture is of another local function, grab its transitive
41754186 // captures instead.
41764187 if (auto capturedFn = getAnyFunctionRefFromCapture (capture)) {
@@ -4302,13 +4313,7 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
43024313 }
43034314
43044315 // Collect non-function captures.
4305- ValueDecl *value = capture.getDecl ();
4306- auto existing = captures.find (value);
4307- if (existing != captures.end ()) {
4308- existing->second = existing->second .mergeFlags (capture);
4309- } else {
4310- captures.insert (std::pair<ValueDecl *, CapturedValue>(value, capture));
4311- }
4316+ recordCapture (capture);
43124317 }
43134318 };
43144319
@@ -4320,8 +4325,21 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
43204325 return ;
43214326
43224327 PrettyStackTraceAnyFunctionRef (" lowering local captures" , curFn);
4323- auto dc = curFn.getAsDeclContext ();
4324- collectCaptures (curFn.getCaptureInfo (), dc);
4328+ collectCaptures (curFn.getCaptureInfo ());
4329+
4330+ if (auto *afd = curFn.getAbstractFunctionDecl ()) {
4331+ // If a local function inherits isolation from the enclosing context,
4332+ // make sure we capture the isolated parameter, if we haven't already.
4333+ if (afd->isLocalCapture ()) {
4334+ auto actorIsolation = getActorIsolation (afd);
4335+ if (actorIsolation.getKind () == ActorIsolation::ActorInstance) {
4336+ if (auto *var = actorIsolation.getActorInstance ()) {
4337+ assert (isa<ParamDecl>(var));
4338+ recordCapture (CapturedValue (var, 0 , afd->getLoc ()));
4339+ }
4340+ }
4341+ }
4342+ }
43254343
43264344 // A function's captures also include its default arguments, because
43274345 // when we reference a function we don't track which default arguments
@@ -4332,7 +4350,7 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
43324350 if (auto *AFD = curFn.getAbstractFunctionDecl ()) {
43334351 for (auto *P : *AFD->getParameters ()) {
43344352 if (P->hasDefaultExpr ())
4335- collectCaptures (P->getDefaultArgumentCaptureInfo (), dc );
4353+ collectCaptures (P->getDefaultArgumentCaptureInfo ());
43364354 }
43374355 }
43384356 };
@@ -4345,10 +4363,8 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
43454363 if (auto *afd = dyn_cast<AbstractFunctionDecl>(curFn.getDecl ())) {
43464364 auto *param = getParameterAt (static_cast <ValueDecl *>(afd),
43474365 curFn.defaultArgIndex );
4348- if (param->hasDefaultExpr ()) {
4349- auto dc = afd->getInnermostDeclContext ();
4350- collectCaptures (param->getDefaultArgumentCaptureInfo (), dc);
4351- }
4366+ if (param->hasDefaultExpr ())
4367+ collectCaptures (param->getDefaultArgumentCaptureInfo ());
43524368 return ;
43534369 }
43544370
0 commit comments