@@ -325,11 +325,8 @@ class FindCapturedVars : public ASTWalker {
325
325
return { false , DRE };
326
326
}
327
327
328
- void propagateCaptures (AnyFunctionRef innerClosure, SourceLoc captureLoc) {
329
- TypeChecker::computeCaptures (innerClosure);
330
-
331
- auto &captureInfo = innerClosure.getCaptureInfo ();
332
-
328
+ void propagateCaptures (const CaptureInfo &captureInfo,
329
+ SourceLoc loc) {
333
330
for (auto capture : captureInfo.getCaptures ()) {
334
331
// If the decl was captured from us, it isn't captured *by* us.
335
332
if (capture.getDecl ()->getDeclContext () == CurDC)
@@ -347,18 +344,19 @@ class FindCapturedVars : public ASTWalker {
347
344
if (!NoEscape)
348
345
Flags &= ~CapturedValue::IsNoEscape;
349
346
350
- addCapture (CapturedValue (capture.getDecl (), Flags, captureLoc ));
347
+ addCapture (CapturedValue (capture.getDecl (), Flags, capture. getLoc () ));
351
348
}
352
349
353
350
if (GenericParamCaptureLoc.isInvalid ())
354
351
if (captureInfo.hasGenericParamCaptures ())
355
- GenericParamCaptureLoc = innerClosure. getLoc () ;
352
+ GenericParamCaptureLoc = loc ;
356
353
357
- if (DynamicSelfCaptureLoc.isInvalid ())
354
+ if (DynamicSelfCaptureLoc.isInvalid ()) {
358
355
if (captureInfo.hasDynamicSelfCapture ()) {
359
- DynamicSelfCaptureLoc = innerClosure. getLoc () ;
356
+ DynamicSelfCaptureLoc = loc ;
360
357
DynamicSelf = captureInfo.getDynamicSelfType ();
361
358
}
359
+ }
362
360
363
361
if (!OpaqueValue) {
364
362
if (captureInfo.hasOpaqueValueCapture ())
@@ -368,13 +366,13 @@ class FindCapturedVars : public ASTWalker {
368
366
369
367
bool walkToDeclPre (Decl *D) override {
370
368
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(D)) {
371
- propagateCaptures (AFD, AFD->getLoc ());
369
+ TypeChecker::computeCaptures (AFD);
370
+ propagateCaptures (AFD->getCaptureInfo (), AFD->getLoc ());
372
371
373
- // Can default parameter initializers capture state? That seems like
374
- // a really bad idea.
375
- for (auto *param : *AFD->getParameters ())
376
- if (auto E = param->getDefaultValue ())
377
- E->walk (*this );
372
+ for (auto *P : *AFD->getParameters ())
373
+ if (P->getDefaultValue ())
374
+ propagateCaptures (P->getDefaultArgumentCaptureInfo (),
375
+ P->getLoc ());
378
376
379
377
return false ;
380
378
}
@@ -569,7 +567,8 @@ class FindCapturedVars : public ASTWalker {
569
567
// list computed; we just propagate it, filtering out stuff that they
570
568
// capture from us.
571
569
if (auto *SubCE = dyn_cast<AbstractClosureExpr>(E)) {
572
- propagateCaptures (SubCE, SubCE->getStartLoc ());
570
+ TypeChecker::computeCaptures (SubCE);
571
+ propagateCaptures (SubCE->getCaptureInfo (), SubCE->getLoc ());
573
572
return { false , E };
574
573
}
575
574
@@ -609,17 +608,36 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
609
608
finder.checkType (AFR.getType (), AFR.getLoc ());
610
609
}
611
610
612
- auto captures = finder.getCaptureInfo ();
613
-
614
611
// A generic function always captures outer generic parameters.
612
+ bool isGeneric = false ;
615
613
auto *AFD = AFR.getAbstractFunctionDecl ();
616
- if (AFD) {
617
- if (AFD->getGenericParams ())
618
- captures.setGenericParamCaptures (true );
619
- }
614
+ if (AFD)
615
+ isGeneric = AFD->isGeneric ();
620
616
617
+ auto captures = finder.getCaptureInfo ();
618
+ if (isGeneric)
619
+ captures.setGenericParamCaptures (true );
621
620
AFR.setCaptureInfo (captures);
622
621
622
+ // Compute captures for default argument expressions.
623
+ if (auto *AFD = AFR.getAbstractFunctionDecl ()) {
624
+ for (auto *P : *AFD->getParameters ()) {
625
+ if (auto E = P->getDefaultValue ()) {
626
+ FindCapturedVars finder (Context,
627
+ E->getLoc (),
628
+ AFD,
629
+ /* isNoEscape=*/ false ,
630
+ /* isObjC=*/ false );
631
+ E->walk (finder);
632
+
633
+ auto captures = finder.getCaptureInfo ();
634
+ if (isGeneric)
635
+ captures.setGenericParamCaptures (true );
636
+ P->setDefaultArgumentCaptureInfo (captures);
637
+ }
638
+ }
639
+ }
640
+
623
641
// Extensions of generic ObjC functions can't use generic parameters from
624
642
// their context.
625
643
if (AFD && finder.getGenericParamCaptureLoc ().isValid ()) {
0 commit comments