Is this "Sendable" warning correct?

Hello,

using the following class:

final class SomeClass: Sendable {
  @MainActor public var someInt: Int = 2
}

I get the warning "Stored property 'someInt' of 'Sendable'-conforming class 'SomeClass' is mutable", which I do not quite understand. I mean I do understand what the warning says, but not why this is a problem in this case. Shouldn't it be save to use SomeClass in a concurrent scenario, since access to someInt can only be done via the MainActor?

(I am using Xcode 13.3 beta 3)

Cheers, Michael

1 Like

It looks like the compiler is giving you this warning for two reasons, first because you have a mutable property and you've marked the type as thread safe with Sendable. Second because a class is a reference type and can be mutated from other places in your code. Unlike a struct in which you will receive a copy instead. There is an example of this in a test case out in the Swift repo.

1 Like

To me it seems that both cases (mutable property and reference type), should be taken care of by the property only being accessible through the MainActor. Am I being wrong here?

Looking at the test case I could not find any taking the MainActor scenario into account.

1 Like

Looking at the test case I could not find any taking the MainActor scenario into account.

You are correct, the test case does not encompass the MainActor scenario. The part that causes the issue here is having a mutable property on a reference type that is marked as Sendable. However, I'm not sure that marking the mutable property as being used by the MainActor helps helps alleviate this issue because of the Sendable conformance.

1 Like

A global-actor-isolated property shouldn’t be a problem for sendability; this looks like a bug.

2 Likes