Is AttributedString sendable?

Here is a warning:

let sendableString: Sendable = AttributedString()
example.swift:13:32: warning: conformance of 'AttributedString' to 'Sendable' is unavailable; this is an error in Swift 6
let sendableString: Sendable = AttributedString()
Foundation.AttributedString:3:11: note: conformance of 'AttributedString' to 'Sendable' has been explicitly marked unavailable here
extension AttributedString : @unchecked Sendable {

Some questions about this:

  1. Is AttributedString Sendable? Is it threadsafe?
  2. Where is this conformance referenced at Foundation.AttributedString:3:11? Xcode cannot find it and pull it up in the editor.
  3. I was hoping to pull up this conformance definition and dig into the history here. For example, maybe there is a comment above the extension, explaining why it was marked unavailable. Maybe there is a related commit in git blame, maybe it is available in some OS version... I searched around in both GitHub - apple/swift: The Swift Programming Language and GitHub - apple/swift-corelibs-foundation: The Foundation Project, providing core utilities, internationalization, and OS independence without figuring out where this extension is coming from.
1 Like

AttributedString is part of Foundation, so the code that is used on Apple platforms is not open source. You could check out the open source implementation which might or might not work like the one you are interested about: swift-corelibs-foundation/Sources/Foundation/AttributedString at main · apple/swift-corelibs-foundation · GitHub

It looks like it contains yet another implementation of an os_unfair_lock wrapper, like these ones:

The documentation states that AttributedString conforms to Sendable, yet inspecting the generated module definition for Foundation I can’t find any declaration or protocol extension for AttributedString that defines Sendable

Unfortunately that in itself doesn't necessarily mean it does not conform, as types can be implicitly conforming to Sendable according to TSPL.

But AttributedString is marked as public, so it wouldn’t be elligible for implicit Sendable conformance anyway:

To satisfy the requirements of the Sendable protocol, an enumeration or structure must have only sendable members and associated values. In some cases, structures and enumerations that satisfy the requirements implicitly conform to Sendable:

  • Frozen structures and enumerations
  • Structures and enumerations that aren’t public and aren’t marked @usableFromInline.

I’m still confused as to where the documentation is getting the conformance from.

1 Like

Oops, of course you're right, @Luke_Lau, that slipped right past me!

In this case it seems the bug is within the documentation (at the moment), which is still weird. And of course it would be better if it was Sendable as it's a neat thing, especially in SwiftUI.

1 Like

Filed a feedback report for Foundation at FB10681345


We can try this in swift playground