Skip to content

Commit 8c91b2a

Browse files
committed
Apply Fix-Its for @safe/@unsafe throughout more libraries
1 parent 501b6cd commit 8c91b2a

28 files changed

+204
-204
lines changed

stdlib/private/SwiftPrivate/IO.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Android
2828
#elseif canImport(WASILibc)
2929
import WASILibc
3030
#endif
31-
let (platform_read, platform_write, platform_close) = (read, write, close)
31+
@unsafe let (platform_read, platform_write, platform_close) = (read, write, close)
3232
#endif
3333

3434
#if os(Windows)
@@ -121,7 +121,7 @@ public struct _FDInputStream {
121121
return nil
122122
}
123123

124-
public mutating func read() {
124+
@safe(unchecked) public mutating func read() {
125125
let minFree = 128
126126
var bufferFree = _buffer.count - _bufferUsed
127127
if bufferFree < minFree {
@@ -213,7 +213,7 @@ public struct _FDOutputStream : TextOutputStream {
213213
self.fd = fd
214214
}
215215

216-
public mutating func write(_ string: String) {
216+
@safe(unchecked) public mutating func write(_ string: String) {
217217
let utf8CStr = string.utf8CString
218218
utf8CStr.withUnsafeBufferPointer {
219219
(utf8CStr) -> Void in

stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public struct _stdlib_ShardedAtomicCounter {
2828
// Using an array causes retains/releases, which create contention on the
2929
// reference count.
3030
// FIXME: guard every element against false sharing.
31-
var _shardsPtr: UnsafeMutablePointer<Int>
31+
@unsafe var _shardsPtr: UnsafeMutablePointer<Int>
3232
var _shardsCount: Int
3333

34-
public init() {
34+
@safe(unchecked) public init() {
3535
let hardwareConcurrency = _stdlib_getHardwareConcurrency()
3636
let count = max(8, hardwareConcurrency * hardwareConcurrency)
3737
let shards = UnsafeMutablePointer<Int>.allocate(capacity: count)
@@ -42,19 +42,19 @@ public struct _stdlib_ShardedAtomicCounter {
4242
self._shardsCount = count
4343
}
4444

45-
public func `deinit`() {
45+
@safe(unchecked) public func `deinit`() {
4646
self._shardsPtr.deinitialize(count: self._shardsCount)
4747
self._shardsPtr.deallocate()
4848
}
4949

50-
public func add(_ operand: Int, randomInt: Int) {
50+
@safe(unchecked) public func add(_ operand: Int, randomInt: Int) {
5151
let shardIndex = Int(UInt(bitPattern: randomInt) % UInt(self._shardsCount))
5252
_ = _swift_stdlib_atomicFetchAddInt(
5353
object: self._shardsPtr + shardIndex, operand: operand)
5454
}
5555

5656
// FIXME: non-atomic as a whole!
57-
public func getSum() -> Int {
57+
@safe(unchecked) public func getSum() -> Int {
5858
var result = 0
5959
let shards = self._shardsPtr
6060
let count = self._shardsCount

stdlib/private/SwiftPrivate/SwiftPrivate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public func scatter<T>(_ a: [T], _ idx: [Int]) -> [T] {
5757
return result
5858
}
5959

60-
public func withArrayOfCStrings<R>(
60+
@unsafe public func withArrayOfCStrings<R>(
6161
_ args: [String], _ body: ([UnsafeMutablePointer<CChar>?]) -> R
6262
) -> R {
6363
let argsCounts = Array(args.map { $0.utf8.count + 1 })

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,31 @@ public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus {
188188
#if os(Linux)
189189
typealias _stdlib_posix_spawn_file_actions_t = posix_spawn_file_actions_t
190190
#else
191-
typealias _stdlib_posix_spawn_file_actions_t = posix_spawn_file_actions_t?
191+
@unsafe typealias _stdlib_posix_spawn_file_actions_t = posix_spawn_file_actions_t?
192192
#endif
193193

194-
@_silgen_name("_stdlib_posix_spawn_file_actions_init")
194+
@unsafe @_silgen_name("_stdlib_posix_spawn_file_actions_init")
195195
internal func _stdlib_posix_spawn_file_actions_init(
196196
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>
197197
) -> CInt
198198

199-
@_silgen_name("_stdlib_posix_spawn_file_actions_destroy")
199+
@unsafe @_silgen_name("_stdlib_posix_spawn_file_actions_destroy")
200200
internal func _stdlib_posix_spawn_file_actions_destroy(
201201
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>
202202
) -> CInt
203203

204-
@_silgen_name("_stdlib_posix_spawn_file_actions_addclose")
204+
@unsafe @_silgen_name("_stdlib_posix_spawn_file_actions_addclose")
205205
internal func _stdlib_posix_spawn_file_actions_addclose(
206206
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>,
207207
_ filedes: CInt) -> CInt
208208

209-
@_silgen_name("_stdlib_posix_spawn_file_actions_adddup2")
209+
@unsafe @_silgen_name("_stdlib_posix_spawn_file_actions_adddup2")
210210
internal func _stdlib_posix_spawn_file_actions_adddup2(
211211
_ file_actions: UnsafeMutablePointer<_stdlib_posix_spawn_file_actions_t>,
212212
_ filedes: CInt,
213213
_ newfiledes: CInt) -> CInt
214214

215-
@_silgen_name("_stdlib_posix_spawn")
215+
@unsafe @_silgen_name("_stdlib_posix_spawn")
216216
internal func _stdlib_posix_spawn(
217217
_ pid: UnsafeMutablePointer<pid_t>?,
218218
_ file: UnsafePointer<Int8>,
@@ -223,7 +223,7 @@ internal func _stdlib_posix_spawn(
223223
#endif
224224

225225
/// Calls POSIX `pipe()`.
226-
func posixPipe() -> (readFD: CInt, writeFD: CInt) {
226+
@safe(unchecked) func posixPipe() -> (readFD: CInt, writeFD: CInt) {
227227
var fds: [CInt] = [ -1, -1 ]
228228
if pipe(&fds) != 0 {
229229
preconditionFailure("pipe() failed")
@@ -257,7 +257,7 @@ func print(_ s: String) {
257257

258258
/// Start the same executable as a child process, redirecting its stdout and
259259
/// stderr.
260-
public func spawnChild(_ args: [String])
260+
@safe(unchecked) public func spawnChild(_ args: [String])
261261
-> (pid: pid_t, stdinFD: CInt, stdoutFD: CInt, stderrFD: CInt) {
262262
// The stdout, stdin, and stderr from the child process will be redirected
263263
// to these pipes.
@@ -450,14 +450,14 @@ internal func _make_posix_spawn_file_actions_t()
450450
return posix_spawn_file_actions_t()
451451
}
452452
#else
453-
internal func _make_posix_spawn_file_actions_t()
453+
@unsafe internal func _make_posix_spawn_file_actions_t()
454454
-> _stdlib_posix_spawn_file_actions_t {
455455
return nil
456456
}
457457
#endif
458458
#endif
459459

460-
public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus {
460+
@safe(unchecked) public func posixWaitpid(_ pid: pid_t) -> ProcessTerminationStatus {
461461
var status: CInt = 0
462462
#if os(Cygwin)
463463
withUnsafeMutablePointer(to: &status) {

stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import WASILibc
2525
import CRT
2626
#endif
2727

28-
public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
28+
@safe(unchecked) public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
2929
#if os(Android) || os(Haiku) || os(Windows) || os(WASI)
3030
preconditionFailure("mkstemps doesn't work on your platform")
3131
#else
@@ -78,7 +78,7 @@ public struct _stdlib_fd_set {
7878
~UInt(1 << (fdInt % _stdlib_fd_set._wordBits))
7979
}
8080

81-
public mutating func zero() {
81+
@safe(unchecked) public mutating func zero() {
8282
let count = _data.count
8383
return _data.withUnsafeMutableBufferPointer {
8484
(_data) in
@@ -90,7 +90,7 @@ public struct _stdlib_fd_set {
9090
}
9191
}
9292

93-
public func _stdlib_select(
93+
@unsafe public func _stdlib_select(
9494
_ readfds: inout _stdlib_fd_set, _ writefds: inout _stdlib_fd_set,
9595
_ errorfds: inout _stdlib_fd_set, _ timeout: UnsafeMutablePointer<timeval>?
9696
) -> CInt {
@@ -126,7 +126,7 @@ public func _stdlib_select(
126126

127127

128128
/// Swift-y wrapper around pipe(2)
129-
public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) {
129+
@safe(unchecked) public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) {
130130
var fds: [CInt] = [0, 0]
131131
let ret = fds.withUnsafeMutableBufferPointer { unsafeFds -> CInt in
132132
#if os(Windows)

stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class ThreadBlockContext {
3636
/// Execute the block, and return an `UnsafeMutablePointer` to memory
3737
/// allocated with `UnsafeMutablePointer.alloc` containing the result of the
3838
/// block.
39-
func run() -> UnsafeMutableRawPointer { fatalError("abstract") }
39+
@unsafe func run() -> UnsafeMutableRawPointer { fatalError("abstract") }
4040
}
4141

4242
internal class ThreadBlockContextImpl<Argument, Result>: ThreadBlockContext {
@@ -49,15 +49,15 @@ internal class ThreadBlockContextImpl<Argument, Result>: ThreadBlockContext {
4949
super.init()
5050
}
5151

52-
override func run() -> UnsafeMutableRawPointer {
52+
@unsafe override func run() -> UnsafeMutableRawPointer {
5353
let result = UnsafeMutablePointer<Result>.allocate(capacity: 1)
5454
result.initialize(to: block(arg))
5555
return UnsafeMutableRawPointer(result)
5656
}
5757
}
5858

5959
/// Entry point for `pthread_create` that invokes a block context.
60-
internal func invokeBlockContext(
60+
@unsafe internal func invokeBlockContext(
6161
_ contextAsVoidPointer: UnsafeMutableRawPointer?
6262
) -> UnsafeMutableRawPointer! {
6363
// The context is passed in +1; we're responsible for releasing it.
@@ -71,21 +71,21 @@ internal func invokeBlockContext(
7171
#if os(Windows)
7272
public typealias ThreadHandle = HANDLE
7373
#else
74-
public typealias ThreadHandle = pthread_t
74+
@unsafe public typealias ThreadHandle = pthread_t
7575

7676
#if (os(Linux) && !canImport(Musl)) || os(Android)
7777
internal func _make_pthread_t() -> pthread_t {
7878
return pthread_t()
7979
}
8080
#else
81-
internal func _make_pthread_t() -> pthread_t? {
81+
@unsafe internal func _make_pthread_t() -> pthread_t? {
8282
return nil
8383
}
8484
#endif
8585
#endif
8686

8787
/// Block-based wrapper for `pthread_create`.
88-
public func _stdlib_thread_create_block<Argument, Result>(
88+
@unsafe public func _stdlib_thread_create_block<Argument, Result>(
8989
_ start_routine: @escaping (Argument) -> Result,
9090
_ arg: Argument
9191
) -> (CInt, ThreadHandle?) {
@@ -120,7 +120,7 @@ public func _stdlib_thread_create_block<Argument, Result>(
120120
}
121121

122122
/// Block-based wrapper for `pthread_join`.
123-
public func _stdlib_thread_join<Result>(
123+
@unsafe public func _stdlib_thread_join<Result>(
124124
_ thread: ThreadHandle,
125125
_ resultType: Result.Type
126126
) -> (CInt, Result?) {
@@ -160,12 +160,12 @@ public func _stdlib_thread_join<Result>(
160160
public class _stdlib_Barrier {
161161
var _threadBarrier: _stdlib_thread_barrier_t
162162

163-
var _threadBarrierPtr: UnsafeMutablePointer<_stdlib_thread_barrier_t> {
163+
@unsafe var _threadBarrierPtr: UnsafeMutablePointer<_stdlib_thread_barrier_t> {
164164
return _getUnsafePointerToStoredProperties(self)
165165
.assumingMemoryBound(to: _stdlib_thread_barrier_t.self)
166166
}
167167

168-
public init(threadCount: Int) {
168+
@safe(unchecked) public init(threadCount: Int) {
169169
self._threadBarrier = _stdlib_thread_barrier_t()
170170
let ret = _stdlib_thread_barrier_init(
171171
_threadBarrierPtr, CUnsignedInt(threadCount))
@@ -174,11 +174,11 @@ public class _stdlib_Barrier {
174174
}
175175
}
176176

177-
deinit {
177+
@safe(unchecked) deinit {
178178
_stdlib_thread_barrier_destroy(_threadBarrierPtr)
179179
}
180180

181-
public func wait() {
181+
@safe(unchecked) public func wait() {
182182
let ret = _stdlib_thread_barrier_wait(_threadBarrierPtr)
183183
if !(ret == 0 || ret == _stdlib_THREAD_BARRIER_SERIAL_THREAD) {
184184
fatalError("_stdlib_thread_barrier_wait() failed")

stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public struct _stdlib_thread_barrier_t {
4545
#elseif os(WASI)
4646
// pthread is currently not available on WASI
4747
#else
48-
var mutex: UnsafeMutablePointer<pthread_mutex_t>?
49-
var cond: UnsafeMutablePointer<pthread_cond_t>?
48+
@unsafe var mutex: UnsafeMutablePointer<pthread_mutex_t>?
49+
@unsafe var cond: UnsafeMutablePointer<pthread_cond_t>?
5050
#endif
5151

5252
/// The number of threads to synchronize.
@@ -60,7 +60,7 @@ public struct _stdlib_thread_barrier_t {
6060
public init() {}
6161
}
6262

63-
public func _stdlib_thread_barrier_init(
63+
@unsafe public func _stdlib_thread_barrier_init(
6464
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>,
6565
_ count: CUnsignedInt
6666
) -> CInt {
@@ -93,7 +93,7 @@ public func _stdlib_thread_barrier_init(
9393
}
9494

9595
#if !os(Windows) && !os(WASI)
96-
private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt {
96+
@unsafe private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt {
9797
guard pthread_mutex_init(barrier.pointee.mutex!, nil) == 0 else {
9898
return -1
9999
}
@@ -105,7 +105,7 @@ private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutable
105105
}
106106
#endif
107107

108-
public func _stdlib_thread_barrier_destroy(
108+
@unsafe public func _stdlib_thread_barrier_destroy(
109109
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>
110110
) {
111111
#if os(Windows)
@@ -131,7 +131,7 @@ public func _stdlib_thread_barrier_destroy(
131131
return
132132
}
133133

134-
public func _stdlib_thread_barrier_wait(
134+
@unsafe public func _stdlib_thread_barrier_wait(
135135
_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>
136136
) -> CInt {
137137
#if os(Windows)

0 commit comments

Comments
 (0)