swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.25k stars 10.32k forks source link

Wrong Sendable closure diagnostics with implicitly unwrapped optional #72727

Open groue opened 5 months ago

groue commented 5 months ago

Description

Hello,

The Swift compiler 5.10 that ships with Xcode 15.3 (15E204a) emits spurious warnings regarding sendable closures, in the specific case of implicitly unwrapped optionals.

Reproduction

In the following code, the compiler emit false positive warnings. They are all related to implicitly unwrapped optionals.

Converting non-sendable function value to '@Sendable () -> Void' may introduce data races

struct ClosureHolder {
    var closure: @Sendable () -> Void = { }
    var closureOpt: (@Sendable () -> Void)?
    var closureIUO: (@Sendable () -> Void)!
}

func f() {
    var holder: ClosureHolder = ClosureHolder()
    holder.closure = { }        // OK
    holder.closureOpt = { }     // OK
    holder.closureIUO = { }     // ⚠️ WARNING

    var holderOpt: ClosureHolder?
    holderOpt?.closure = { }    // OK
    holderOpt?.closureOpt = { } // OK
    holderOpt?.closureIUO = { } // ⚠️ WARNING

    var holderIUO: ClosureHolder!
    holderIUO.closure = { }     // ⚠️ WARNING
    holderIUO.closureOpt = { }  // ⚠️ WARNING
    holderIUO.closureIUO = { }  // ⚠️ WARNING
}

Expected behavior

The compiler emits no warning.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4) Target: arm64-apple-macosx14.0

Additional information

No response

groue commented 5 months ago

Reproducible with both swift-DEVELOPMENT-SNAPSHOT-2024-03-29-a and swift-6.0-DEVELOPMENT-SNAPSHOT-2024-03-26-a.

groue commented 2 months ago

The bug is still present in Xcode 16.0 beta (16A5171c).

groue commented 1 month ago

The bug is still present in Xcode 16.0 beta 3 (16A5202i).