Skip to content

[SE-0456, -0467] Data+Span+MutableSpan #1276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ list(APPEND _SwiftFoundation_versions
list(APPEND _SwiftFoundation_availability_names
"FoundationPreview"
"FoundationPredicate"
"FoundationPredicateRegex")
"FoundationPredicateRegex"
"FoundationSpan")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a new availability macro?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires a 6.2 toolchain to get the symbols, and won't have the same range of deployment targets as any of the others, as far as I can tell.


# The aligned availability for each name (in the same order)
list(APPEND _SwiftFoundation_availability_releases
Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import CompilerPluginSupport
let availabilityTags: [_Availability] = [
_Availability("FoundationPreview"), // Default FoundationPreview availability,
_Availability("FoundationPredicate"), // Predicate relies on pack parameter runtime support
_Availability("FoundationPredicateRegex") // Predicate regexes rely on new stdlib APIs
_Availability("FoundationPredicateRegex"), // Predicate regexes rely on new stdlib APIs
_Availability("FoundationSpan"), // Availability of Span types
]
let versionNumbers = ["0.1", "0.2", "0.3", "0.4", "6.0.2", "6.1", "6.2"]

Expand Down Expand Up @@ -133,6 +134,9 @@ let package = Package(
] + wasiLibcCSettings,
swiftSettings: [
.enableExperimentalFeature("VariadicGenerics"),
.enableExperimentalFeature("LifetimeDependence"),
.enableExperimentalFeature("AddressableTypes"),
.enableExperimentalFeature("BuiltinModule"),
.enableExperimentalFeature("AccessLevelOnImport")
] + availabilityMacros + concurrencyChecking,
linkerSettings: [
Expand Down
161 changes: 160 additions & 1 deletion Sources/FoundationEssentials/Data/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#endif

internal import _FoundationCShims
import Builtin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be internal import Builtin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Builtin module is public by virtue of being inlined all over the standard library; in this file we're calling it from opaque code, so there's nothing being reexported at all.


#if canImport(Darwin)
import Darwin
Expand Down Expand Up @@ -604,6 +605,7 @@ internal final class __DataStorage : @unchecked Sendable {

@frozen
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
@_addressableForDependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ABI or source stability impact for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no source stability impact. It only changes the mangling for functions that try to establish a lifetime dependence on Data, and there are none of those until we publish these span properties.

public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollection, RangeReplaceableCollection, MutableDataProtocol, ContiguousBytes, Sendable {

public typealias Index = Int
Expand Down Expand Up @@ -2198,7 +2200,105 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
public func withUnsafeBytes<ResultType>(_ body: (UnsafeRawBufferPointer) throws -> ResultType) rethrows -> ResultType {
return try _representation.withUnsafeBytes(body)
}


#if compiler(>=6.2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only support building this with the 6.2 compiler in the first place.

@available(FoundationSpan 6.2, *)
public var bytes: RawSpan {
@lifetime(borrow self)
borrowing get {
let buffer: UnsafeRawBufferPointer
switch _representation {
case .empty:
buffer = UnsafeRawBufferPointer(start: nil, count: 0)
case .inline:
buffer = unsafe UnsafeRawBufferPointer(
start: UnsafeRawPointer(Builtin.addressOfBorrow(self)),
count: _representation.count
)
case .large(let slice):
buffer = unsafe UnsafeRawBufferPointer(
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count
)
case .slice(let slice):
buffer = unsafe UnsafeRawBufferPointer(
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count
)
}
let span = unsafe RawSpan(_unsafeBytes: buffer)
return unsafe _overrideLifetime(span, borrowing: self)
}
}

@available(FoundationSpan 6.2, *)
public var span: Span<UInt8> {
@lifetime(borrow self)
borrowing get {
let span = unsafe bytes._unsafeView(as: UInt8.self)
return _overrideLifetime(span, borrowing: self)
}
}

@available(FoundationSpan 6.2, *)
public var mutableBytes: MutableRawSpan {
@lifetime(&self)
mutating get {
let buffer: UnsafeMutableRawBufferPointer
switch _representation {
case .empty:
buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0)
case .inline:
buffer = unsafe UnsafeMutableRawBufferPointer(
start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)),
count: _representation.count
)
case .large(let slice):
buffer = unsafe UnsafeMutableRawBufferPointer(
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count
)
case .slice(let slice):
buffer = unsafe UnsafeMutableRawBufferPointer(
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count
)
}
let span = unsafe MutableRawSpan(_unsafeBytes: buffer)
return unsafe _overrideLifetime(span, mutating: &self)
}
}

@available(FoundationSpan 6.2, *)
public var mutableSpan: MutableSpan<UInt8> {
@lifetime(&self)
mutating get {
#if false // see https://github.com/swiftlang/swift/issues/81218
var bytes = mutableBytes
let span = unsafe bytes._unsafeMutableView(as: UInt8.self)
return _overrideLifetime(span, mutating: &self)
#else
let buffer: UnsafeMutableRawBufferPointer
switch _representation {
case .empty:
buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0)
case .inline:
buffer = unsafe UnsafeMutableRawBufferPointer(
start: UnsafeMutableRawPointer(Builtin.addressOfBorrow(self)),
count: _representation.count
)
case .large(let slice):
buffer = unsafe UnsafeMutableRawBufferPointer(
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count
)
case .slice(let slice):
buffer = unsafe UnsafeMutableRawBufferPointer(
start: slice.storage.mutableBytes?.advanced(by: slice.startIndex), count: slice.count
)
}
let span = unsafe MutableSpan<UInt8>(_unsafeBytes: buffer)
return unsafe _overrideLifetime(span, mutating: &self)
#endif
}
}
#endif

