File tree Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Expand file tree Collapse file tree 2 files changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -1538,6 +1538,20 @@ static bool closureHasWeakSelfCapture(const AbstractClosureExpr *ACE) {
1538
1538
return false ;
1539
1539
}
1540
1540
1541
+ // / Whether or not this closure with a `weak self` capture is permitted
1542
+ // / to use implicit self. We can't permit this for escaping closures in
1543
+ // / Swift 5 mode, because the implicit self AST in Swift 5 would cause
1544
+ // / self to be captured strongly instead of weakly.
1545
+ static bool
1546
+ allowsImplicitSelfForWeakSelfCapture (const AbstractClosureExpr *ACE) {
1547
+ if (ACE->getASTContext ().LangOpts .isSwiftVersionAtLeast (6 )) {
1548
+ return true ;
1549
+ }
1550
+
1551
+ return AnyFunctionRef (const_cast <AbstractClosureExpr *>(ACE))
1552
+ .isKnownNoEscape ();
1553
+ }
1554
+
1541
1555
// / Whether or not implicit self decls in this closure require manual
1542
1556
// / lookup in order to perform diagnostics with the semantics described
1543
1557
// / in SE-0365. This is necessary for closures that capture self weakly
@@ -1640,7 +1654,8 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
1640
1654
// let self = .someOptionalVariable else { return }` or `let self =
1641
1655
// someUnrelatedVariable`. If self hasn't been unwrapped yet and is still
1642
1656
// an optional, we would have already hit an error elsewhere.
1643
- if (closureHasWeakSelfCapture (inClosure)) {
1657
+ if (closureHasWeakSelfCapture (inClosure) &&
1658
+ allowsImplicitSelfForWeakSelfCapture (inClosure)) {
1644
1659
return !implicitWeakSelfReferenceIsValid (DRE, inClosure);
1645
1660
}
1646
1661
Original file line number Diff line number Diff line change @@ -749,21 +749,21 @@ public class TestImplicitSelfForWeakSelfCapture {
749
749
doVoidStuff { [ weak self] in
750
750
method ( ) // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
751
751
guard let self = self else { return }
752
- method ( )
752
+ method ( ) // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
753
753
}
754
754
755
755
doVoidStuff { [ weak self] in
756
756
guard let self else { return }
757
- method ( )
757
+ method ( ) // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
758
758
}
759
759
760
760
doVoidStuff { [ weak self] in
761
761
if let self = self {
762
- method ( )
762
+ method ( ) // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
763
763
}
764
764
765
765
if let self {
766
- method ( )
766
+ method ( ) // expected-error {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
767
767
}
768
768
}
769
769
You can’t perform that action at this time.
0 commit comments