Skip to content

Conversation

@jmschonfeld
Copy link
Contributor

AttributedString is Sendable, and it requires that all of its attribute values are Sendable. However, prior to this decision, we shipped attributes in the Apple SDKs that have non-Sendable values. We could not add an AttributedStringKey.Value : Sendable constraint because that constraint would cause downstream projects to fail to build when built with concurrency errors enabled, so instead every public AttributedString entry point has a where Key.Value : Sendable constraint to prevent reading/writing attributes values that are non-Sendable.

Unfortunately, there are a few situations where we cannot put this constraint because clients provide an attribute scope rather than an individual attribute key: attribute encoding/decoding and NSAttributedString conversion. In these cases, Foundation currently has warnings that indicate the values we're reading/writing may not be Sendable. This change effectively silences those warnings as there isn't anything we can do in Foundation about them.

This does leave a Sendable "escape hatch" in AttributedString: you can have an NSAttributedString that contains non-Sendable attributes and convert it to an AttributedString which stores it in a Sendable object. The constraints prevent reading the attribute value from AttributedString itself, but you can pass it across an isolation boundary and then convert it back to an NSAttributedString at which point you can read the non-Sendable value. Unfortunately, there's not much that we can do to prevent this currently, so the best we can do is at least allow Foundation to build without the warnings.

I've put this internal escape hatch in FOUNDATION_FRAMEWORK to help prevent accidental uses where it isn't needed.

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit 4d74f7a into swiftlang:main May 23, 2025
15 checks passed
@jmschonfeld jmschonfeld deleted the attrstr/sendable-warnings branch May 23, 2025 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants