Skip to content

Fix issue with older compilers not handling MutableSpan code #81289

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 1 commit into from
May 5, 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
12 changes: 12 additions & 0 deletions stdlib/public/core/Span/MutableRawSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,14 @@ extension MutableRawSpan {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(first maxLength: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(maxLength >= 0, "Can't have a prefix of negative length")
let newCount = min(maxLength, byteCount)
let newSpan = unsafe Self(_unchecked: _pointer, byteCount: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}

/// Returns a span over all but the given number of trailing elements.
Expand All @@ -611,11 +615,15 @@ extension MutableRawSpan {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(droppingLast k: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(k >= 0, "Can't drop a negative number of elements")
let droppedCount = min(k, byteCount)
let newCount = byteCount &- droppedCount
let newSpan = unsafe Self(_unchecked: _pointer, byteCount: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}

/// Returns a span containing the final elements of the span,
Expand Down Expand Up @@ -660,11 +668,15 @@ extension MutableRawSpan {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(droppingFirst k: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(k >= 0, "Can't drop a negative number of bytes")
let droppedCount = min(k, byteCount)
let newStart = unsafe _pointer?.advanced(by: droppedCount)
let newCount = byteCount &- droppedCount
let newSpan = unsafe Self(_unchecked: newStart, byteCount: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}
}
16 changes: 16 additions & 0 deletions stdlib/public/core/Span/MutableSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,14 @@ extension MutableSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(first maxLength: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(maxLength >= 0, "Can't have a prefix of negative length")
let newCount = min(maxLength, count)
let newSpan = unsafe Self(_unchecked: _pointer, count: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}

/// Returns a span over all but the given number of trailing elements.
Expand All @@ -848,11 +852,15 @@ extension MutableSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(droppingLast k: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(k >= 0, "Can't drop a negative number of elements")
let droppedCount = min(k, count)
let newCount = count &- droppedCount
let newSpan = unsafe Self(_unchecked: _pointer, count: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}

/// Returns a span containing the final elements of the span,
Expand All @@ -873,12 +881,16 @@ extension MutableSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(last maxLength: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(maxLength >= 0, "Can't have a suffix of negative length")
let newCount = min(maxLength, count)
let offset = (count &- newCount) * MemoryLayout<Element>.stride
let newStart = unsafe _pointer?.advanced(by: offset)
let newSpan = unsafe Self(_unchecked: newStart, count: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}

/// Returns a span over all but the given number of initial elements.
Expand All @@ -898,12 +910,16 @@ extension MutableSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(&self)
mutating public func extracting(droppingFirst k: Int) -> Self {
#if compiler(>=5.3) && hasFeature(SendableCompletionHandlers)
_precondition(k >= 0, "Can't drop a negative number of elements")
let droppedCount = min(k, count)
let offset = droppedCount * MemoryLayout<Element>.stride
let newStart = unsafe _pointer?.advanced(by: offset)
let newCount = count &- droppedCount
let newSpan = unsafe Self(_unchecked: newStart, count: newCount)
return unsafe _overrideLifetime(newSpan, mutating: &self)
#else
fatalError("Unsupported compiler")
#endif
}
}