Skip to content

stdlib: handle unsafe annotations in additional places #82342

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 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ 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
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
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
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
13 changes: 7 additions & 6 deletions stdlib/public/core/KeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ 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 +105,7 @@ public class AnyKeyPath: _AppendKeyPath {
}
return offset
#elseif _pointerBitWidth(_32)
let offset = Int(bitPattern: _kvcKeyPathStringPtr) &- 1
let offset = unsafe Int(bitPattern: _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 @@ -4152,7 +4153,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 +4166,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
2 changes: 1 addition & 1 deletion stdlib/public/core/StringObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ extension _StringObject {
countAndFlags: countAndFlags)
#elseif _pointerBitWidth(_32) || _pointerBitWidth(_16)
self.init(
variant: .immortal(start: bufPtr.baseAddress._unsafelyUnwrappedUnchecked),
variant: unsafe .immortal(start: bufPtr.baseAddress._unsafelyUnwrappedUnchecked),
discriminator: Nibbles.largeImmortal(),
countAndFlags: countAndFlags)
#else
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/UInt128.swift
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion stdlib/public/core/VarArgs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ final internal class __VaListBuilder {
// We may need to retain an object that provides a pointer value.
if let obj = arg as? _CVarArgObject {
arg = obj._cVarArgObject
retainer.append(arg)
unsafe retainer.append(arg)
}
#endif

Expand Down