Skip to content

Commit 08156ec

Browse files
committed
[Sema] Add defensive guard for implicit use of self in closure check
Add assetion along with defensive guard to avoid crashing in release build. Hopefully, sourcekitd Stress Tester find a reproducer for the crash. rdar://problem/47895109 (cherry picked from commit ce30a72)
1 parent 4e893ec commit 08156ec

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,10 +1625,20 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
16251625
/// Return true if this is an implicit reference to self.
16261626
static bool isImplicitSelfUse(Expr *E) {
16271627
auto *DRE = dyn_cast<DeclRefExpr>(E);
1628-
return DRE && DRE->isImplicit() && isa<VarDecl>(DRE->getDecl()) &&
1629-
cast<VarDecl>(DRE->getDecl())->isSelfParameter() &&
1630-
// Metatype self captures don't extend the lifetime of an object.
1631-
!DRE->getType()->is<MetatypeType>();
1628+
1629+
if (!DRE || !DRE->isImplicit() || !isa<VarDecl>(DRE->getDecl()) ||
1630+
!cast<VarDecl>(DRE->getDecl())->isSelfParameter())
1631+
return false;
1632+
1633+
// Defensive check for type. If the expression doesn't have type here, it
1634+
// should have been diagnosed somewhere else.
1635+
Type ty = DRE->getType();
1636+
assert(ty && "Implicit self parameter ref without type");
1637+
if (!ty)
1638+
return false;
1639+
1640+
// Metatype self captures don't extend the lifetime of an object.
1641+
return !ty->is<MetatypeType>();
16321642
}
16331643

16341644
/// Return true if this is a closure expression that will require "self."

0 commit comments

Comments
 (0)