Skip to content

Commit 86cddcf

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 a6d5ef6 commit 86cddcf

File tree

11 files changed

+42
-41
lines changed

11 files changed

+42
-41
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: 3 additions & 3 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
}

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/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/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

stdlib/public/core/VarArgs.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
//===----------------------------------------------------------------------===//
23
//
34
// This source file is part of the Swift.org open source project
@@ -609,7 +610,7 @@ final internal class __VaListBuilder {
609610
// We may need to retain an object that provides a pointer value.
610611
if let obj = arg as? _CVarArgObject {
611612
arg = obj._cVarArgObject
612-
retainer.append(arg)
613+
unsafe retainer.append(arg)
613614
}
614615
#endif
615616

@@ -621,10 +622,10 @@ final internal class __VaListBuilder {
621622
#if (arch(arm) && !os(iOS)) || arch(arm64_32) || arch(wasm32)
622623
if let arg = arg as? _CVarArgAligned {
623624
let alignmentInWords = arg._cVarArgAlignment / MemoryLayout<Int>.size
624-
let misalignmentInWords = count % alignmentInWords
625+
let misalignmentInWords = unsafe count % alignmentInWords
625626
if misalignmentInWords != 0 {
626627
let paddingInWords = alignmentInWords - misalignmentInWords
627-
appendWords([Int](repeating: -1, count: paddingInWords))
628+
unsafe appendWords([Int](repeating: -1, count: paddingInWords))
628629
}
629630
}
630631
#endif

0 commit comments

Comments
 (0)