Skip to content

[stdlib, 6.2] add missing computed properties (again) #82422

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
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/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,18 @@ extension Unsafe${Mutable}BufferPointer where Element: ~Copyable {
return unsafe _overrideLifetime(span, borrowing: self)
}
}
%if Mutable:

@unsafe
@available(SwiftStdlib 6.2, *)
public var mutableSpan: MutableSpan<Element> {
@lifetime(borrow self)
@_alwaysEmitIntoClient
get {
unsafe MutableSpan(_unsafeElements: self)
}
}
%end
}

extension Unsafe${Mutable}BufferPointer {
Expand Down
10 changes: 10 additions & 0 deletions stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,16 @@ extension Unsafe${Mutable}RawBufferPointer {
}
}

@unsafe
@available(SwiftStdlib 6.2, *)
public var mutableBytes: MutableRawSpan {
@lifetime(borrow self)
@_alwaysEmitIntoClient
get {
unsafe MutableRawSpan(_unsafeBytes: self)
}
}

% end
@_alwaysEmitIntoClient
public func withContiguousStorageIfAvailable<R>(
Expand Down
2 changes: 2 additions & 0 deletions test/abi/macOS/arm64/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,8 @@ Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
Added: _$sSw12mutableBytess14MutableRawSpanVvr
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. Could the property itself have been @_alwaysEmitIntoClient to avoid these symbols, or no?

Copy link
Contributor Author

@glessard glessard Jun 25, 2025

Choose a reason for hiding this comment

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

Unfortunately computed properties for escapable types produce symbols to support keypaths. This is fixed for non-escapable types: their computed properties do not produce symbols.
Actually that's right, moving @_alwaysEmitIntoClient to the property rather than the accessor makes the symbol go away for these.

Copy link
Member

Choose a reason for hiding this comment

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

Yay!

Copy link
Member

Choose a reason for hiding this comment

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

@glessard can you follow up with a PR to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Of course! I've started searching for how many other span/mutableSpan/etc symbols I missed this way…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: followup here.


// _SwiftifyInfo enum for _SwiftifyImports macro
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC
Expand Down
2 changes: 2 additions & 0 deletions test/abi/macOS/x86_64/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@ Added: _$ss10ArraySliceV11mutableSpans07MutableD0VyxGvr
Added: _$ss15ContiguousArrayV11mutableSpans07MutableD0VyxGvr
Added: _$ss11InlineArrayVsRi__rlE11mutableSpans07MutableD0Vyq_Gvr
Added: _$ss15CollectionOfOneV11mutableSpans07MutableE0VyxGvr
Added: _$sSrsRi_zrlE11mutableSpans07MutableB0VyxGvr
Added: _$sSw12mutableBytess14MutableRawSpanVvr

// _SwiftifyInfo enum for _SwiftifyImports macro
Added: _$ss13_SwiftifyExprO5paramyABSicABmFWC
Expand Down
23 changes: 23 additions & 0 deletions test/stdlib/Span/MutableRawSpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,26 @@ suite.test("extracting suffixes")
expectEqual(span.extracting(droppingFirst: 1).byteCount, b.count)
}
}

suite.test("MutableRawSpan from UnsafeMutableRawBufferPointer")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }

let capacity = 4
let b = UnsafeMutableRawBufferPointer.allocate(
byteCount: capacity*MemoryLayout<Int64>.stride,
alignment: MemoryLayout<Int64>.alignment
)
defer {
b.deallocate()
}
_ = b.initializeMemory(as: Int64.self, fromContentsOf: 0..<Int64(capacity))

var span = b.mutableBytes
span.storeBytes(of: 3, toByteOffset: 10, as: UInt16.self)

_ = consume span

let v = b.load(fromByteOffset: 8, as: Int64.self)
expectNotEqual(v, 1)
}
22 changes: 22 additions & 0 deletions test/stdlib/Span/MutableSpanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -720,3 +720,25 @@ suite.test("extracting suffixes")
expectEqual(span.extracting(droppingFirst: 1).count, b.count)
}
}

suite.test("MutableSpan from UnsafeMutableBufferPointer")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }

let capacity = 4
let b = UnsafeMutableBufferPointer<Int>.allocate(capacity: capacity)
defer {
b.deallocate()
}
_ = b.initialize(fromContentsOf: 0..<capacity)

var span = b.mutableSpan
expectEqual(span.count, capacity)

span.swapAt(0, 3)
span.swapAt(1, 2)

_ = consume span

expectTrue(b.elementsEqual((0..<capacity).reversed()))
}