Skip to content

Commit 4a7b35d

Browse files
committed
Sema: Compute captures correctly for closures without source locations
Soon we'll be synthesizing more implicit autoclosures in CSApply, and this case comes up.
1 parent db357d9 commit 4a7b35d

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class FindCapturedVars : public ASTWalker {
4242
OpaqueValueExpr *OpaqueValue = nullptr;
4343
SourceLoc CaptureLoc;
4444
DeclContext *CurDC;
45-
bool NoEscape, ObjC, IsGenericFunction;
45+
bool NoEscape, ObjC;
46+
bool HasGenericParamCaptures;
4647

4748
public:
4849
FindCapturedVars(SourceLoc CaptureLoc,
@@ -51,23 +52,23 @@ class FindCapturedVars : public ASTWalker {
5152
bool ObjC,
5253
bool IsGenericFunction)
5354
: Context(CurDC->getASTContext()), CaptureLoc(CaptureLoc), CurDC(CurDC),
54-
NoEscape(NoEscape), ObjC(ObjC), IsGenericFunction(IsGenericFunction) {}
55+
NoEscape(NoEscape), ObjC(ObjC), HasGenericParamCaptures(IsGenericFunction) {}
5556

5657
CaptureInfo getCaptureInfo() const {
5758
DynamicSelfType *dynamicSelfToRecord = nullptr;
58-
bool hasGenericParamCaptures = IsGenericFunction;
5959

6060
// Only local functions capture dynamic 'Self'.
6161
if (CurDC->getParent()->isLocalContext()) {
62-
if (GenericParamCaptureLoc.isValid())
63-
hasGenericParamCaptures = true;
64-
6562
if (DynamicSelfCaptureLoc.isValid())
6663
dynamicSelfToRecord = DynamicSelf;
6764
}
6865

6966
return CaptureInfo(Context, Captures, dynamicSelfToRecord, OpaqueValue,
70-
hasGenericParamCaptures);
67+
HasGenericParamCaptures);
68+
}
69+
70+
bool hasGenericParamCaptures() const {
71+
return HasGenericParamCaptures;
7172
}
7273

7374
SourceLoc getGenericParamCaptureLoc() const {
@@ -148,17 +149,19 @@ class FindCapturedVars : public ASTWalker {
148149
if ((t->is<ArchetypeType>() ||
149150
t->is<GenericTypeParamType>()) &&
150151
!t->isOpenedExistential() &&
151-
GenericParamCaptureLoc.isInvalid()) {
152+
!HasGenericParamCaptures) {
152153
GenericParamCaptureLoc = loc;
154+
HasGenericParamCaptures = true;
153155
}
154156
}));
155157
}
156158

157159
if (auto *gft = type->getAs<GenericFunctionType>()) {
158160
TypeCaptureWalker walker(ObjC, [&](Type t) {
159161
if (t->is<GenericTypeParamType>() &&
160-
GenericParamCaptureLoc.isInvalid()) {
162+
!HasGenericParamCaptures) {
161163
GenericParamCaptureLoc = loc;
164+
HasGenericParamCaptures = true;
162165
}
163166
});
164167

@@ -339,9 +342,12 @@ class FindCapturedVars : public ASTWalker {
339342
addCapture(CapturedValue(capture.getDecl(), Flags, capture.getLoc()));
340343
}
341344

342-
if (GenericParamCaptureLoc.isInvalid())
343-
if (captureInfo.hasGenericParamCaptures())
345+
if (!HasGenericParamCaptures) {
346+
if (captureInfo.hasGenericParamCaptures()) {
344347
GenericParamCaptureLoc = loc;
348+
HasGenericParamCaptures = true;
349+
}
350+
}
345351

346352
if (DynamicSelfCaptureLoc.isInvalid()) {
347353
if (captureInfo.hasDynamicSelfCapture()) {
@@ -631,7 +637,7 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
631637

632638
// Extensions of generic ObjC functions can't use generic parameters from
633639
// their context.
634-
if (AFD && finder.getGenericParamCaptureLoc().isValid()) {
640+
if (AFD && finder.hasGenericParamCaptures()) {
635641
if (auto Clas = AFD->getParent()->getSelfClassDecl()) {
636642
if (Clas->usesObjCGenericsModel()) {
637643
AFD->diagnose(diag::objc_generic_extension_using_type_parameter);

0 commit comments

Comments
 (0)