Skip to content

[Windows] Clean up workarounds and enable Span #1349

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

Merged
merged 2 commits into from
Jun 18, 2025
Merged
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
19 changes: 1 addition & 18 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ let package = Package(
swiftSettings: [
.enableExperimentalFeature("VariadicGenerics"),
.enableExperimentalFeature("LifetimeDependence"),
.enableExperimentalFeature(
"InoutLifetimeDependence",
.when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .linux])
),
.enableExperimentalFeature(
"LifetimeDependenceMutableAccessors",
.when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .linux])
),
.enableExperimentalFeature("AddressableTypes"),
.enableExperimentalFeature("AllowUnsafeAttribute"),
.enableExperimentalFeature("BuiltinModule"),
Expand All @@ -167,16 +159,7 @@ let package = Package(
resources: [
.copy("Resources")
],
swiftSettings: [
.enableExperimentalFeature(
"InoutLifetimeDependence",
.when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .linux])
),
.enableExperimentalFeature(
"LifetimeDependenceMutableAccessors",
.when(platforms: [.macOS, .iOS, .watchOS, .tvOS, .linux])
),
] + availabilityMacros + featureSettings + testOnlySwiftSettings
swiftSettings: availabilityMacros + featureSettings + testOnlySwiftSettings
),

// FoundationInternationalization
Expand Down
4 changes: 1 addition & 3 deletions Sources/FoundationEssentials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ endif()

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
target_compile_options(FoundationEssentials PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -Xcc -Xfrontend -D_GNU_SOURCE>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend InoutLifetimeDependence>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend LifetimeDependenceMutableAccessors>")
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -Xcc -Xfrontend -D_GNU_SOURCE>")
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
endif()

Expand Down
8 changes: 0 additions & 8 deletions Sources/FoundationEssentials/Data/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,6 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
return try _representation.withUnsafeBytes(body)
}

#if compiler(>=6.2) && $LifetimeDependence
@available(FoundationSpan 6.2, *)
public var bytes: RawSpan {
@lifetime(borrow self)
Expand Down Expand Up @@ -2239,9 +2238,7 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
return _overrideLifetime(span, borrowing: self)
}
}
#endif

#if compiler(>=5.9) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
@available(FoundationSpan 6.2, *)
public var mutableBytes: MutableRawSpan {
@lifetime(&self)
Expand Down Expand Up @@ -2301,7 +2298,6 @@ public struct Data : Equatable, Hashable, RandomAccessCollection, MutableCollect
#endif
}
}
#endif // $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors

@_alwaysEmitIntoClient
public func withContiguousStorageIfAvailable<ResultType>(_ body: (_ buffer: UnsafeBufferPointer<UInt8>) throws -> ResultType) rethrows -> ResultType? {
Expand Down Expand Up @@ -2976,7 +2972,6 @@ extension Data : Codable {
}

// TODO: remove once _overrideLifetime is public in the standard library
#if compiler(>=6.2) && $LifetimeDependence
/// 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.
Expand Down Expand Up @@ -3008,9 +3003,7 @@ internal func _overrideLifetime<
) -> T {
dependent
}
#endif

