Here's an example from CareKit (link), which is the only project in the source compatibility suite that does this:
open func didSelectContactView(_ contactView: UIView & OCKContactDisplayable) {
handleThrowable(method: controller.initiateSystemContactLookup) { [weak self] contactViewController in
contactViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self,
action: #selector(dismissViewController))
let navigationController = UINavigationController(rootViewController: contactViewController)
present(navigationController, animated: true, completion: nil)
}
}
The #selector(dismissViewController) and present(navigationController, animated: true, completion: nil) calls both refer to instance methods on self.
According to the behavior described in SE-0365, using implicit self like this on a weak self capture is valid but only after self has been unwrapped and is no longer an optional type.
The existing implementation of SE-0365 actually correctly (imo) rejects this code as invalid. I think the best thing to do here is probably to continue accept this code in the Swift 5 language mode (with a warning), and then reject it as an error in Swift 6. I can try to make this change as a part of landing SE-0365.
On the implementation side of things, I'm not entirely sure it's possible to continue supporting this for non-escaping closures while also supporting the behavior described in SE-0365 for escaping closures. Supporting the new behavior requires changes to AST lookup, which I'm not sure we can make conditional on whether or not the closure is non-escaping (since the closure itself isn't type-checked yet at this point, afaict). Continuing to investigate options here, though.
@Jumhyn -- if this bug blocks landing the implementation of SE-0365, do you think it would make sense to fix this bug in Swift 5.8 instead of Swift 6 (so we can land SE-0365 in Swift 5.8), or do you think we'd need to delay SE-0365 until Swift 6?
edit: good news, I was able to update the implementation of SE-0365 to handle this in Swift 5.8 without breaking existing code (but not allow this in Swift 6). Hopefully we're able to land that soon!