Skip to content

[stdlib] make swiftinterfaces compatible with older toolchains #72365

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
Mar 16, 2024
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
36 changes: 36 additions & 0 deletions stdlib/public/core/FloatingPointParsing.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ extension ${Self}: LosslessStringConvertible {
self.init(Substring(text))
} else {
self = 0.0
#if hasFeature(TypedThrows)
let success = _withUnprotectedUnsafeMutablePointer(to: &self) { p -> Bool in
text.withCString { chars -> Bool in
switch chars[0] {
Expand All @@ -184,6 +185,23 @@ extension ${Self}: LosslessStringConvertible {
return endPtr != nil && endPtr![0] == 0
}
}
#else
let success = __abi_se0413_withUnsafeMutablePointer(to: &self) { p -> Bool in
text.withCString { chars -> Bool in
switch chars[0] {
case 9, 10, 11, 12, 13, 32:
return false // Reject leading whitespace
case 0:
return false // Reject empty string
default:
break
}
let endPtr = _swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(chars, p)
// Succeed only if endPtr points to end of C string
return endPtr != nil && endPtr![0] == 0
}
}
#endif
if !success {
return nil
}
Expand All @@ -198,6 +216,7 @@ extension ${Self}: LosslessStringConvertible {
@available(SwiftStdlib 5.3, *)
public init?(_ text: Substring) {
self = 0.0
#if hasFeature(TypedThrows)
let success = _withUnprotectedUnsafeMutablePointer(to: &self) { p -> Bool in
text.withCString { chars -> Bool in
switch chars[0] {
Expand All @@ -213,6 +232,23 @@ extension ${Self}: LosslessStringConvertible {
return endPtr != nil && endPtr![0] == 0
}
}
#else
let success = __abi_se0413_withUnsafeMutablePointer(to: &self) { p -> Bool in
text.withCString { chars -> Bool in
switch chars[0] {
case 9, 10, 11, 12, 13, 32:
return false // Reject leading whitespace
case 0:
return false // Reject empty string
default:
break
}
let endPtr = _swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(chars, p)
// Succeed only if endPtr points to end of C string
return endPtr != nil && endPtr![0] == 0
}
}
#endif
if !success {
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions stdlib/public/core/Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,15 @@ public struct SystemRandomNumberGenerator: RandomNumberGenerator, Sendable {
@inlinable
public mutating func next() -> UInt64 {
var random: UInt64 = 0
#if hasFeature(TypedThrows)
_withUnprotectedUnsafeMutablePointer(to: &random) {
swift_stdlib_random($0, MemoryLayout<UInt64>.size)
}
#else
__abi_se0413_withUnsafeMutablePointer(to: &random) {
swift_stdlib_random($0, MemoryLayout<UInt64>.size)
}
#endif
return random
}
}
8 changes: 8 additions & 0 deletions stdlib/public/core/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,19 @@ func _stdlib_atomicInitializeARCRef(
let desiredPtr = unmanaged.toOpaque()
let rawTarget = UnsafeMutableRawPointer(target).assumingMemoryBound(
to: Optional<UnsafeRawPointer>.self)
#if hasFeature(TypedThrows)
let wonRace = withUnsafeMutablePointer(to: &expected) {
_stdlib_atomicCompareExchangeStrongPtr(
object: rawTarget, expected: $0, desired: desiredPtr
)
}
#else
let wonRace = __abi_se0413_withUnsafeMutablePointer(to: &expected) {
_stdlib_atomicCompareExchangeStrongPtr(
object: rawTarget, expected: $0, desired: desiredPtr
)
}
#endif
if !wonRace {
// Some other thread initialized the value. Balance the retain that we
// performed on 'desired'.
Expand Down
14 changes: 14 additions & 0 deletions stdlib/public/core/SmallString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ extension _SmallString {
) rethrows -> Result {
let count = self.count
var raw = self.zeroTerminatedRawCodeUnits
#if hasFeature(TypedThrows)
return try Swift._withUnprotectedUnsafeBytes(of: &raw) {
let rawPtr = $0.baseAddress._unsafelyUnwrappedUnchecked
// Rebind the underlying (UInt64, UInt64) tuple to UInt8 for the
Expand All @@ -234,6 +235,19 @@ extension _SmallString {
}
return try f(UnsafeBufferPointer(_uncheckedStart: ptr, count: count))
}
#else
return try Swift.__abi_se0413_withUnsafeBytes(of: &raw) {
let rawPtr = $0.baseAddress._unsafelyUnwrappedUnchecked
// Rebind the underlying (UInt64, UInt64) tuple to UInt8 for the
// duration of the closure. Accessing self after this rebind is undefined.
let ptr = rawPtr.bindMemory(to: UInt8.self, capacity: count)
defer {
// Restore the memory type of self._storage
_ = rawPtr.bindMemory(to: RawBitPattern.self, capacity: 1)
}
return try f(UnsafeBufferPointer(_uncheckedStart: ptr, count: count))
}
#endif
}

// Overwrite stored code units, including uninitialized. `f` should return the
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/core/UnicodeScalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,19 @@ extension Unicode.Scalar {

// The first code unit is in the least significant byte of codeUnits.
codeUnits = codeUnits.littleEndian
#if hasFeature(TypedThrows)
return try Swift._withUnprotectedUnsafePointer(to: &codeUnits) {
return try $0.withMemoryRebound(to: UInt8.self, capacity: 4) {
return try body(UnsafeBufferPointer(start: $0, count: utf8Count))
}
}
#else
return try Swift.__abi_se0413_withUnsafePointer(to: &codeUnits) {
return try $0.withMemoryRebound(to: UInt8.self, capacity: 4) {
return try body(UnsafeBufferPointer(start: $0, count: utf8Count))
}
}
#endif
}
}

Expand Down
10 changes: 10 additions & 0 deletions stdlib/public/core/UnsafeRawPointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1378,13 +1378,23 @@ public struct UnsafeMutableRawPointer: _Pointer {
"storeBytes to misaligned raw pointer")

var temp = value
#if hasFeature(TypedThrows)
withUnsafeMutablePointer(to: &temp) { source in
let rawSrc = UnsafeMutableRawPointer(source)._rawValue
// FIXME: to be replaced by _memcpy when conversions are implemented.
Builtin.int_memcpy_RawPointer_RawPointer_Int64(
(self + offset)._rawValue, rawSrc, UInt64(MemoryLayout<T>.size)._value,
/*volatile:*/ false._value)
}
#else
__abi_se0413_withUnsafeMutablePointer(to: &temp) { source in
let rawSrc = UnsafeMutableRawPointer(source)._rawValue
// FIXME: to be replaced by _memcpy when conversions are implemented.
Builtin.int_memcpy_RawPointer_RawPointer_Int64(
(self + offset)._rawValue, rawSrc, UInt64(MemoryLayout<T>.size)._value,
/*volatile:*/ false._value)
}
#endif
}

/// Copies the specified number of bytes from the given raw pointer's memory
Expand Down
8 changes: 8 additions & 0 deletions stdlib/public/core/VarArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,19 @@ public func _encodeBitsAsWords<T>(_ x: T) -> [Int] {
_internalInvariant(!result.isEmpty)
var tmp = x
// FIXME: use UnsafeMutablePointer.assign(from:) instead of memcpy.
#if hasFeature(TypedThrows)
_withUnprotectedUnsafeMutablePointer(to: &tmp) {
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
src: $0,
size: UInt(MemoryLayout<T>.size))
}
#else
__abi_se0413_withUnsafeMutablePointer(to: &tmp) {
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
src: $0,
size: UInt(MemoryLayout<T>.size))
}
#endif
return result
}

Expand Down