@@ -744,23 +744,30 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
744
744
BaseExpr = TypeExpr::createImplicitForDecl (
745
745
UDRE->getNameLoc (), NTD, BaseDC,
746
746
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).
751
752
// - Since we don't know the correct type of self yet,
752
753
// we leave this as an UnresolvedDeclRefExpr that is
753
754
// 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
+ }
764
771
}
765
772
766
773
auto isInClosureContext = [&](ValueDecl *decl) -> bool {
0 commit comments