Skip to content

Commit b138c2c

Browse files
Updated logic for diagnosting implicit self capture to handle immutable weak capture (minor source-breaking change)
1 parent 4aa2847 commit b138c2c

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

include/swift/AST/Stmt.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,7 @@ class alignas(1 << PatternAlignInBits) StmtConditionElement {
729729
/// RHS of the self condition references a var defined in a capture list.
730730
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
731731
/// the self condition is a `LoadExpr`.
732-
bool rebindsSelf(ASTContext &Ctx, bool requiresCaptureListRef = false,
733-
bool requireLoadExpr = false) const;
732+
bool rebindsSelf(ASTContext &Ctx, bool requiresCaptureListRef = false) const;
734733

735734
SourceLoc getStartLoc() const;
736735
SourceLoc getEndLoc() const;
@@ -841,8 +840,7 @@ class LabeledConditionalStmt : public LabeledStmt {
841840
/// RHS of the self condition references a var defined in a capture list.
842841
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
843842
/// the self condition is a `LoadExpr`.
844-
bool rebindsSelf(ASTContext &Ctx, bool requiresCaptureListRef = false,
845-
bool requireLoadExpr = false) const;
843+
bool rebindsSelf(ASTContext &Ctx, bool requiresCaptureListRef = false) const;
846844

847845
static bool classof(const Stmt *S) {
848846
return S->getKind() >= StmtKind::First_LabeledConditionalStmt &&

lib/AST/Stmt.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,9 @@ void LabeledConditionalStmt::setCond(StmtCondition e) {
517517
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
518518
/// the self condition is a `LoadExpr`.
519519
bool LabeledConditionalStmt::rebindsSelf(ASTContext &Ctx,
520-
bool requiresCaptureListRef,
521-
bool requireLoadExpr) const {
522-
return llvm::any_of(getCond(), [&Ctx, requiresCaptureListRef,
523-
requireLoadExpr](const auto &cond) {
524-
return cond.rebindsSelf(Ctx, requiresCaptureListRef, requireLoadExpr);
520+
bool requiresCaptureListRef) const {
521+
return llvm::any_of(getCond(), [&Ctx, requiresCaptureListRef](const auto &cond) {
522+
return cond.rebindsSelf(Ctx, requiresCaptureListRef);
525523
});
526524
}
527525

@@ -532,8 +530,7 @@ bool LabeledConditionalStmt::rebindsSelf(ASTContext &Ctx,
532530
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
533531
/// the self condition is a `LoadExpr`.
534532
bool StmtConditionElement::rebindsSelf(ASTContext &Ctx,
535-
bool requiresCaptureListRef,
536-
bool requireLoadExpr) const {
533+
bool requiresCaptureListRef) const {
537534
auto pattern = getPatternOrNull();
538535
if (!pattern) {
539536
return false;
@@ -561,10 +558,6 @@ bool StmtConditionElement::rebindsSelf(ASTContext &Ctx,
561558
return false;
562559
}
563560

564-
if (requireLoadExpr && !isa<LoadExpr>(exprToCheckForDRE)) {
565-
return false;
566-
}
567-
568561
if (auto *load = dyn_cast<LoadExpr>(exprToCheckForDRE)) {
569562
exprToCheckForDRE = load->getSubExpr();
570563
}

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,7 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
17551755
// guard let self = self else { return }
17561756
// method() // <- implicit self is not allowed
17571757
//
1758-
return conditionalStmt->rebindsSelf(Ctx, /*requiresCaptureListRef*/ false,
1759-
/*requireLoadExpr*/ true);
1758+
return conditionalStmt->rebindsSelf(Ctx, /*requiresCaptureListRef*/ true);
17601759
}
17611760

17621761
static bool

test/expr/closure/closures.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,9 +1604,7 @@ final class AutoclosureTests {
16041604
let someOptional: Self? = Self()
16051605
var `self` = self ?? someOptional // expected-warning {{'self' was never mutated; consider changing to 'let' constant}}
16061606
guard let self = self else { return }
1607-
// This is not supposed to be permitted, but has been allowed since Swift 5.8,
1608-
// so we have to continue allowing it to maintain source compatibility.
1609-
method()
1607+
method() // expected-error{{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
16101608
}
16111609

16121610
doVoidStuff { // expected-note {{capture 'self' explicitly to enable implicit 'self' in this closure}}

0 commit comments

Comments
 (0)