Skip to content

Commit fa5d493

Browse files
authored
Merge pull request #22912 from rintaro/5.0-sema-implicitselfinclosure-rdar47895109
[5.0][Sema] Add defensive guard for implicit use of self in closure check
2 parents 4e893ec + 08156ec commit fa5d493

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)