Functions with both sending non-Sendable default parameter and isolation parameter cannot be called without passing argument to the non-Sendable parameter

In the example below, the test function accept a sending non-Sendable parameter with a default value and an isolation parameter. When calling it, it is OK to pass an instance of NonSendable. However, if I call it without providing an instance of NonSendable (use the default parameter), I will get an error “Sending value of non-Sendable type 'NonSendable' risks causing data races“. Why is it not allowed in the second case?

i think this is also a bug[1], or at least a confusing interaction between various features. i think the reason it occurs is because the defaulted parameter value is considered to be merged with the isolated parameter before test() is called, which for some reason prevents it from being sent and produces an error in the region based isolation pass (you can see more of the details in the verbose RBI logging here).

@hborla @Michael_Gottesman – can you confirm whether this is a bug please? i assume the two cases here should ideally behave the same, but perhaps there's some additional complexity here?


  1. you're on a roll! :) ↩︎

1 Like

It does feel like a bug. Fortunately I can use a workaround for this one with an overload

func test(
    _ value: sending NonSendable,
    isolated isolation: isolated (any Actor)? = #isolation
) {
    // ...
}

func test(isolated isolation: isolated (any Actor)? = #isolation) {
    test(.init())
}

But it will still be nicer to use default value :)