#if compiler(>=5.9) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
/// 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.
Expand All @@ -3027,4 +3020,3 @@ internal func _overrideLifetime<
) -> T {
dependent
}
#endif // $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
18 changes: 0 additions & 18 deletions Tests/FoundationEssentialsTests/DataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,6 @@ class DataTests : XCTestCase {
func test_InlineDataSpan() throws {
guard #available(FoundationSpan 6.2, *) else { throw XCTSkip("Span not available") }

#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
var source = Data()
var span = source.span
XCTAssertTrue(span.isEmpty)
Expand All @@ -1647,18 +1646,15 @@ class DataTests : XCTestCase {
XCTAssertFalse(span.isEmpty)
XCTAssertEqual(span.count, source.count)
XCTAssertEqual(span[0], 1)
#endif
}

func test_InlineSliceDataSpan() throws {
guard #available(FoundationSpan 6.2, *) else { throw XCTSkip("Span not available") }

#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
let source = Data(0 ... .max)
let span = source.span
XCTAssertEqual(span.count, source.count)
XCTAssertEqual(span[span.indices.last!], .max)
#endif
}

func test_LargeSliceDataSpan() throws {
Expand All @@ -1672,19 +1668,16 @@ class DataTests : XCTestCase {
#error("This test needs updating")
#endif

#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
let source = Data(repeating: 0, count: count).dropFirst()
XCTAssertNotEqual(source.startIndex, 0)
let span = source.span
XCTAssertFalse(span.isEmpty)
#endif
}

func test_InlineDataMutableSpan() throws {
guard #available(FoundationSpan 6.2, *) else { throw XCTSkip("Span not available") }

#if !canImport(Darwin) || FOUNDATION_FRAMEWORK
#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
var source = Data()
var span = source.mutableSpan
XCTAssertTrue(span.isEmpty)
Expand All @@ -1700,15 +1693,13 @@ class DataTests : XCTestCase {
var sub = span.extracting(i ..< i+1)
sub.update(repeating: v)
XCTAssertEqual(source[i], v)
#endif
#endif
}

func test_InlineSliceDataMutableSpan() throws {
guard #available(FoundationSpan 6.2, *) else { throw XCTSkip("Span not available") }

#if !canImport(Darwin) || FOUNDATION_FRAMEWORK
#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
var source = Data(0..<100)
let count = source.count
var span = source.mutableSpan
Expand All @@ -1717,7 +1708,6 @@ class DataTests : XCTestCase {
var sub = span.extracting(i..<i+1)
sub.update(repeating: .max)
XCTAssertEqual(source[i], .max)
#endif
#endif
}

Expand All @@ -1733,7 +1723,6 @@ class DataTests : XCTestCase {
#endif

#if !canImport(Darwin) || FOUNDATION_FRAMEWORK
#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
var source = Data(repeating: 0, count: count).dropFirst()
XCTAssertNotEqual(source.startIndex, 0)
count = source.count
Expand All @@ -1743,14 +1732,12 @@ class DataTests : XCTestCase {
span[i] = .max
XCTAssertEqual(source[i], 0)
XCTAssertEqual(source[i+1], .max)
#endif
#endif
}

func test_InlineDataMutableRawSpan() throws {
guard #available(FoundationSpan 6.2, *) else { throw XCTSkip("Span not available") }

#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
var source = Data()
var span = source.mutableBytes
XCTAssertTrue(span.isEmpty)
Expand All @@ -1765,21 +1752,18 @@ class DataTests : XCTestCase {
var sub = span.extracting(i..<i+1)
sub.storeBytes(of: v, as: UInt8.self)
XCTAssertEqual(source[i], v)
#endif
}

func test_InlineSliceDataMutableRawSpan() throws {
guard #available(FoundationSpan 6.2, *) else { throw XCTSkip("Span not available") }

#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
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)
#endif
}

func test_LargeSliceDataMutableRawSpan() throws {
Expand All @@ -1793,7 +1777,6 @@ class DataTests : XCTestCase {
#error("This test needs updating")
#endif

#if compiler(>=6.2) && $InoutLifetimeDependence && $LifetimeDependenceMutableAccessors
var source = Data(repeating: 0, count: count).dropFirst()
XCTAssertNotEqual(source.startIndex, 0)
count = source.count
Expand All @@ -1803,7 +1786,6 @@ class DataTests : XCTestCase {
span.storeBytes(of: -1, toByteOffset: i, as: Int8.self)
XCTAssertEqual(source[i], 0)
XCTAssertEqual(source[i+1], .max)
#endif
}

#if false // FIXME: XCTest doesn't support crash tests yet rdar://20195010&22387653
Expand Down