@_alwaysEmitIntoClient
public func withContiguousStorageIfAvailable<ResultType>(_ body: (_ buffer: UnsafeBufferPointer<UInt8>) throws -> ResultType) rethrows -> ResultType? {
return try _representation.withUnsafeBytes {
Expand Down Expand Up @@ -2848,3 +2948,62 @@ extension Data : Codable {
}
}
}

#if compiler(>=6.2)
// TODO: remove once _overrideLifetime is public in the standard library
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will that happen?

Copy link
Contributor Author

@glessard glessard Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the PR: swiftlang/swift#81225
I presume that this could be in a nightly snapshot within a week.


/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
/// a value identical to `dependent` with a lifetime dependency on the caller's
/// borrow scope of the `source` argument.
@unsafe
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
@_transparent
@lifetime(borrow source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
_ dependent: consuming T, borrowing source: borrowing U
) -> T {
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}

/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
/// a value identical to `dependent` that inherits all lifetime dependencies from
/// the `source` argument.
@unsafe
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
@_transparent
@lifetime(copy source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
_ dependent: consuming T, copying source: borrowing U
) -> T {
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}

/// Unsafely discard any lifetime dependency on the `dependent` argument.
/// Return a value identical to `dependent` with a lifetime dependency
/// on the caller's exclusive borrow scope of the `source` argument.
@unsafe
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
@_transparent
@lifetime(&source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
_ dependent: consuming T,
mutating source: inout U
) -> T {
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
// should be expressed by a builtin that is hidden within the function body.
dependent
}
#endif
134 changes: 134 additions & 0 deletions Tests/FoundationEssentialsTests/DataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,140 @@ class DataTests : XCTestCase {
// source.advanced(by: 5)
}

@available(FoundationSpan 6.2, *)
func test_InlineDataSpan() throws {
var source = Data()
var span = source.span
XCTAssertTrue(span.isEmpty)

source.append(contentsOf: [1, 2, 3])
span = source.span
XCTAssertFalse(span.isEmpty)
XCTAssertEqual(span.count, source.count)
XCTAssertEqual(span[0], 1)
}

@available(FoundationSpan 6.2, *)
func test_InlineSliceDataSpan() throws {
let source = Data(0 ... .max)
let span = source.span
XCTAssertEqual(span.count, source.count)
XCTAssertEqual(span[span.indices.last!], .max)
}

