Skip to content

[SE-0365] Allow implicit self in inner functions in [weak self] closures, like with [self] closures #65211

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 3 commits into from
Apr 20, 2023

Conversation

calda
Copy link
Contributor

@calda calda commented Apr 15, 2023

This PR fixes an issue in the implementation of SE-0365, where implicit self was disallowed inside inner function declarations:

doWorkEscaping { [weak self] in
  guard let self else { return }
  func innerFunction() {
    // Before: Error: call to method 'test' in closure requires explicit use of 'self' to make capture semantics explicit
    // Now allowed without an error
    test()
  }
}

The intent of SE-0365 is to mirror the behavior of SE-0269 with [self] captures. This usage is allowed by SE-0269, so it should be allowed by SE-0365 as well:

doWorkEscaping { [self] in
  func innerFunction() {
    test() // ✅
  }
}

I confirmed on the forums that this was an intentional design choice of SE-0269.

Please review: @xedin

@calda calda force-pushed the cal--weak-self-capture-inner-func branch from 0e7479c to 9e55fb0 Compare April 15, 2023 15:27
Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I left a few comments inline, I would be great to get a few cases with more nesting and i.e. inits and subscripts involved (both valid and invalid)

@calda
Copy link
Contributor Author

calda commented Apr 19, 2023

Thanks for the review @xedin -- I incorporated your suggestions and added more extensive tests in 6670639

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@xedin
Copy link
Contributor

xedin commented Apr 19, 2023

@swift-ci please test

@xedin
Copy link
Contributor

xedin commented Apr 19, 2023

@swift-ci please test source compatibility

@calda
Copy link
Contributor Author

calda commented Apr 19, 2023

The one issue in the Release source compat suite is unrelated:

build. MovieSwift, 5.0, 237010, MovieSwift, generic/platform=iOS
This project built successfully, but it was expected to fail. #65203

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants