Skip to content
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

Explicit self required unexpectedly when capturing self in closure with Swift 6 SE-0365 #79014

Open
OliverBrown-Next opened this issue Jan 29, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@OliverBrown-Next
Copy link

OliverBrown-Next commented Jan 29, 2025

Description

An example that looks like it should be allowed by https://github.com/swiftlang/swift-evolution/blob/main/proposals/0365-implicit-self-weak-capture.md is causinga compile error Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit.

This was discovered after running swiftformat which removed a (seemingly) redundant self.

nicklockwood/SwiftFormat#1732

Reproduction

func takesEscapingClosure(_ closure: @escaping () -> Void) { closure() }

actor TestActor {
    func setUp() {
        takesEscapingClosure { [weak self] in
            Task { [weak self] in
                guard let self else {
                    return
                }
                await handleNetworkPathUpdate() // explicit self is needed here to avoid the compilation error
            }
        }
    }

    @MainActor
    func handleNetworkPathUpdate() { }
}

Expected behavior

Code should compile without the explicit self.

I see that SE-0365 has quite a lot about nested closures so I realise I may be missing something that this is expected.

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx14.0

Additional information

No response

@OliverBrown-Next OliverBrown-Next added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant