@@ -369,20 +369,24 @@ ValueDecl *UnqualifiedLookupFactory::lookupBaseDecl(const DeclContext *baseDC) c
369
369
return nullptr ;
370
370
}
371
371
372
- auto selfDecl = ASTScope::lookupSingleLocalDecl (
373
- DC->getParentSourceFile (), DeclName (Ctx.Id_self ), Loc);
374
-
375
- if (!selfDecl) {
376
- return nullptr ;
377
- }
378
-
379
372
bool capturesSelfWeakly = false ;
380
373
if (auto decl = closureExpr->getCapturedSelfDecl ()) {
381
374
if (auto a = decl->getAttrs ().getAttribute <ReferenceOwnershipAttr>()) {
382
375
capturesSelfWeakly = a->get () == ReferenceOwnership::Weak;
383
376
}
384
377
}
385
378
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
+
386
390
// In Swift 5 mode, implicit self is allowed within non-escaping
387
391
// `weak self` closures even before self is unwrapped.
388
392
// For example, this is allowed:
@@ -407,7 +411,7 @@ ValueDecl *UnqualifiedLookupFactory::lookupBaseDecl(const DeclContext *baseDC) c
407
411
// In these cases, using the Swift 6 lookup behavior doesn't affect
408
412
// how the body is type-checked, so it can be used in Swift 5 mode
409
413
// without breaking source compatibility for non-escaping closures.
410
- if (capturesSelfWeakly && !Ctx.LangOpts .isSwiftVersionAtLeast (6 ) &&
414
+ if (!Ctx.LangOpts .isSwiftVersionAtLeast (6 ) &&
411
415
!implicitSelfReferenceIsUnwrapped (selfDecl)) {
412
416
return nullptr ;
413
417
}
0 commit comments