Last active
April 12, 2024 01:30
-
-
Save rnapier/cac382eb5e589e5229af4ccdc32a850e to your computer and use it in GitHub Desktop.
Somewhat surprising impact of non-Sendable default values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With Strict Concurrency | |
class C {} | |
struct S { | |
func f() { | |
Task { // Surprisingly need `@MainActor in` here to make this correct | |
await g() // Warning: Passing argument of non-sendable type 'C' into main actor-isolated context may introduce data races | |
} | |
} | |
@MainActor func g(value: C = C()) {} | |
} | |
/// It is somewhat surprising to the reader that you must move the entire Task to MainActor to avoid this warning. | |
/// Nothing at the call site hints to why. Given the `await`, you would expect that any needed context hopping | |
/// would already happen. But that's because you can't see the `C()` in the call, since it's a default value. | |
/// | |
/// You might imagine that IsolatedDefaultValues would resolve this, but it does not. I'm torn about whether it should. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment