Skip to content

Commit 7710d63

Browse files
authored
Merge pull request #22913 from rintaro/5.1-sema-implicitselfinclosure-rdar47895109
[5.1][Sema] Add defensive guard for implicit use of self in closure check
2 parents 9dbe676 + 413d7eb commit 7710d63

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
@@ -1524,10 +1524,20 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
15241524
/// Return true if this is an implicit reference to self.
15251525
static bool isImplicitSelfUse(Expr *E) {
15261526
auto *DRE = dyn_cast<DeclRefExpr>(E);
1527-
return DRE && DRE->isImplicit() && isa<VarDecl>(DRE->getDecl()) &&
1528-
cast<VarDecl>(DRE->getDecl())->isSelfParameter() &&
1529-
// Metatype self captures don't extend the lifetime of an object.
1530-
!DRE->getType()->is<MetatypeType>();
1527+
1528+
if (!DRE || !DRE->isImplicit() || !isa<VarDecl>(DRE->getDecl()) ||
1529+
!cast<VarDecl>(DRE->getDecl())->isSelfParameter())
1530+
return false;
1531+
1532+
// Defensive check for type. If the expression doesn't have type here, it
1533+
// should have been diagnosed somewhere else.
1534+
Type ty = DRE->getType();
1535+
assert(ty && "Implicit self parameter ref without type");
1536+
if (!ty)
1537+
return false;
1538+
1539+
// Metatype self captures don't extend the lifetime of an object.
1540+
return !ty->is<MetatypeType>();
15311541
}
15321542

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

0 commit comments

Comments
 (0)