@available(FoundationSpan 6.2, *)
func test_LargeSliceDataSpan() throws {
#if _pointerBitWidth(_64)
let count = Int(Int32.max)
#elseif _pointerBitWidth(_32)
let count = Int(Int16.max)
#else
#error("This test needs updating")
#endif

let source = Data(repeating: 0, count: count).dropFirst()
XCTAssertNotEqual(source.startIndex, 0)
let span = source.span
XCTAssertFalse(span.isEmpty)
}

@available(FoundationSpan 6.2, *)
func test_InlineDataMutableSpan() throws {
var source = Data()
var span = source.mutableSpan
XCTAssertTrue(span.isEmpty)

source.append(contentsOf: [1, 2, 3])
let count = source.count
span = source.mutableSpan
let i = try XCTUnwrap(span.indices.randomElement())
XCTAssertFalse(span.isEmpty)
XCTAssertEqual(span.count, count)
let v = UInt8.random(in: 10..<100)
span[i] = v
XCTAssertEqual(source[i], v)
}

@available(FoundationSpan 6.2, *)
func test_InlineSliceDataMutableSpan() throws {
var source = Data(0..<100)
let count = source.count
var span = source.mutableSpan
XCTAssertEqual(span.count, count)
let i = try XCTUnwrap(span.indices.randomElement())
span[i] = .max
XCTAssertEqual(source[i], .max)
}

@available(FoundationSpan 6.2, *)
func test_LargeSliceDataMutableSpan() throws {
#if _pointerBitWidth(_64)
var count = Int(Int32.max)
#elseif _pointerBitWidth(_32)
var count = Int(Int16.max)
#else
#error("This test needs updating")
#endif

var source = Data(repeating: 0, count: count).dropFirst()
XCTAssertNotEqual(source.startIndex, 0)
count = source.count
var span = source.mutableSpan
XCTAssertEqual(span.count, count)
let i = try XCTUnwrap(span.indices.dropFirst().randomElement())
span[i] = .max
XCTAssertEqual(source[i], 0)
XCTAssertEqual(source[i+1], .max)
}

@available(FoundationSpan 6.2, *)
func test_InlineDataMutableRawSpan() throws {
var source = Data()
var span = source.mutableBytes
XCTAssertTrue(span.isEmpty)

source.append(contentsOf: [1, 2, 3])
let count = source.count
span = source.mutableBytes
let i = try XCTUnwrap(span.byteOffsets.randomElement())
XCTAssertFalse(span.isEmpty)
XCTAssertEqual(span.byteCount, count)
let v = UInt8.random(in: 10..<100)
span.storeBytes(of: v, toByteOffset: i, as: UInt8.self)
XCTAssertEqual(source[i], v)
}

@available(FoundationSpan 6.2, *)
func test_InlineSliceDataMutableRawSpan() throws {
var source = Data(0..<100)
let count = source.count
var span = source.mutableBytes
XCTAssertEqual(span.byteCount, count)
let i = try XCTUnwrap(span.byteOffsets.randomElement())
span.storeBytes(of: -1, toByteOffset: i, as: Int8.self)
XCTAssertEqual(source[i], .max)
}

@available(FoundationSpan 6.2, *)
func test_LargeSliceDataMutableRawSpan() throws {
#if _pointerBitWidth(_64)
var count = Int(Int32.max)
#elseif _pointerBitWidth(_32)
var count = Int(Int16.max)
#else
#error("This test needs updating")
#endif

var source = Data(repeating: 0, count: count).dropFirst()
XCTAssertNotEqual(source.startIndex, 0)
count = source.count
var span = source.mutableBytes
XCTAssertEqual(span.byteCount, count)
let i = try XCTUnwrap(span.byteOffsets.dropFirst().randomElement())
span.storeBytes(of: -1, toByteOffset: i, as: Int8.self)
XCTAssertEqual(source[i], 0)
XCTAssertEqual(source[i+1], .max)
}

#if false // FIXME: XCTest doesn't support crash tests yet rdar://20195010&22387653
func test_bounding_failure_subdata() {
Expand Down
Loading