Skip to content

Commit dbd3d40

Browse files
committed
stdlib: Address StrictMemorySafety warnings in KeyPath related code.
1 parent 60e66f3 commit dbd3d40

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

stdlib/public/Distributed/LocalTestingDistributedActorSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fileprivate class _Lock {
252252

253253
init() {
254254
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(visionOS)
255-
self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
255+
unsafe self.underlying = UnsafeMutablePointer.allocate(capacity: 1)
256256
unsafe self.underlying.initialize(to: os_unfair_lock())
257257
#elseif os(Windows)
258258
self.underlying = UnsafeMutablePointer.allocate(capacity: 1)

stdlib/public/core/KeyPath.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class AnyKeyPath: _AppendKeyPath {
7777
}
7878

7979
#if _pointerBitWidth(_64)
80-
_kvcKeyPathStringPtr = unsafe UnsafePointer<CChar>(bitPattern: -offset - 1)
80+
unsafe _kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: -offset - 1)
8181
#elseif _pointerBitWidth(_32)
8282
if offset <= maximumOffsetOn32BitArchitecture {
8383
_kvcKeyPathStringPtr = UnsafePointer<CChar>(bitPattern: (offset + 1))
@@ -96,7 +96,7 @@ public class AnyKeyPath: _AppendKeyPath {
9696
}
9797

9898
#if _pointerBitWidth(_64)
99-
let offset = (0 &- Int(bitPattern: _kvcKeyPathStringPtr)) &- 1
99+
let offset = unsafe (0 &- Int(bitPattern: _kvcKeyPathStringPtr)) &- 1
100100
guard _fastPath(offset >= 0) else {
101101
// This happens to be an actual _kvcKeyPathStringPtr, not an offset, if
102102
// we get here.
@@ -126,7 +126,7 @@ public class AnyKeyPath: _AppendKeyPath {
126126
guard self.getOffsetFromStorage() == nil else {
127127
return nil
128128
}
129-
guard let ptr = _kvcKeyPathStringPtr else { return nil }
129+
guard let ptr = unsafe _kvcKeyPathStringPtr else { return nil }
130130

131131
return unsafe String(validatingCString: ptr)
132132
}
@@ -155,7 +155,7 @@ public class AnyKeyPath: _AppendKeyPath {
155155
"capacity must be multiple of 4 bytes")
156156
let result = Builtin.allocWithTailElems_1(self, (bytes/4)._builtinWordValue,
157157
Int32.self)
158-
result._kvcKeyPathStringPtr = nil
158+
unsafe result._kvcKeyPathStringPtr = nil
159159
let base = UnsafeMutableRawPointer(Builtin.projectTailElems(result,
160160
Int32.self))
161161
unsafe body(UnsafeMutableRawBufferPointer(start: base, count: bytes))
@@ -173,7 +173,7 @@ public class AnyKeyPath: _AppendKeyPath {
173173
@usableFromInline // Exposed as public API by MemoryLayout<Root>.offset(of:)
174174
internal var _storedInlineOffset: Int? {
175175
#if !$Embedded
176-
return withBuffer {
176+
return unsafe withBuffer {
177177
var buffer = unsafe $0
178178

179179
// The identity key path is effectively a stored keypath of type Self
@@ -219,7 +219,7 @@ extension AnyKeyPath: Hashable {
219219
@_effects(releasenone)
220220
final public func hash(into hasher: inout Hasher) {
221221
ObjectIdentifier(type(of: self)).hash(into: &hasher)
222-
return withBuffer {
222+
return unsafe withBuffer {
223223
var buffer = unsafe $0
224224
if unsafe buffer.data.isEmpty { return }
225225
while true {
@@ -243,9 +243,9 @@ extension AnyKeyPath: Hashable {
243243
if type(of: a) != type(of: b) {
244244
return false
245245
}
246-
return a.withBuffer {
246+
return unsafe a.withBuffer {
247247
var aBuffer = unsafe $0
248-
return b.withBuffer {
248+
return unsafe b.withBuffer {
249249
var bBuffer = unsafe $0
250250

251251
// Two equivalent key paths should have the same reference prefix
@@ -344,7 +344,7 @@ public class KeyPath<Root, Value>: PartialKeyPath<Root> {
344344
}
345345
}
346346

347-
return withBuffer {
347+
return unsafe withBuffer {
348348
var buffer = unsafe $0
349349

350350
if unsafe _slowPath(buffer.data.isEmpty) {
@@ -441,7 +441,7 @@ public class KeyPath<Root, Value>: PartialKeyPath<Root> {
441441

442442
deinit {
443443
#if !$Embedded
444-
withBuffer { unsafe $0.destroy() }
444+
unsafe withBuffer { unsafe $0.destroy() }
445445
#else
446446
fatalError() // unreachable, keypaths in embedded Swift are compile-time
447447
#endif
@@ -475,7 +475,7 @@ public class WritableKeyPath<Root, Value>: KeyPath<Root, Value> {
475475
var type: Any.Type = Root.self
476476
var keepAlive: AnyObject?
477477

478-
return withBuffer {
478+
return unsafe withBuffer {
479479
var buffer = unsafe $0
480480

481481
unsafe _internalInvariant(!buffer.hasReferencePrefix,
@@ -531,7 +531,7 @@ public class ReferenceWritableKeyPath<
531531
internal final func _projectMutableAddress(from origBase: Root)
532532
-> (pointer: UnsafeMutablePointer<Value>, owner: AnyObject?) {
533533
var keepAlive: AnyObject?
534-
let address: UnsafeMutablePointer<Value> = withBuffer {
534+
let address: UnsafeMutablePointer<Value> = unsafe withBuffer {
535535
var buffer = unsafe $0
536536

537537
// Project out the reference prefix.
@@ -2619,9 +2619,9 @@ internal func _appendingKeyPaths<
26192619
leaf: KeyPath<Value, AppendedValue>
26202620
) -> Result {
26212621
let resultTy = type(of: root).appendedType(with: type(of: leaf))
2622-
var returnValue: AnyKeyPath = root.withBuffer {
2622+
var returnValue: AnyKeyPath = unsafe root.withBuffer {
26232623
var rootBuffer = unsafe $0
2624-
return leaf.withBuffer {
2624+
return unsafe leaf.withBuffer {
26252625
var leafBuffer = unsafe $0
26262626

26272627
// If either operand is the identity key path, then we should return
@@ -2638,8 +2638,8 @@ internal func _appendingKeyPaths<
26382638
let appendedKVCLength: Int, rootKVCLength: Int, leafKVCLength: Int
26392639

26402640
if root.getOffsetFromStorage() == nil, leaf.getOffsetFromStorage() == nil,
2641-
let rootPtr = root._kvcKeyPathStringPtr,
2642-
let leafPtr = leaf._kvcKeyPathStringPtr {
2641+
let rootPtr = unsafe root._kvcKeyPathStringPtr,
2642+
let leafPtr = unsafe leaf._kvcKeyPathStringPtr {
26432643
rootKVCLength = unsafe Int(_swift_stdlib_strlen(rootPtr))
26442644
leafKVCLength = unsafe Int(_swift_stdlib_strlen(leafPtr))
26452645
// root + "." + leaf
@@ -2673,7 +2673,7 @@ internal func _appendingKeyPaths<
26732673

26742674
var kvcStringBuffer: UnsafeMutableRawPointer? = nil
26752675

2676-
let result = resultTy._create(capacityInBytes: totalResultSize) {
2676+
let result = unsafe resultTy._create(capacityInBytes: totalResultSize) {
26772677
var destBuffer = unsafe $0
26782678

26792679
// Remember where the tail-allocated KVC string buffer begins.
@@ -2771,7 +2771,7 @@ internal func _appendingKeyPaths<
27712771
dest: kvcStringBuffer.advanced(by: rootKVCLength + 1),
27722772
src: leafPtr,
27732773
size: UInt(leafKVCLength))
2774-
result._kvcKeyPathStringPtr =
2774+
unsafe result._kvcKeyPathStringPtr =
27752775
unsafe UnsafePointer(kvcStringBuffer.assumingMemoryBound(to: CChar.self))
27762776
unsafe kvcStringBuffer.advanced(by: rootKVCLength + leafKVCLength + 1)
27772777
.storeBytes(of: 0 /* '\0' */, as: CChar.self)
@@ -2874,7 +2874,7 @@ public func _swift_getKeyPath(pattern: UnsafeMutableRawPointer,
28742874
var pureStructOffset: UInt32? = nil
28752875

28762876
// Allocate the instance.
2877-
let instance = keyPathClass._create(
2877+
let instance = unsafe keyPathClass._create(
28782878
capacityInBytes: sizeWithMaxSize
28792879
) { instanceData in
28802880
// Instantiate the pattern into the instance.
@@ -2893,11 +2893,11 @@ public func _swift_getKeyPath(pattern: UnsafeMutableRawPointer,
28932893

28942894
if kvcStringOffset == 0 {
28952895
// Null pointer.
2896-
instance._kvcKeyPathStringPtr = nil
2896+
unsafe instance._kvcKeyPathStringPtr = nil
28972897
} else {
28982898
let kvcStringPtr = unsafe _resolveRelativeAddress(kvcStringBase, kvcStringOffset)
2899-
instance._kvcKeyPathStringPtr =
2900-
unsafe kvcStringPtr.assumingMemoryBound(to: CChar.self)
2899+
unsafe instance._kvcKeyPathStringPtr =
2900+
kvcStringPtr.assumingMemoryBound(to: CChar.self)
29012901
}
29022902
if unsafe instance._kvcKeyPathStringPtr == nil, let offset = pureStructOffset {
29032903
instance.assignOffsetToStorage(offset: Int(offset))
@@ -4222,7 +4222,7 @@ public func _createOffsetBasedKeyPath(
42224222
// The buffer header is 32 bits, but components must start on a word
42234223
// boundary.
42244224
let kpBufferSize = MemoryLayout<Int>.size + MemoryLayout<Int32>.size
4225-
let kp = kpTy._create(capacityInBytes: kpBufferSize) {
4225+
let kp = unsafe kpTy._create(capacityInBytes: kpBufferSize) {
42264226
var builder = unsafe KeyPathBuffer.Builder($0)
42274227
let header = unsafe KeyPathBuffer.Header(
42284228
size: kpBufferSize - MemoryLayout<Int>.size,
@@ -4265,7 +4265,7 @@ public func _rerootKeyPath<NewRoot>(
42654265
hasReferencePrefix,
42664266
isSingleComponent,
42674267
componentSize
4268-
) = existingKp.withBuffer {
4268+
) = unsafe existingKp.withBuffer {
42694269
unsafe ($0.trivial, $0.hasReferencePrefix, $0.isSingleComponent, $0.data.count)
42704270
}
42714271

@@ -4297,7 +4297,7 @@ public func _rerootKeyPath<NewRoot>(
42974297
capacity = MemoryLayout<Int>._roundingUpToAlignment(capacity)
42984298
capacity += MemoryLayout<Int>.size
42994299

4300-
return newKpTy._create(
4300+
return unsafe newKpTy._create(
43014301
capacityInBytes: capacity
43024302
) {
43034303
var builder = unsafe KeyPathBuffer.Builder($0)
@@ -4310,7 +4310,7 @@ public func _rerootKeyPath<NewRoot>(
43104310

43114311
unsafe builder.pushHeader(header)
43124312

4313-
existingKp.withBuffer {
4313+
unsafe existingKp.withBuffer {
43144314
var existingBuffer = unsafe $0
43154315

43164316
while true {
@@ -4382,7 +4382,7 @@ extension AnyKeyPath: CustomDebugStringConvertible {
43824382
@available(SwiftStdlib 5.8, *)
43834383
public var debugDescription: String {
43844384
var description = "\\\(String(describing: Self.rootType))"
4385-
return withBuffer {
4385+
return unsafe withBuffer {
43864386
var buffer = unsafe $0
43874387
if unsafe buffer.data.isEmpty {
43884388
description.append(".self")

stdlib/public/core/ReflectionMirror.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public func _forEachFieldWithKeyPath<Root>(
356356
return KeyPath<Root, Leaf>.self
357357
}
358358
let resultSize = MemoryLayout<Int32>.size + MemoryLayout<Int>.size
359-
let partialKeyPath = _openExistential(childType, do: keyPathType)
359+
let partialKeyPath = unsafe _openExistential(childType, do: keyPathType)
360360
._create(capacityInBytes: resultSize) {
361361
var destBuilder = unsafe KeyPathBuffer.Builder($0)
362362
unsafe destBuilder.pushHeader(KeyPathBuffer.Header(

0 commit comments

Comments
 (0)