Skip to content

False Sendable data race warnings when passing functions as parameters #67279

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

Closed
MahdiBM opened this issue Jul 13, 2023 · 5 comments
Closed

False Sendable data race warnings when passing functions as parameters #67279

MahdiBM opened this issue Jul 13, 2023 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself concurrency Feature: umbrella label for concurrency language features function types Feature → types: function types implicit conversions Feature: implicit conversions Sendable Area → standard library: The Sendable protocol swift 5.9 type checker Area → compiler: Semantic analysis types Feature: types unexpected warning Bug: Unexpected warning

Comments

@MahdiBM
Copy link

MahdiBM commented Jul 13, 2023

Description

There are false Sendable warnings when passing functions as parameters.

Steps to reproduce

struct Something: Sendable {
    @Sendable func doNothing() { }
}
/// ^~~~ The warning is the same, with or without any of the two `Sendable`s.

func doSomething(
    _ block: @Sendable (Something) -> (() -> Void)
    /// ~~~~~^ `@Sendable` required to trigger the warning.
) {
    fatalError()
}

doSomething(Something.doNothing)
/// ^~~~~ Warning: Converting non-sendable function value to '@Sendable (Something) -> (() -> Void)' may introduce data races

The warning goes away if you open the closure instead of directly passing the function:

doSomething { $0.doNothing }
/// ^~~~~ No warnings

Expected behavior
No false warnings.

Environment
Linux CI with swift 5.8 on Ubuntu jammy shows these warnings, as well as my own local environment with Xcode 15 beta 2 and Swift 5.9, so it's fair to say this is not tied to a platform or a Swift version.

~ % swiftc -version
swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.10 clang-1500.0.29.1)
Target: arm64-apple-macosx14.0
~ % xcodebuild -version
Xcode 15.0
Build version 15A5161b
~ % uname -a
Darwin Macbook.local 23.0.0 Darwin Kernel Version 23.0.0: Tue Jun 13 21:16:44 PDT 2023; root:xnu-10002.0.116.505.3~3/RELEASE_ARM64_T6000 arm64

Additional Context

@MahdiBM MahdiBM added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 13, 2023
@angela-laar angela-laar self-assigned this Nov 2, 2023
@angela-laar angela-laar added concurrency Feature: umbrella label for concurrency language features type checker Area → compiler: Semantic analysis compiler The Swift compiler itself labels Jan 9, 2024
@hborla
Copy link
Member

hborla commented Jan 23, 2024

This is fixed under -enable-experimental-feature InferSendableFromCaptures on main

@MahdiBM
Copy link
Author

MahdiBM commented Jan 23, 2024

@hborla can confirm, thanks!
New:

~ docker run --rm -it swiftlang/swift:nightly-main-jammy swift -enable-experimental-feature InferSendableFromCaptures -e "struct Something: Sendable {                                                  
         @Sendable func doNothing() { }
     }
     /// ^~~~ The warning is the same, with or without any of the two `Sendable`s.

     func doSomething(
         _ block: @Sendable (Something) -> (() -> Void)
         /// ~~~~~^ `@Sendable` required to trigger the warning.
     ) {
         print(\"Doing some!\")
     }

     doSomething(Something.doNothing)"
Doing some!

Old:

~ docker run --rm -it swiftlang/swift:nightly-main-jammy swift -e "struct Something: Sendable {                                                                                                             
         @Sendable func doNothing() { }
     }
     /// ^~~~ The warning is the same, with or without any of the two `Sendable`s.

     func doSomething(
         _ block: @Sendable (Something) -> (() -> Void)
         /// ~~~~~^ `@Sendable` required to trigger the warning.
     ) {
         print(\"Doing some!\")
     }

     doSomething(Something.doNothing)"
-e:13:23: warning: converting non-sendable function value to '@Sendable (Something) -> (() -> Void)' may introduce data races
doSomething(Something.doNothing)
                      ^
Doing some!

@MahdiBM MahdiBM closed this as completed Jan 23, 2024
@MahdiBM
Copy link
Author

MahdiBM commented Feb 11, 2024

@hborla It appears there has been a regression re-introducing this warning, based on the latest Docker images:

New, emits warnings:

~ docker run --rm -it swiftlang/swift:nightly-main-jammy@sha256:39867f18bb76cf9a02d4e7e9dbbaf9674154675063c1b1d7928daa63d2658ea2 swift -enable-experimental-feature InferSendableFromCaptures -e "struct Something: Sendable {
              @Sendable func doNothing() { }
          }
          /// ^~~~ The warning is the same, with or without any of the two `Sendable`s.

          func doSomething(
              _ block: @Sendable (Something) -> (() -> Void)
              /// ~~~~~^ `@Sendable` required to trigger the warning.
          ) {
              print(\"Doing some!\")
          }

          doSomething(Something.doNothing)"
-e:13:28: warning: converting non-sendable function value to '@Sendable (Something) -> (() -> Void)' may introduce data races
     doSomething(Something.doNothing)
                           ^
Doing some!

Old, no warnings:

~ docker run --rm -it swiftlang/swift:nightly-main-jammy@sha256:c46884670e329f4f50e6b640be66bdcc16dcb75829878e0e1d22ebc659f3fd3a swift -enable-experimental-feature InferSendableFromCaptures -e "struct Something: Sendable {
              @Sendable func doNothing() { }
          }
          /// ^~~~ The warning is the same, with or without any of the two `Sendable`s.

          func doSomething(
              _ block: @Sendable (Something) -> (() -> Void)
              /// ~~~~~^ `@Sendable` required to trigger the warning.
          ) {
              print(\"Doing some!\")
          }

          doSomething(Something.doNothing)"
Doing some!

Notice the hash difference of the nightly-main images.

@xedin
Copy link
Contributor

xedin commented Feb 12, 2024

@MahdiBM We have promoted the feature to "upcoming" since it has been accepted by the Swift Evolution which means that the flag now is -enable-upcoming-feature instead of -enable-experimental-feature. I think we should warn about this to avoid the confusion...

@MahdiBM
Copy link
Author

MahdiBM commented Feb 12, 2024

@xedin -enable-upcoming-feature does work, thanks!

@MahdiBM MahdiBM closed this as completed Feb 12, 2024
@AnthonyLatsis AnthonyLatsis added unexpected warning Bug: Unexpected warning Sendable Area → standard library: The Sendable protocol function types Feature → types: function types implicit conversions Feature: implicit conversions types Feature: types swift 5.9 and removed triage needed This issue needs more specific labels labels Feb 13, 2024
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. compiler The Swift compiler itself concurrency Feature: umbrella label for concurrency language features function types Feature → types: function types implicit conversions Feature: implicit conversions Sendable Area → standard library: The Sendable protocol swift 5.9 type checker Area → compiler: Semantic analysis types Feature: types unexpected warning Bug: Unexpected warning
Projects
None yet
Development

No branches or pull requests

5 participants