Skip to content

Commit 0cd9f19

Browse files
committed
Fix issue where 'let = MyCls()' in function body would unexpectedly permit implicit self
1 parent c6b4a20 commit 0cd9f19

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/Sema/PreCheckExpr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,15 @@ 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")) {
747+
} else if (Base->getBaseIdentifier().is("self") && Base->getDeclContext()->getContextKind() == DeclContextKind::AbstractClosureExpr) {
748748
// If this is an implicit self call, we don't actually
749749
// know if self is Self or Optional<Self> (e.g. in a weak self
750750
// closure before self is unwrapped).
751751
// - Since we don't know the correct type of self yet,
752752
// we leave this as an UnresolvedDeclRefExpr that is
753753
// 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.
754756
auto selfNameRef = new DeclNameRef(Base->getBaseIdentifier());
755757
BaseExpr = new (Context) UnresolvedDeclRefExpr(*selfNameRef,
756758
DeclRefKind::Ordinary,

0 commit comments

Comments
 (0)