Skip to content

Commit 0a5e4e0

Browse files
committed
Fix incorrect DeclContext check
1 parent 0cd9f19 commit 0a5e4e0

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

lib/Sema/PreCheckExpr.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -744,23 +744,30 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
744744
BaseExpr = TypeExpr::createImplicitForDecl(
745745
UDRE->getNameLoc(), NTD, BaseDC,
746746
DC->mapTypeIntoContext(NTD->getInterfaceType()));
747-
} else if (Base->getBaseIdentifier().is("self") && Base->getDeclContext()->getContextKind() == DeclContextKind::AbstractClosureExpr) {
748-
// If this is an implicit self call, we don't actually
749-
// know if self is Self or Optional<Self> (e.g. in a weak self
750-
// closure before self is unwrapped).
747+
} else {
748+
// If this is an implicit self reference, the `VarDecl` will
749+
// currently have a non-optional type. But at this point,
750+
// we don't actually know if this is corret (it could be a
751+
// weak self capture that hasn't been unwrapped yet).
751752
// - Since we don't know the correct type of self yet,
752753
// we leave this as an UnresolvedDeclRefExpr that is
753754
// populated with the actual type later.
754-
// - We only do this within a closure context, since that's
755-
// the only place where self can be rebound.
756-
auto selfNameRef = new DeclNameRef(Base->getBaseIdentifier());
757-
BaseExpr = new (Context) UnresolvedDeclRefExpr(*selfNameRef,
758-
DeclRefKind::Ordinary,
759-
UDRE->getNameLoc());
760-
BaseExpr->setImplicit();
761-
} else {
762-
BaseExpr = new (Context) DeclRefExpr(Base, UDRE->getNameLoc(),
763-
/*Implicit=*/true);
755+
bool isClosureImplicitSelfParameter = false;
756+
if (DC->getContextKind() == DeclContextKind::AbstractClosureExpr)
757+
if (auto varDecl = dyn_cast<VarDecl>(Base))
758+
if (varDecl->isSelfParameter())
759+
isClosureImplicitSelfParameter = true;
760+
761+
if (isClosureImplicitSelfParameter) {
762+
auto selfNameRef = new DeclNameRef(Base->getBaseIdentifier());
763+
BaseExpr = new (Context) UnresolvedDeclRefExpr(*selfNameRef,
764+
DeclRefKind::Ordinary,
765+
UDRE->getNameLoc());
766+
BaseExpr->setImplicit();
767+
} else {
768+
BaseExpr = new (Context) DeclRefExpr(Base, UDRE->getNameLoc(),
769+
/*Implicit=*/true);
770+
}
764771
}
765772

766773
auto isInClosureContext = [&](ValueDecl *decl) -> bool {

0 commit comments

Comments
 (0)