swift icon indicating copy to clipboard operation
swift copied to clipboard

Wrong Sendable closure diagnostics with implicitly unwrapped optional

Open groue opened this issue 1 year ago • 3 comments

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 avatar Mar 30 '24 16:03 groue

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

groue avatar Mar 31 '24 08:03 groue

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

groue avatar Jun 11 '24 16:06 groue

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

groue avatar Jul 09 '24 05:07 groue