Skip to content

Commit 5f5c75d

Browse files
committed
[abi] preserve original implementations of withMemoryRebound
1 parent c2dfb94 commit 5f5c75d

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,12 @@ extension Unsafe${Mutable}BufferPointer {
699699
/// - Returns: The return value, if any, of the `body` closure parameter.
700700
@inlinable // unsafe-performance
701701
@_alwaysEmitIntoClient
702+
// This custom silgen name is chosen to not interfere with the old ABI
703+
% if Mutable:
704+
@_silgen_name("$_swift_se0333_UnsafeMutableBufferPointer_withMemoryRebound")
705+
% else:
706+
@_silgen_name("$_swift_se0333_UnsafeBufferPointer_withMemoryRebound")
707+
% end
702708
public func withMemoryRebound<T, Result>(
703709
to type: T.Type,
704710
_ body: (_ buffer: ${Self}<T>) throws -> Result
@@ -727,6 +733,33 @@ extension Unsafe${Mutable}BufferPointer {
727733
return try body(.init(start: .init(base), count: newCount))
728734
}
729735

736+
// This unavailable implementation uses the expected mangled name
737+
// of `withMemoryRebound`, and provides an entry point for any
738+
// binary compiled against the stlib binary for Swift 5.6 and older.
739+
@available(*, unavailable)
740+
% if Mutable:
741+
@_silgen_name("$sSr17withMemoryRebound2to_qd_0_qd__m_qd_0_Sryqd__GKXEtKr0_lF")
742+
% else:
743+
@_silgen_name("$sSR17withMemoryRebound2to_qd_0_qd__m_qd_0_SRyqd__GKXEtKr0_lF")
744+
% end
745+
public func _legacy_se0333_withMemoryRebound<T, Result>(
746+
to type: T.Type, _ body: (${Self}<T>) throws -> Result
747+
) rethrows -> Result {
748+
if let base = _position {
749+
_debugPrecondition(MemoryLayout<Element>.stride == MemoryLayout<T>.stride)
750+
Builtin.bindMemory(base._rawValue, count._builtinWordValue, T.self)
751+
defer {
752+
Builtin.bindMemory(base._rawValue, count._builtinWordValue, Element.self)
753+
}
754+
755+
return try body(${Self}<T>(
756+
start: Unsafe${Mutable}Pointer<T>(base._rawValue), count: count))
757+
}
758+
else {
759+
return try body(${Self}<T>(start: nil, count: 0))
760+
}
761+
}
762+
730763
/// A pointer to the first element of the buffer.
731764
///
732765
/// If the `baseAddress` of this buffer is `nil`, the count is zero. However,

stdlib/public/core/UnsafePointer.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ public struct UnsafePointer<Pointee>: _Pointer {
303303
/// - Returns: The return value, if any, of the `body` closure parameter.
304304
@inlinable
305305
@_alwaysEmitIntoClient
306+
// This custom silgen name is chosen to not interfere with the old ABI
307+
@_silgen_name("_swift_se0333_UnsafePointer_withMemoryRebound")
306308
public func withMemoryRebound<T, Result>(
307309
to type: T.Type, capacity count: Int,
308310
_ body: (_ pointer: UnsafePointer<T>) throws -> Result
@@ -318,6 +320,21 @@ public struct UnsafePointer<Pointee>: _Pointer {
318320
return try body(.init(_rawValue))
319321
}
320322

323+
// This unavailable implementation uses the expected mangled name
324+
// of `withMemoryRebound`, and provides an entry point for any
325+
// binary compiled against the stlib binary for Swift 5.6 and older.
326+
@available(*, unavailable)
327+
@_silgen_name("$sSP17withMemoryRebound2to8capacity_qd_0_qd__m_Siqd_0_SPyqd__GKXEtKr0_lF")
328+
public func _legacy_se0333_withMemoryRebound<T, Result>(to type: T.Type, capacity count: Int,
329+
_ body: (UnsafePointer<T>) throws -> Result
330+
) rethrows -> Result {
331+
Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
332+
defer {
333+
Builtin.bindMemory(_rawValue, count._builtinWordValue, Pointee.self)
334+
}
335+
return try body(.init(_rawValue))
336+
}
337+
321338
/// Accesses the pointee at the specified offset from this pointer.
322339
///
323340
///
@@ -970,6 +987,8 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
970987
/// - Returns: The return value, if any, of the `body` closure parameter.
971988
@inlinable
972989
@_alwaysEmitIntoClient
990+
// This custom silgen name is chosen to not interfere with the old ABI
991+
@_silgen_name("$_swift_se0333_UnsafeMutablePointer_withMemoryRebound")
973992
public func withMemoryRebound<T, Result>(
974993
to type: T.Type, capacity count: Int,
975994
_ body: (_ pointer: UnsafeMutablePointer<T>) throws -> Result
@@ -985,6 +1004,21 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
9851004
return try body(.init(_rawValue))
9861005
}
9871006

1007+
// This unavailable implementation uses the expected mangled name
1008+
// of `withMemoryRebound`, and provides an entry point for any
1009+
// binary compiled against the stlib binary for Swift 5.6 and older.
1010+
@available(*, unavailable)
1011+
@_silgen_name("$sSp17withMemoryRebound2to8capacity_qd_0_qd__m_Siqd_0_Spyqd__GKXEtKr0_lF")
1012+
public func _legacy_se0333_withMemoryRebound<T, Result>(to type: T.Type, capacity count: Int,
1013+
_ body: (UnsafeMutablePointer<T>) throws -> Result
1014+
) rethrows -> Result {
1015+
Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
1016+
defer {
1017+
Builtin.bindMemory(_rawValue, count._builtinWordValue, Pointee.self)
1018+
}
1019+
return try body(.init(_rawValue))
1020+
}
1021+
9881022
/// Accesses the pointee at the specified offset from this pointer.
9891023
///
9901024
/// For a pointer `p`, the memory at `p + i` must be initialized when reading

0 commit comments

Comments
 (0)