Skip to content

Commit 592462b

Browse files
authored
Merge pull request #74260 from hamishknight/another-implicit-self-fix
[Sema] Restore 5.10 implicit self behavior prior to Swift 6 mode
2 parents 04db12f + fe272e6 commit 592462b

File tree

6 files changed

+349
-270
lines changed

6 files changed

+349
-270
lines changed

lib/AST/UnqualifiedLookup.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,24 @@ ValueDecl *UnqualifiedLookupFactory::lookupBaseDecl(const DeclContext *baseDC) c
369369
return nullptr;
370370
}
371371

372-
auto selfDecl = ASTScope::lookupSingleLocalDecl(
373-
DC->getParentSourceFile(), DeclName(Ctx.Id_self), Loc);
374-
375-
if (!selfDecl) {
376-
return nullptr;
377-
}
378-
379372
bool capturesSelfWeakly = false;
380373
if (auto decl = closureExpr->getCapturedSelfDecl()) {
381374
if (auto a = decl->getAttrs().getAttribute<ReferenceOwnershipAttr>()) {
382375
capturesSelfWeakly = a->get() == ReferenceOwnership::Weak;
383376
}
384377
}
385378

379+
// Previously we didn't perform the lookup of 'self' for anything outside
380+
// of a '[weak self]' closure, maintain that behavior until Swift 6 mode.
381+
if (!Ctx.LangOpts.isSwiftVersionAtLeast(6) && !capturesSelfWeakly)
382+
return nullptr;
383+
384+
auto selfDecl = ASTScope::lookupSingleLocalDecl(DC->getParentSourceFile(),
385+
DeclName(Ctx.Id_self), Loc);
386+
if (!selfDecl) {
387+
return nullptr;
388+
}
389+
386390
// In Swift 5 mode, implicit self is allowed within non-escaping
387391
// `weak self` closures even before self is unwrapped.
388392
// For example, this is allowed:
@@ -407,7 +411,7 @@ ValueDecl *UnqualifiedLookupFactory::lookupBaseDecl(const DeclContext *baseDC) c
407411
// In these cases, using the Swift 6 lookup behavior doesn't affect
408412
// how the body is type-checked, so it can be used in Swift 5 mode
409413
// without breaking source compatibility for non-escaping closures.
410-
if (capturesSelfWeakly && !Ctx.LangOpts.isSwiftVersionAtLeast(6) &&
414+
if (!Ctx.LangOpts.isSwiftVersionAtLeast(6) &&
411415
!implicitSelfReferenceIsUnwrapped(selfDecl)) {
412416
return nullptr;
413417
}

0 commit comments

Comments
 (0)