Resolve AttributedString attribute storage Sendable warnings #1313
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
AttributedStringisSendable, and it requires that all of its attribute values areSendable. However, prior to this decision, we shipped attributes in the Apple SDKs that have non-Sendablevalues. We could not add anAttributedStringKey.Value : Sendableconstraint because that constraint would cause downstream projects to fail to build when built with concurrency errors enabled, so instead every publicAttributedStringentry point has awhere Key.Value : Sendableconstraint 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
NSAttributedStringconversion. In these cases, Foundation currently has warnings that indicate the values we're reading/writing may not beSendable. 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" inAttributedString: you can have anNSAttributedStringthat contains non-Sendableattributes and convert it to anAttributedStringwhich stores it in aSendableobject. The constraints prevent reading the attribute value fromAttributedStringitself, but you can pass it across an isolation boundary and then convert it back to anNSAttributedStringat which point you can read the non-Sendablevalue. 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_FRAMEWORKto help prevent accidental uses where it isn't needed.