Skip to content

[6.0] [Sema] Preserve compatibility for weak self with @_implicitSelfCapture #73997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,13 @@ static void diagnoseImplicitSelfUseInClosure(const Expr *E,
return true;
}

// Implicit self was also permitted for weak self captures in closures
// passed to @_implicitSelfCapture parameters in Swift 5.7.
if (auto *CE = dyn_cast<ClosureExpr>(ACE)) {
if (CE->allowsImplicitSelfCapture())
return true;
}

// Invalid captures like `[weak self = somethingElse]`
// were permitted in Swift 5.8, so we must only warn.
if (!isSimpleSelfCapture(weakSelfDecl)) {
Expand Down
12 changes: 10 additions & 2 deletions test/expr/closure/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,8 @@ public class TestImplicitCaptureOfExplicitCaptureOfSelfInEscapingClosure {
}
}

func takesEscapingWithAllowedImplicitSelf(@_implicitSelfCapture _ fn: @escaping () -> Void) {}

public class TestImplicitSelfForWeakSelfCapture {
static let staticOptional: TestImplicitSelfForWeakSelfCapture? = nil
func method() { }
Expand Down Expand Up @@ -817,7 +819,13 @@ public class TestImplicitSelfForWeakSelfCapture {
method()
}
}


takesEscapingWithAllowedImplicitSelf { [weak self] in
method() // expected-warning {{call to method 'method' in closure requires explicit use of 'self' to make capture semantics explicit}}
guard let self = self else { return }
method()
}

doVoidStuff { [weak self] in
let `self`: TestImplicitSelfForWeakSelfCapture? = self ?? TestImplicitSelfForWeakSelfCapture.staticOptional
guard let self = self else { return }
Expand Down Expand Up @@ -1743,4 +1751,4 @@ struct TestAsyncLetInStruct {
async let _ = bar()
}
}
}
}
10 changes: 9 additions & 1 deletion test/expr/closure/closures_swift6.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class C_56501 {
}
}

func takesEscapingWithAllowedImplicitSelf(@_implicitSelfCapture _ fn: @escaping () -> Void) {}

public final class TestImplicitSelfForWeakSelfCapture: Sendable {
static let staticOptional: TestImplicitSelfForWeakSelfCapture? = .init()
func method() { }
Expand Down Expand Up @@ -130,6 +132,12 @@ public final class TestImplicitSelfForWeakSelfCapture: Sendable {
}
}

takesEscapingWithAllowedImplicitSelf { [weak self] in
method() // expected-error {{explicit use of 'self' is required when 'self' is optional, to make control flow explicit}} expected-note {{reference 'self?.' explicitly}}
guard let self = self else { return }
method()
}

doVoidStuff { [weak self] in
doVoidStuff { // expected-note {{capture 'self' explicitly to enable implicit 'self' in this closure}}
guard let self = self else { return }
Expand Down Expand Up @@ -796,4 +804,4 @@ struct TestInvalidSelfCaptureInStruct {
self.method()
}
}
}
}