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

Warning suggests @preconcurrency import but when added, there is a warning that @preconcurrency import has no effect. #74904

Closed
jqsilver opened this issue Jul 2, 2024 · 11 comments · Fixed by #77057
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features

Comments

@jqsilver
Copy link

jqsilver commented Jul 2, 2024

Description

When I compile a class that implements WKNavigationDelegate, I get a warning on the line with import WebKit:
Add '@preconcurrency' to suppress 'Sendable'-related warnings from module 'WebKit'
However, there are no Sendable-related warnings in the file.

When I add the @preconcurrency to the import WebKit statement, I get this warning:
'@preconcurrency' attribute on module 'WebKit' has no effect

Eventually I determined that the WKNavigationDelegate methods were outdated for iOS 18, and I needed to add a @MainActor attribute to a closure in one of the delegate methods.

Reproduction

I created a new project using Xcode 16 beta 2, and then added this code as a new file.

import UIKit
import WebKit  // warning: `Add '@preconcurrency' to suppress 'Sendable'-related warnings from module 'WebKit'`

public final class SmartHTMLView: UIView, WKNavigationDelegate {

  public override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(webView)
    webView.navigationDelegate = self
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  private let webView = WKWebView(frame: .zero)

  public func webView(
    _: WKWebView,
    decidePolicyFor navigationAction: WKNavigationAction,
    decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
  {
  }
}

When I add the @preconcurrency attribute, I get a different warning.

import UIKit
@preconcurrency import WebKit  // warning: `'@preconcurrency' attribute on module 'WebKit' has no effect`

public final class SmartHTMLView: UIView, WKNavigationDelegate {

  public override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(webView)
    webView.navigationDelegate = self
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  private let webView = WKWebView(frame: .zero)

  public func webView(
    _: WKWebView,
    decidePolicyFor navigationAction: WKNavigationAction,
    decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
  {
  }
}

I was able to fix the warnings by updating webView(:decidePolicyFor:decisionHandler:) method to match the iOS 18 WKNavigationDelegate API:

import UIKit
import WebKit

public final class SmartHTMLView: UIView, WKNavigationDelegate {

  public override init(frame: CGRect) {
    super.init(frame: frame)
    addSubview(webView)
    webView.navigationDelegate = self
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  private let webView = WKWebView(frame: .zero)

  public func webView(
    _: WKWebView,
    decidePolicyFor navigationAction: WKNavigationAction,
    decisionHandler: @escaping @MainActor (WKNavigationActionPolicy) -> Void)
  {
  }
}

Expected behavior

  1. A warning suggesting that @preconcurrency be added should not be emitted when there are no Sendable-related warnings in the file.
  2. There should be a warning that the following method doesn't match the updated WKNavigationDelegate protocol:
  public func webView(
     _: WKWebView,
     decidePolicyFor navigationAction: WKNavigationAction,
     decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
   {
   }

Environment

WebKitImportPreconcurrencyRepro (main)$ swiftc -version
swift-driver version: 1.110 Apple Swift version 6.0 (swiftlang-6.0.0.4.52 clang-1600.0.21.1.3)
Target: arm64-apple-macosx14.0

Additional information

No response

@jqsilver jqsilver added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 2, 2024
@mikaelacaron
Copy link

mikaelacaron commented Jul 7, 2024

I am having this same issue! But when importing TabletopKit, without making any changes, it's happening to the TabletopKit sample project that you can download here: https://developer.apple.com/documentation/tabletopkit/tabletopkitsample

I'm using Xcode 16.0 Beta 2, on macOS 14.5

@drewolbrich
Copy link

Thank you so much for posting this. I spent quite a lot of time trying to figure this one out, and wasn't able to discover your webView(:decidePolicyFor:decisionHandler:) @MainActor fix myself.

I submitted feedback about the circular warning issue to Apple as FB14066938.

@jqsilver
Copy link
Author

I noticed that with Xcode 16 beta 3 this protocol has been updated:

optional func webView(
    _ webView: WKWebView,
    decidePolicyFor navigationAction: WKNavigationAction,
    decisionHandler: @escaping @MainActor @Sendable (WKNavigationActionPolicy) -> Void
)

Note that the decisionHandler is now also @Sendable.

@drewolbrich
Copy link

drewolbrich commented Jul 10, 2024

I don't know if this is significant, but Xcode autocomplete does not include @Sendable:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, 
    decisionHandler: @escaping @MainActor (WKNavigationActionPolicy) -> Void) {
    // ...
}

I do see @Sendable in the corresponding method definition in the definition of WKNavigationDelegate, however.

@hborla hborla added concurrency Feature: umbrella label for concurrency language features and removed triage needed This issue needs more specific labels labels Jul 10, 2024
@jqsilver
Copy link
Author

I verified that these two warnings still occur in Xcode 16 beta 3, although it still goes away with just the @MainActor attribute and without the @Sendable attribute

@chaitu-venky
Copy link

My project also facing similar issue, if add the attribute @mainactor to the protocol am able to compile with xcode 16 beta. But if i compile the same project with xcode 15 build fails. Wanted to compile that project both xcode 16 and as well xcode 15. Someone can help me how to fix this?

@mikaelacaron
Copy link

My project also facing similar issue, if add the attribute @mainactor to the protocol am able to compile with xcode 16 beta. But if i compile the same project with xcode 15 build fails. Wanted to compile that project both xcode 16 and as well xcode 15. Someone can help me how to fix this?

@chaitu-venky I think it's expected to fail with Xcode 15, because @preconcurrency is a Swift 6 feature. so that sounds like it's working as expected

@jqsilver
Copy link
Author

jqsilver commented Aug 14, 2024

Actually we've been using @preconcurrency with Xcode 15 already. I think the problem with adding @MainActor is that changes the type signature for your delegate method, which is different between iOS 18 (on Xcode 16) and iOS 17 (on Xcode 15). We had to use #if compiler(>=6.0) to get around this, eg:

extension BasicWebViewController: WKNavigationDelegate {
  #if compiler(>=6.0)
  open func webView(
    _: WKWebView,
    decidePolicyFor navigationAction: WKNavigationAction,
    decisionHandler: @escaping @MainActor (WKNavigationActionPolicy) -> Void)
  {
    ...
  }
 #else
  open func webView(
    _: WKWebView,
    decidePolicyFor navigationAction: WKNavigationAction,
    decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) 
  {
    ... 
  }
  #endif

@mikaelacaron
Copy link

oh good to know!

@paulb777
Copy link

The circular warnings problem still occurs in Xcode 16.1 beta 1.

sophiapoirier added a commit to sophiapoirier/swift that referenced this issue Oct 16, 2024
…ndability conformance diagnostic has been suppressed under minimal concurrency checking (resolves swiftlang#74904)
sophiapoirier added a commit to sophiapoirier/swift that referenced this issue Oct 16, 2024
…protocol sendability conformance diagnostic has been suppressed under minimal concurrency checking (resolves swiftlang#74904)
sophiapoirier added a commit to sophiapoirier/swift that referenced this issue Oct 16, 2024
…protocol sendability conformance diagnostic has been suppressed under minimal concurrency checking (resolves swiftlang#74904)
@sophiapoirier
Copy link
Contributor

btw the fix for this will no longer produce the erroneous warning message under minimal or targeted concurrency:
Add '@preconcurrency' to suppress 'Sendable'-related warnings from module 'WebKit'
but the correct warning if you do @preconcurrency import WebKit will remain:
'@preconcurrency' attribute on module 'WebKit' has no effect

The desired diagnostic about matching the attributes in the signature of the function being overridden already is produced when using complete strict concurrency.

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. concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants