Skip to content

stdlib: Fix missing unsafe operators in more places #82192

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions stdlib/public/Cxx/std/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension std.string {
// Use the 2 parameter constructor.
// The MSVC standard library has a enable_if template guard
// on the 3 parameter constructor, and thus it's not imported into Swift.
std.string(buffer, string.utf8.count)
unsafe std.string(buffer, string.utf8.count)
#else
unsafe std.string(buffer, string.utf8.count, .init())
#endif
Expand All @@ -40,7 +40,7 @@ extension std.string {
// Use the 2 parameter constructor.
// The MSVC standard library has a enable_if template guard
// on the 3 parameter constructor, and thus it's not imported into Swift.
self.init(str, UTF8._nullCodeUnitOffset(in: str))
unsafe self.init(str, UTF8._nullCodeUnitOffset(in: str))
#else
unsafe self.init(str, UTF8._nullCodeUnitOffset(in: str), .init())
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ fileprivate class _Lock {
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
unsafe self.underlying.initialize(to: os_unfair_lock())
#elseif os(Windows)
self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
InitializeSRWLock(self.underlying)
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
unsafe InitializeSRWLock(self.underlying)
#elseif os(WASI)
// WASI environment has only a single thread
#else
self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
guard unsafe pthread_mutex_init(self.underlying, nil) == 0 else {
fatalError("pthread_mutex_init failed")
}
Expand Down Expand Up @@ -292,7 +292,7 @@ fileprivate class _Lock {
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
unsafe os_unfair_lock_lock(self.underlying)
#elseif os(Windows)
AcquireSRWLockExclusive(self.underlying)
unsafe AcquireSRWLockExclusive(self.underlying)
#elseif os(WASI)
// WASI environment has only a single thread
#else
Expand All @@ -305,7 +305,7 @@ fileprivate class _Lock {
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
unsafe os_unfair_lock_unlock(self.underlying)
#elseif os(Windows)
ReleaseSRWLockExclusive(self.underlying)
unsafe ReleaseSRWLockExclusive(self.underlying)
#elseif os(WASI)
// WASI environment has only a single thread
#else
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/Synchronization/Mutex/LinuxImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ extension Atomic where Value == UInt32 {
// This returns 'false' on success and 'true' on error. Check 'errno' for the
// specific error value.
internal borrowing func _futexLock() -> UInt32 {
_swift_stdlib_futex_lock(.init(_rawAddress))
unsafe _swift_stdlib_futex_lock(.init(_rawAddress))
}

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

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

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Synchronization/Mutex/WasmImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32) -> UI
extension Atomic where Value == _MutexHandle.State {
internal borrowing func _wait(expected: _MutexHandle.State) {
#if _runtime(_multithreaded)
_ = _swift_stdlib_wait(
_ = unsafe _swift_stdlib_wait(
on: .init(_rawAddress),
expected: expected.rawValue,

Expand All @@ -39,7 +39,7 @@ extension Atomic where Value == _MutexHandle.State {
internal borrowing func _wake() {
#if _runtime(_multithreaded)
// Only wake up 1 thread
_ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
_ = unsafe _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
#endif
}
}
Expand Down
10 changes: 5 additions & 5 deletions stdlib/public/Synchronization/Mutex/WindowsImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import WinSDK.core.synch

@available(SwiftStdlib 6.0, *)
@frozen
@_staticExclusiveOnly
@safe @_staticExclusiveOnly
public struct _MutexHandle: ~Copyable {
@usableFromInline
let value: _Cell<SRWLOCK>
Expand All @@ -23,28 +23,28 @@ public struct _MutexHandle: ~Copyable {
@_alwaysEmitIntoClient
@_transparent
public init() {
value = _Cell(SRWLOCK())
unsafe value = _Cell(SRWLOCK())
}

@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
internal borrowing func _lock() {
AcquireSRWLockExclusive(value._address)
unsafe AcquireSRWLockExclusive(value._address)
}

@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
internal borrowing func _tryLock() -> Bool {
// Windows BOOLEAN gets imported as 'UInt8'...
TryAcquireSRWLockExclusive(value._address) != 0
unsafe TryAcquireSRWLockExclusive(value._address) != 0
}

@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@_transparent
internal borrowing func _unlock() {
ReleaseSRWLockExclusive(value._address)
unsafe ReleaseSRWLockExclusive(value._address)
}
}
8 changes: 4 additions & 4 deletions stdlib/public/core/BridgeObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -716,19 +716,19 @@ public func swift_unboxFromSwiftValueWithType<T>(

if source === _nullPlaceholder {
if let unpacked = Optional<Any>.none as? T {
result.initialize(to: unpacked)
unsafe result.initialize(to: unpacked)
return true
}
}

if let box = source as? __SwiftValue {
if let value = box.value as? T {
result.initialize(to: value)
unsafe result.initialize(to: value)
return true
}
} else if let box = source as? _NSSwiftValue {
if let value = box.value as? T {
result.initialize(to: value)
unsafe result.initialize(to: value)
return true
}
}
Expand Down Expand Up @@ -819,7 +819,7 @@ public func _bridgeAnythingToObjectiveC<T>(_ x: T) -> AnyObject {

if !done {
if type(of: source) as? AnyClass != nil {
result = unsafeBitCast(x, to: AnyObject.self)
result = unsafe unsafeBitCast(x, to: AnyObject.self)
} else if let object = _bridgeToObjectiveCUsingProtocolIfPossible(source) {
result = object
} else {
Expand Down
12 changes: 6 additions & 6 deletions stdlib/public/core/CTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,19 @@ public struct CVaListPointer {
__vr_top: UnsafeMutablePointer<Int>?,
__gr_off: Int32,
__vr_off: Int32) {
_value = (__stack, __gr_top, __vr_top, __gr_off, __vr_off)
unsafe _value = (__stack, __gr_top, __vr_top, __gr_off, __vr_off)
}
}

@_unavailableInEmbedded
extension CVaListPointer: CustomDebugStringConvertible {
@safe
public var debugDescription: String {
return "(\(_value.__stack.debugDescription), " +
"\(_value.__gr_top.debugDescription), " +
"\(_value.__vr_top.debugDescription), " +
"\(_value.__gr_off), " +
"\(_value.__vr_off))"
return "(\(unsafe _value.__stack.debugDescription), " +
"\(unsafe _value.__gr_top.debugDescription), " +
"\(unsafe _value.__vr_top.debugDescription), " +
"\(unsafe _value.__gr_off), " +
"\(unsafe _value.__vr_off))"
}
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/Int128.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public struct Int128: Sendable {
public var _value: Builtin.Int128 {
@_transparent
get {
unsafeBitCast(self, to: Builtin.Int128.self)
unsafe unsafeBitCast(self, to: Builtin.Int128.self)
}

@_transparent
Expand All @@ -88,7 +88,7 @@ public struct Int128: Sendable {
@available(SwiftStdlib 6.0, *)
@_transparent
public init(_ _value: Builtin.Int128) {
self = unsafeBitCast(_value, to: Self.self)
self = unsafe unsafeBitCast(_value, to: Self.self)
}
#endif

Expand Down
14 changes: 7 additions & 7 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public class AnyKeyPath: _AppendKeyPath {
unsafe _kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: -offset - 1)
#elseif _pointerBitWidth(_32)
if offset <= maximumOffsetOn32BitArchitecture {
_kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: (offset + 1))
unsafe _kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: (offset + 1))
} else {
_kvcKeyPathStringPtr = nil
unsafe _kvcKeyPathStringPtr = nil
}
#else
// Don't assign anything.
Expand All @@ -104,7 +104,7 @@ public class AnyKeyPath: _AppendKeyPath {
}
return offset
#elseif _pointerBitWidth(_32)
let offset = Int(bitPattern: _kvcKeyPathStringPtr) &- 1
let offset = Int(bitPattern: unsafe _kvcKeyPathStringPtr) &- 1
// Pointers above 0x7fffffff will come in as negative numbers which are
// less than maximumOffsetOn32BitArchitecture, be sure to reject them.
if offset >= 0, offset <= maximumOffsetOn32BitArchitecture {
Expand Down Expand Up @@ -3119,7 +3119,7 @@ internal func _resolveRelativeIndirectableAddress(_ base: UnsafeRawPointer,
internal func _resolveCompactFunctionPointer(_ base: UnsafeRawPointer, _ offset: Int32)
-> UnsafeRawPointer {
#if SWIFT_COMPACT_ABSOLUTE_FUNCTION_POINTER
return UnsafeRawPointer(bitPattern: Int(offset))._unsafelyUnwrappedUnchecked
return unsafe UnsafeRawPointer(bitPattern: Int(offset))._unsafelyUnwrappedUnchecked
#else
return unsafe _resolveRelativeAddress(base, offset)
#endif
Expand Down Expand Up @@ -4152,7 +4152,7 @@ internal func _instantiateKeyPathBuffer(
var walker = unsafe ValidatingInstantiateKeyPathBuffer(sizeVisitor: sizeWalker,
instantiateVisitor: instantiateWalker)
#else
var walker = InstantiateKeyPathBuffer(
var walker = unsafe InstantiateKeyPathBuffer(
destData: destData,
patternArgs: arguments,
root: rootType)
Expand All @@ -4165,8 +4165,8 @@ internal func _instantiateKeyPathBuffer(
let endOfReferencePrefixComponent =
unsafe walker.instantiateVisitor.endOfReferencePrefixComponent
#else
let isTrivial = walker.isTrivial
let endOfReferencePrefixComponent = walker.endOfReferencePrefixComponent
let isTrivial = unsafe walker.isTrivial
let endOfReferencePrefixComponent = unsafe walker.endOfReferencePrefixComponent
#endif

// Write out the header.
Expand Down
22 changes: 11 additions & 11 deletions stdlib/public/core/StaticPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension ConstantVPrintFInterpolation {
public mutating func appendInterpolation(
_ pointer: @autoclosure @escaping () -> UnsafeRawBufferPointer
) {
appendInterpolation(pointer().baseAddress!)
unsafe appendInterpolation(pointer().baseAddress!)
}

/// Defines interpolation for UnsafeRawPointer.
Expand All @@ -64,7 +64,7 @@ extension ConstantVPrintFInterpolation {
_ pointer: @autoclosure @escaping () -> UnsafeRawPointer
) {
formatString += "%p"
arguments.append(pointer)
unsafe arguments.append(pointer)
}
}

Expand Down Expand Up @@ -734,7 +734,7 @@ extension UnsafeRawPointer: CVarArg {
/// appropriately interpreted by C varargs.
@inlinable // c-abi
public var _cVarArgEncoding: [Int] {
return _encodeBitsAsWords(self)
return unsafe _encodeBitsAsWords(self)
}
}

Expand All @@ -758,8 +758,8 @@ extension ConstantVPrintFArguments {
@_optimize(none)
internal mutating func append(_ value: @escaping () -> String) {
argumentClosures.append({ continuation in
value().withCString { str in
continuation(str._cVarArgEncoding)
unsafe value().withCString { str in
unsafe continuation(str._cVarArgEncoding)
}
})
}
Expand Down Expand Up @@ -817,15 +817,15 @@ internal func constant_vprintf_backend_recurse(
if let closure = argumentClosures.first {
closure { newArg in
args.append(contentsOf: newArg)
constant_vprintf_backend_recurse(
unsafe constant_vprintf_backend_recurse(
fmt: fmt,
argumentClosures: argumentClosures.dropFirst(),
args: &args
)
}
} else {
_ = withVaList(args) { valist in
_swift_stdlib_vprintf(fmt, valist)
_ = unsafe withVaList(args) { valist in
unsafe _swift_stdlib_vprintf(fmt, valist)
}
}
}
Expand All @@ -839,14 +839,14 @@ internal func constant_vprintf_backend(
if let closure = argumentClosures.first {
closure { newArg in
args.append(contentsOf: newArg)
constant_vprintf_backend_recurse(
unsafe constant_vprintf_backend_recurse(
fmt: fmt,
argumentClosures: argumentClosures.dropFirst(),
args: &args
)
}
} else {
constant_vprintf_backend_recurse(
unsafe constant_vprintf_backend_recurse(
fmt: fmt,
argumentClosures: ArraySlice(argumentClosures),
args: &args
Expand All @@ -864,7 +864,7 @@ public func print(_ message: ConstantVPrintFMessage) {
let argumentClosures = message.interpolation.arguments.argumentClosures
if Bool(_builtinBooleanLiteral: Builtin.ifdef_SWIFT_STDLIB_PRINT_DISABLED()) { return }
let formatStringPointer = _getGlobalStringTablePointer(formatString)
constant_vprintf_backend(
unsafe constant_vprintf_backend(
fmt: formatStringPointer,
argumentClosures: argumentClosures
)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/StringObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ extension _StringObject {
_internalInvariantFailure()
}
#if !$Embedded
return _unsafeUncheckedDowncast(storage, to: __StringStorage.self)
return unsafe _unsafeUncheckedDowncast(storage, to: __StringStorage.self)
#else
return Builtin.castFromNativeObject(storage)
#endif
Expand Down Expand Up @@ -1044,7 +1044,7 @@ extension _StringObject {
_internalInvariantFailure()
}
#if !$Embedded
return _unsafeUncheckedDowncast(storage, to: __SharedStringStorage.self)
return unsafe _unsafeUncheckedDowncast(storage, to: __SharedStringStorage.self)
#else
return Builtin.castFromNativeObject(storage)
#endif
Expand Down Expand Up @@ -1244,7 +1244,7 @@ extension _StringObject {
discriminator: Nibbles.largeImmortal(),
countAndFlags: countAndFlags)
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
self.init(
unsafe self.init(
variant: .immortal(start: bufPtr.baseAddress._unsafelyUnwrappedUnchecked),
discriminator: Nibbles.largeImmortal(),
countAndFlags: countAndFlags)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/UInt128.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public struct UInt128: Sendable {
#if _endian(little)
self = unsafe unsafeBitCast((_low, _high), to: Self.self)
#else
self = unsafeBitCast((_high, _low), to: Self.self)
self = unsafe unsafeBitCast((_high, _low), to: Self.self)
#endif
}

Expand Down Expand Up @@ -76,7 +76,7 @@ public struct UInt128: Sendable {
public var _value: Builtin.Int128 {
@_transparent
get {
unsafeBitCast(self, to: Builtin.Int128.self)
unsafe unsafeBitCast(self, to: Builtin.Int128.self)
}

@_transparent
Expand All @@ -88,7 +88,7 @@ public struct UInt128: Sendable {
@available(SwiftStdlib 6.0, *)
@_transparent
public init(_ _value: Builtin.Int128) {
self = unsafeBitCast(_value, to: Self.self)
self = unsafe unsafeBitCast(_value, to: Self.self)
}
#endif

Expand Down
Loading