Skip to content

Commit 8459d06

Browse files
committed
[stdlib] make swiftinterfaces compatible with older toolchains
1 parent bae6450 commit 8459d06

File tree

7 files changed

+90
-0
lines changed

7 files changed

+90
-0
lines changed

stdlib/public/core/FloatingPointParsing.swift.gyb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ extension ${Self}: LosslessStringConvertible {
169169
self.init(Substring(text))
170170
} else {
171171
self = 0.0
172+
#if hasFeature(TypedThrows)
172173
let success = _withUnprotectedUnsafeMutablePointer(to: &self) { p -> Bool in
173174
text.withCString { chars -> Bool in
174175
switch chars[0] {
@@ -184,6 +185,23 @@ extension ${Self}: LosslessStringConvertible {
184185
return endPtr != nil && endPtr![0] == 0
185186
}
186187
}
188+
#else
189+
let success = __abi_se0413_withUnsafeMutablePointer(to: &self) { p -> Bool in
190+
text.withCString { chars -> Bool in
191+
switch chars[0] {
192+
case 9, 10, 11, 12, 13, 32:
193+
return false // Reject leading whitespace
194+
case 0:
195+
return false // Reject empty string
196+
default:
197+
break
198+
}
199+
let endPtr = _swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(chars, p)
200+
// Succeed only if endPtr points to end of C string
201+
return endPtr != nil && endPtr![0] == 0
202+
}
203+
}
204+
#endif
187205
if !success {
188206
return nil
189207
}
@@ -198,6 +216,7 @@ extension ${Self}: LosslessStringConvertible {
198216
@available(SwiftStdlib 5.3, *)
199217
public init?(_ text: Substring) {
200218
self = 0.0
219+
#if hasFeature(TypedThrows)
201220
let success = _withUnprotectedUnsafeMutablePointer(to: &self) { p -> Bool in
202221
text.withCString { chars -> Bool in
203222
switch chars[0] {
@@ -213,6 +232,23 @@ extension ${Self}: LosslessStringConvertible {
213232
return endPtr != nil && endPtr![0] == 0
214233
}
215234
}
235+
#else
236+
let success = __abi_se0413_withUnsafeMutablePointer(to: &self) { p -> Bool in
237+
text.withCString { chars -> Bool in
238+
switch chars[0] {
239+
case 9, 10, 11, 12, 13, 32:
240+
return false // Reject leading whitespace
241+
case 0:
242+
return false // Reject empty string
243+
default:
244+
break
245+
}
246+
let endPtr = _swift_stdlib_strto${cFuncSuffix2[bits]}_clocale(chars, p)
247+
// Succeed only if endPtr points to end of C string
248+
return endPtr != nil && endPtr![0] == 0
249+
}
250+
}
251+
#endif
216252
if !success {
217253
return nil
218254
}

stdlib/public/core/Random.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,15 @@ public struct SystemRandomNumberGenerator: RandomNumberGenerator, Sendable {
158158
@inlinable
159159
public mutating func next() -> UInt64 {
160160
var random: UInt64 = 0
161+
#if hasFeature(TypedThrows)
161162
_withUnprotectedUnsafeMutablePointer(to: &random) {
162163
swift_stdlib_random($0, MemoryLayout<UInt64>.size)
163164
}
165+
#else
166+
__abi_se0413_withUnsafeMutablePointer(to: &random) {
167+
swift_stdlib_random($0, MemoryLayout<UInt64>.size)
168+
}
169+
#endif
164170
return random
165171
}
166172
}

stdlib/public/core/Runtime.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,19 @@ func _stdlib_atomicInitializeARCRef(
139139
let desiredPtr = unmanaged.toOpaque()
140140
let rawTarget = UnsafeMutableRawPointer(target).assumingMemoryBound(
141141
to: Optional<UnsafeRawPointer>.self)
142+
#if hasFeature(TypedThrows)
142143
let wonRace = withUnsafeMutablePointer(to: &expected) {
143144
_stdlib_atomicCompareExchangeStrongPtr(
144145
object: rawTarget, expected: $0, desired: desiredPtr
145146
)
146147
}
148+
#else
149+
let wonRace = __abi_se0413_withUnsafeMutablePointer(to: &expected) {
150+
_stdlib_atomicCompareExchangeStrongPtr(
151+
object: rawTarget, expected: $0, desired: desiredPtr
152+
)
153+
}
154+
#endif
147155
if !wonRace {
148156
// Some other thread initialized the value. Balance the retain that we
149157
// performed on 'desired'.

stdlib/public/core/SmallString.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ extension _SmallString {
223223
) rethrows -> Result {
224224
let count = self.count
225225
var raw = self.zeroTerminatedRawCodeUnits
226+
#if hasFeature(TypedThrows)
226227
return try Swift._withUnprotectedUnsafeBytes(of: &raw) {
227228
let rawPtr = $0.baseAddress._unsafelyUnwrappedUnchecked
228229
// Rebind the underlying (UInt64, UInt64) tuple to UInt8 for the
@@ -234,6 +235,19 @@ extension _SmallString {
234235
}
235236
return try f(UnsafeBufferPointer(_uncheckedStart: ptr, count: count))
236237
}
238+
#else
239+
return try Swift.__abi_se0413_withUnsafeBytes(of: &raw) {
240+
let rawPtr = $0.baseAddress._unsafelyUnwrappedUnchecked
241+
// Rebind the underlying (UInt64, UInt64) tuple to UInt8 for the
242+
// duration of the closure. Accessing self after this rebind is undefined.
243+
let ptr = rawPtr.bindMemory(to: UInt8.self, capacity: count)
244+
defer {
245+
// Restore the memory type of self._storage
246+
_ = rawPtr.bindMemory(to: RawBitPattern.self, capacity: 1)
247+
}
248+
return try f(UnsafeBufferPointer(_uncheckedStart: ptr, count: count))
249+
}
250+
#endif
237251
}
238252

239253
// Overwrite stored code units, including uninitialized. `f` should return the

stdlib/public/core/UnicodeScalar.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,19 @@ extension Unicode.Scalar {
535535

536536
// The first code unit is in the least significant byte of codeUnits.
537537
codeUnits = codeUnits.littleEndian
538+
#if hasFeature(TypedThrows)
538539
return try Swift._withUnprotectedUnsafePointer(to: &codeUnits) {
539540
return try $0.withMemoryRebound(to: UInt8.self, capacity: 4) {
540541
return try body(UnsafeBufferPointer(start: $0, count: utf8Count))
541542
}
542543
}
544+
#else
545+
return try Swift.__abi_se0413_withUnsafePointer(to: &codeUnits) {
546+
return try $0.withMemoryRebound(to: UInt8.self, capacity: 4) {
547+
return try body(UnsafeBufferPointer(start: $0, count: utf8Count))
548+
}
549+
}
550+
#endif
543551
}
544552
}
545553

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,13 +1378,23 @@ public struct UnsafeMutableRawPointer: _Pointer {
13781378
"storeBytes to misaligned raw pointer")
13791379

13801380
var temp = value
1381+
#if hasFeature(TypedThrows)
13811382
withUnsafeMutablePointer(to: &temp) { source in
13821383
let rawSrc = UnsafeMutableRawPointer(source)._rawValue
13831384
// FIXME: to be replaced by _memcpy when conversions are implemented.
13841385
Builtin.int_memcpy_RawPointer_RawPointer_Int64(
13851386
(self + offset)._rawValue, rawSrc, UInt64(MemoryLayout<T>.size)._value,
13861387
/*volatile:*/ false._value)
13871388
}
1389+
#else
1390+
__abi_se0413_withUnsafeMutablePointer(to: &temp) { source in
1391+
let rawSrc = UnsafeMutableRawPointer(source)._rawValue
1392+
// FIXME: to be replaced by _memcpy when conversions are implemented.
1393+
Builtin.int_memcpy_RawPointer_RawPointer_Int64(
1394+
(self + offset)._rawValue, rawSrc, UInt64(MemoryLayout<T>.size)._value,
1395+
/*volatile:*/ false._value)
1396+
}
1397+
#endif
13881398
}
13891399

13901400
/// Copies the specified number of bytes from the given raw pointer's memory

stdlib/public/core/VarArgs.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,19 @@ public func _encodeBitsAsWords<T>(_ x: T) -> [Int] {
215215
_internalInvariant(!result.isEmpty)
216216
var tmp = x
217217
// FIXME: use UnsafeMutablePointer.assign(from:) instead of memcpy.
218+
#if hasFeature(TypedThrows)
218219
_withUnprotectedUnsafeMutablePointer(to: &tmp) {
219220
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
220221
src: $0,
221222
size: UInt(MemoryLayout<T>.size))
222223
}
224+
#else
225+
__abi_se0413_withUnsafeMutablePointer(to: &tmp) {
226+
_memcpy(dest: UnsafeMutablePointer(result._baseAddressIfContiguous!),
227+
src: $0,
228+
size: UInt(MemoryLayout<T>.size))
229+
}
230+
#endif
223231
return result
224232
}
225233

0 commit comments

Comments
 (0)