Skip to content

[5.1][Sema] Add defensive guard for implicit use of self in closure check #22913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,10 +1524,20 @@ static void diagnoseImplicitSelfUseInClosure(TypeChecker &TC, const Expr *E,
/// Return true if this is an implicit reference to self.
static bool isImplicitSelfUse(Expr *E) {
auto *DRE = dyn_cast<DeclRefExpr>(E);
return DRE && DRE->isImplicit() && isa<VarDecl>(DRE->getDecl()) &&
cast<VarDecl>(DRE->getDecl())->isSelfParameter() &&
// Metatype self captures don't extend the lifetime of an object.
!DRE->getType()->is<MetatypeType>();

if (!DRE || !DRE->isImplicit() || !isa<VarDecl>(DRE->getDecl()) ||
!cast<VarDecl>(DRE->getDecl())->isSelfParameter())
return false;

// Defensive check for type. If the expression doesn't have type here, it
// should have been diagnosed somewhere else.
Type ty = DRE->getType();
assert(ty && "Implicit self parameter ref without type");
if (!ty)
return false;

// Metatype self captures don't extend the lifetime of an object.
return !ty->is<MetatypeType>();
}

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