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

Merged
merged 1 commit into from
Jun 23, 2025
Merged
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 @@ -260,7 +260,7 @@ fileprivate class _Lock {
#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
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
9 changes: 5 additions & 4 deletions stdlib/public/Synchronization/Mutex/WindowsImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import WinSDK.core.synch

@available(SwiftStdlib 6.0, *)
@frozen
@safe
@_staticExclusiveOnly
public struct _MutexHandle: ~Copyable {
@usableFromInline
Expand All @@ -23,28 +24,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)
}
}
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
2 changes: 1 addition & 1 deletion stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3120,7 +3120,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
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
4 changes: 2 additions & 2 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
2 changes: 1 addition & 1 deletion 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
5 changes: 3 additions & 2 deletions stdlib/public/core/VarArgs.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
Expand Down Expand Up @@ -621,10 +622,10 @@ final internal class __VaListBuilder {
#if (arch(arm) && !os(iOS)) || arch(arm64_32) || arch(wasm32)
if let arg = arg as? _CVarArgAligned {
let alignmentInWords = arg._cVarArgAlignment / MemoryLayout<Int>.size
let misalignmentInWords = count % alignmentInWords
let misalignmentInWords = unsafe count % alignmentInWords
if misalignmentInWords != 0 {
let paddingInWords = alignmentInWords - misalignmentInWords
appendWords([Int](repeating: -1, count: paddingInWords))
unsafe appendWords([Int](repeating: -1, count: paddingInWords))
}
}
#endif
Expand Down