Skip to content

Commit 92dd7fd

Browse files
committed
stdlib: Fix missing unsafe operators in more places.
Add `unsafe` where it is still missing. I missed these in previous passes due to conditional compilation.
1 parent 00aa22f commit 92dd7fd

File tree

13 files changed

+57
-56
lines changed

13 files changed

+57
-56
lines changed

stdlib/public/Cxx/std/String.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension std.string {
2626
// Use the 2 parameter constructor.
2727
// The MSVC standard library has a enable_if template guard
2828
// on the 3 parameter constructor, and thus it's not imported into Swift.
29-
std.string(buffer, string.utf8.count)
29+
unsafe std.string(buffer, string.utf8.count)
3030
#else
3131
unsafe std.string(buffer, string.utf8.count, .init())
3232
#endif
@@ -40,7 +40,7 @@ extension std.string {
4040
// Use the 2 parameter constructor.
4141
// The MSVC standard library has a enable_if template guard
4242
// on the 3 parameter constructor, and thus it's not imported into Swift.
43-
self.init(str, UTF8._nullCodeUnitOffset(in: str))
43+
unsafe self.init(str, UTF8._nullCodeUnitOffset(in: str))
4444
#else
4545
unsafe self.init(str, UTF8._nullCodeUnitOffset(in: str), .init())
4646
#endif

stdlib/public/Distributed/LocalTestingDistributedActorSystem.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ fileprivate class _Lock {
255255
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
256256
unsafe self.underlying.initialize(to: os_unfair_lock())
257257
#elseif os(Windows)
258-
self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
259-
InitializeSRWLock(self.underlying)
258+
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
259+
unsafe InitializeSRWLock(self.underlying)
260260
#elseif os(WASI)
261261
// WASI environment has only a single thread
262262
#else
263-
self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
263+
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
264264
guard unsafe pthread_mutex_init(self.underlying, nil) == 0 else {
265265
fatalError("pthread_mutex_init failed")
266266
}
@@ -292,7 +292,7 @@ fileprivate class _Lock {
292292
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
293293
unsafe os_unfair_lock_lock(self.underlying)
294294
#elseif os(Windows)
295-
AcquireSRWLockExclusive(self.underlying)
295+
unsafe AcquireSRWLockExclusive(self.underlying)
296296
#elseif os(WASI)
297297
// WASI environment has only a single thread
298298
#else
@@ -305,7 +305,7 @@ fileprivate class _Lock {
305305
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
306306
unsafe os_unfair_lock_unlock(self.underlying)
307307
#elseif os(Windows)
308-
ReleaseSRWLockExclusive(self.underlying)
308+
unsafe ReleaseSRWLockExclusive(self.underlying)
309309
#elseif os(WASI)
310310
// WASI environment has only a single thread
311311
#else

stdlib/public/Synchronization/Mutex/LinuxImpl.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ extension Atomic where Value == UInt32 {
2323
// This returns 'false' on success and 'true' on error. Check 'errno' for the
2424
// specific error value.
2525
internal borrowing func _futexLock() -> UInt32 {
26-
_swift_stdlib_futex_lock(.init(_rawAddress))
26+
unsafe _swift_stdlib_futex_lock(.init(_rawAddress))
2727
}
2828

2929
// This returns 'false' on success and 'true' on error. Check 'errno' for the
3030
// specific error value.
3131
internal borrowing func _futexTryLock() -> UInt32 {
32-
_swift_stdlib_futex_trylock(.init(_rawAddress))
32+
unsafe _swift_stdlib_futex_trylock(.init(_rawAddress))
3333
}
3434

3535
// This returns 'false' on success and 'true' on error. Check 'errno' for the
3636
// specific error value.
3737
internal borrowing func _futexUnlock() -> UInt32 {
38-
_swift_stdlib_futex_unlock(.init(_rawAddress))
38+
unsafe _swift_stdlib_futex_unlock(.init(_rawAddress))
3939
}
4040
}
4141

stdlib/public/Synchronization/Mutex/WasmImpl.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32) -> UI
2626
extension Atomic where Value == _MutexHandle.State {
2727
internal borrowing func _wait(expected: _MutexHandle.State) {
2828
#if _runtime(_multithreaded)
29-
_ = _swift_stdlib_wait(
29+
_ = unsafe _swift_stdlib_wait(
3030
on: .init(_rawAddress),
3131
expected: expected.rawValue,
3232

@@ -39,7 +39,7 @@ extension Atomic where Value == _MutexHandle.State {
3939
internal borrowing func _wake() {
4040
#if _runtime(_multithreaded)
4141
// Only wake up 1 thread
42-
_ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
42+
_ = unsafe _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
4343
#endif
4444
}
4545
}

stdlib/public/Synchronization/Mutex/WindowsImpl.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import WinSDK.core.synch
1414

1515
@available(SwiftStdlib 6.0, *)
1616
@frozen
17-
@_staticExclusiveOnly
17+
@safe @_staticExclusiveOnly
1818
public struct _MutexHandle: ~Copyable {
1919
@usableFromInline
2020
let value: _Cell<SRWLOCK>
@@ -23,28 +23,28 @@ public struct _MutexHandle: ~Copyable {
2323
@_alwaysEmitIntoClient
2424
@_transparent
2525
public init() {
26-
value = _Cell(SRWLOCK())
26+
unsafe value = _Cell(SRWLOCK())
2727
}
2828

2929
@available(SwiftStdlib 6.0, *)
3030
@_alwaysEmitIntoClient
3131
@_transparent
3232
internal borrowing func _lock() {
33-
AcquireSRWLockExclusive(value._address)
33+
unsafe AcquireSRWLockExclusive(value._address)
3434
}
3535

3636
@available(SwiftStdlib 6.0, *)
3737
@_alwaysEmitIntoClient
3838
@_transparent
3939
internal borrowing func _tryLock() -> Bool {
4040
// Windows BOOLEAN gets imported as 'UInt8'...
41-
TryAcquireSRWLockExclusive(value._address) != 0
41+
unsafe TryAcquireSRWLockExclusive(value._address) != 0
4242
}
4343

4444
@available(SwiftStdlib 6.0, *)
4545
@_alwaysEmitIntoClient
4646
@_transparent
4747
internal borrowing func _unlock() {
48-
ReleaseSRWLockExclusive(value._address)
48+
unsafe ReleaseSRWLockExclusive(value._address)
4949
}
5050
}

stdlib/public/core/BridgeObjectiveC.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,19 +716,19 @@ public func swift_unboxFromSwiftValueWithType<T>(
716716

717717
if source === _nullPlaceholder {
718718
if let unpacked = Optional<Any>.none as? T {
719-
result.initialize(to: unpacked)
719+
unsafe result.initialize(to: unpacked)
720720
return true
721721
}
722722
}
723723

724724
if let box = source as? __SwiftValue {
725725
if let value = box.value as? T {
726-
result.initialize(to: value)
726+
unsafe result.initialize(to: value)
727727
return true
728728
}
729729
} else if let box = source as? _NSSwiftValue {
730730
if let value = box.value as? T {
731-
result.initialize(to: value)
731+
unsafe result.initialize(to: value)
732732
return true
733733
}
734734
}
@@ -819,7 +819,7 @@ public func _bridgeAnythingToObjectiveC<T>(_ x: T) -> AnyObject {
819819

820820
if !done {
821821
if type(of: source) as? AnyClass != nil {
822-
result = unsafeBitCast(x, to: AnyObject.self)
822+
result = unsafe unsafeBitCast(x, to: AnyObject.self)
823823
} else if let object = _bridgeToObjectiveCUsingProtocolIfPossible(source) {
824824
result = object
825825
} else {

stdlib/public/core/CTypes.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,19 @@ public struct CVaListPointer {
314314
__vr_top: UnsafeMutablePointer<Int>?,
315315
__gr_off: Int32,
316316
__vr_off: Int32) {
317-
_value = (__stack, __gr_top, __vr_top, __gr_off, __vr_off)
317+
unsafe _value = (__stack, __gr_top, __vr_top, __gr_off, __vr_off)
318318
}
319319
}
320320

321321
@_unavailableInEmbedded
322322
extension CVaListPointer: CustomDebugStringConvertible {
323323
@safe
324324
public var debugDescription: String {
325-
return "(\(_value.__stack.debugDescription), " +
326-
"\(_value.__gr_top.debugDescription), " +
327-
"\(_value.__vr_top.debugDescription), " +
328-
"\(_value.__gr_off), " +
329-
"\(_value.__vr_off))"
325+
return "(\(unsafe _value.__stack.debugDescription), " +
326+
"\(unsafe _value.__gr_top.debugDescription), " +
327+
"\(unsafe _value.__vr_top.debugDescription), " +
328+
"\(unsafe _value.__gr_off), " +
329+
"\(unsafe _value.__vr_off))"
330330
}
331331
}
332332

stdlib/public/core/Int128.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public struct Int128: Sendable {
7676
public var _value: Builtin.Int128 {
7777
@_transparent
7878
get {
79-
unsafeBitCast(self, to: Builtin.Int128.self)
79+
unsafe unsafeBitCast(self, to: Builtin.Int128.self)
8080
}
8181

8282
@_transparent
@@ -88,7 +88,7 @@ public struct Int128: Sendable {
8888
@available(SwiftStdlib 6.0, *)
8989
@_transparent
9090
public init(_ _value: Builtin.Int128) {
91-
self = unsafeBitCast(_value, to: Self.self)
91+
self = unsafe unsafeBitCast(_value, to: Self.self)
9292
}
9393
#endif
9494

stdlib/public/core/KeyPath.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public class AnyKeyPath: _AppendKeyPath {
8080
unsafe _kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: -offset - 1)
8181
#elseif _pointerBitWidth(_32)
8282
if offset <= maximumOffsetOn32BitArchitecture {
83-
_kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: (offset + 1))
83+
unsafe _kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: (offset + 1))
8484
} else {
85-
_kvcKeyPathStringPtr = nil
85+
unsafe _kvcKeyPathStringPtr = nil
8686
}
8787
#else
8888
// Don't assign anything.
@@ -104,7 +104,7 @@ public class AnyKeyPath: _AppendKeyPath {
104104
}
105105
return offset
106106
#elseif _pointerBitWidth(_32)
107-
let offset = Int(bitPattern: _kvcKeyPathStringPtr) &- 1
107+
let offset = Int(bitPattern: unsafe _kvcKeyPathStringPtr) &- 1
108108
// Pointers above 0x7fffffff will come in as negative numbers which are
109109
// less than maximumOffsetOn32BitArchitecture, be sure to reject them.
110110
if offset >= 0, offset <= maximumOffsetOn32BitArchitecture {
@@ -3119,7 +3119,7 @@ internal func _resolveRelativeIndirectableAddress(_ base: UnsafeRawPointer,
31193119
internal func _resolveCompactFunctionPointer(_ base: UnsafeRawPointer, _ offset: Int32)
31203120
-> UnsafeRawPointer {
31213121
#if SWIFT_COMPACT_ABSOLUTE_FUNCTION_POINTER
3122-
return UnsafeRawPointer(bitPattern: Int(offset))._unsafelyUnwrappedUnchecked
3122+
return unsafe UnsafeRawPointer(bitPattern: Int(offset))._unsafelyUnwrappedUnchecked
31233123
#else
31243124
return unsafe _resolveRelativeAddress(base, offset)
31253125
#endif
@@ -4152,7 +4152,7 @@ internal func _instantiateKeyPathBuffer(
41524152
var walker = unsafe ValidatingInstantiateKeyPathBuffer(sizeVisitor: sizeWalker,
41534153
instantiateVisitor: instantiateWalker)
41544154
#else
4155-
var walker = InstantiateKeyPathBuffer(
4155+
var walker = unsafe InstantiateKeyPathBuffer(
41564156
destData: destData,
41574157
patternArgs: arguments,
41584158
root: rootType)
@@ -4165,8 +4165,8 @@ internal func _instantiateKeyPathBuffer(
41654165
let endOfReferencePrefixComponent =
41664166
unsafe walker.instantiateVisitor.endOfReferencePrefixComponent
41674167
#else
4168-
let isTrivial = walker.isTrivial
4169-
let endOfReferencePrefixComponent = walker.endOfReferencePrefixComponent
4168+
let isTrivial = unsafe walker.isTrivial
4169+
let endOfReferencePrefixComponent = unsafe walker.endOfReferencePrefixComponent
41704170
#endif
41714171

41724172
// Write out the header.

stdlib/public/core/StaticPrint.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension ConstantVPrintFInterpolation {
4747
public mutating func appendInterpolation(
4848
_ pointer: @autoclosure @escaping () -> UnsafeRawBufferPointer
4949
) {
50-
appendInterpolation(pointer().baseAddress!)
50+
unsafe appendInterpolation(pointer().baseAddress!)
5151
}
5252

5353
/// Defines interpolation for UnsafeRawPointer.
@@ -64,7 +64,7 @@ extension ConstantVPrintFInterpolation {
6464
_ pointer: @autoclosure @escaping () -> UnsafeRawPointer
6565
) {
6666
formatString += "%p"
67-
arguments.append(pointer)
67+
unsafe arguments.append(pointer)
6868
}
6969
}
7070

@@ -734,7 +734,7 @@ extension UnsafeRawPointer: CVarArg {
734734
/// appropriately interpreted by C varargs.
735735
@inlinable // c-abi
736736
public var _cVarArgEncoding: [Int] {
737-
return _encodeBitsAsWords(self)
737+
return unsafe _encodeBitsAsWords(self)
738738
}
739739
}
740740

@@ -758,8 +758,8 @@ extension ConstantVPrintFArguments {
758758
@_optimize(none)
759759
internal mutating func append(_ value: @escaping () -> String) {
760760
argumentClosures.append({ continuation in
761-
value().withCString { str in
762-
continuation(str._cVarArgEncoding)
761+
unsafe value().withCString { str in
762+
unsafe continuation(str._cVarArgEncoding)
763763
}
764764
})
765765
}
@@ -817,15 +817,15 @@ internal func constant_vprintf_backend_recurse(
817817
if let closure = argumentClosures.first {
818818
closure { newArg in
819819
args.append(contentsOf: newArg)
820-
constant_vprintf_backend_recurse(
820+
unsafe constant_vprintf_backend_recurse(
821821
fmt: fmt,
822822
argumentClosures: argumentClosures.dropFirst(),
823823
args: &args
824824
)
825825
}
826826
} else {
827-
_ = withVaList(args) { valist in
828-
_swift_stdlib_vprintf(fmt, valist)
827+
_ = unsafe withVaList(args) { valist in
828+
unsafe _swift_stdlib_vprintf(fmt, valist)
829829
}
830830
}
831831
}
@@ -839,14 +839,14 @@ internal func constant_vprintf_backend(
839839
if let closure = argumentClosures.first {
840840
closure { newArg in
841841
args.append(contentsOf: newArg)
842-
constant_vprintf_backend_recurse(
842+
unsafe constant_vprintf_backend_recurse(
843843
fmt: fmt,
844844
argumentClosures: argumentClosures.dropFirst(),
845845
args: &args
846846
)
847847
}
848848
} else {
849-
constant_vprintf_backend_recurse(
849+
unsafe constant_vprintf_backend_recurse(
850850
fmt: fmt,
851851
argumentClosures: ArraySlice(argumentClosures),
852852
args: &args
@@ -864,7 +864,7 @@ public func print(_ message: ConstantVPrintFMessage) {
864864
let argumentClosures = message.interpolation.arguments.argumentClosures
865865
if Bool(_builtinBooleanLiteral: Builtin.ifdef_SWIFT_STDLIB_PRINT_DISABLED()) { return }
866866
let formatStringPointer = _getGlobalStringTablePointer(formatString)
867-
constant_vprintf_backend(
867+
unsafe constant_vprintf_backend(
868868
fmt: formatStringPointer,
869869
argumentClosures: argumentClosures
870870
)

stdlib/public/core/StringObject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ extension _StringObject {
10051005
_internalInvariantFailure()
10061006
}
10071007
#if !$Embedded
1008-
return _unsafeUncheckedDowncast(storage, to: __StringStorage.self)
1008+
return unsafe _unsafeUncheckedDowncast(storage, to: __StringStorage.self)
10091009
#else
10101010
return Builtin.castFromNativeObject(storage)
10111011
#endif
@@ -1044,7 +1044,7 @@ extension _StringObject {
10441044
_internalInvariantFailure()
10451045
}
10461046
#if !$Embedded
1047-
return _unsafeUncheckedDowncast(storage, to: __SharedStringStorage.self)
1047+
return unsafe _unsafeUncheckedDowncast(storage, to: __SharedStringStorage.self)
10481048
#else
10491049
return Builtin.castFromNativeObject(storage)
10501050
#endif
@@ -1244,7 +1244,7 @@ extension _StringObject {
12441244
discriminator: Nibbles.largeImmortal(),
12451245
countAndFlags: countAndFlags)
12461246
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
1247-
self.init(
1247+
unsafe self.init(
12481248
variant: .immortal(start: bufPtr.baseAddress._unsafelyUnwrappedUnchecked),
12491249
discriminator: Nibbles.largeImmortal(),
12501250
countAndFlags: countAndFlags)

stdlib/public/core/UInt128.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public struct UInt128: Sendable {
4747
#if _endian(little)
4848
self = unsafe unsafeBitCast((_low, _high), to: Self.self)
4949
#else
50-
self = unsafeBitCast((_high, _low), to: Self.self)
50+
self = unsafe unsafeBitCast((_high, _low), to: Self.self)
5151
#endif
5252
}
5353

@@ -76,7 +76,7 @@ public struct UInt128: Sendable {
7676
public var _value: Builtin.Int128 {
7777
@_transparent
7878
get {
79-
unsafeBitCast(self, to: Builtin.Int128.self)
79+
unsafe unsafeBitCast(self, to: Builtin.Int128.self)
8080
}
8181

8282
@_transparent
@@ -88,7 +88,7 @@ public struct UInt128: Sendable {
8888
@available(SwiftStdlib 6.0, *)
8989
@_transparent
9090
public init(_ _value: Builtin.Int128) {
91-
self = unsafeBitCast(_value, to: Self.self)
91+
self = unsafe unsafeBitCast(_value, to: Self.self)
9292
}
9393
#endif
9494

0 commit comments

Comments
 (0)