Skip to content

Commit fd3e8a1

Browse files
committed
[stdlib] add explanations to _debugPrecondition calls
1 parent feecc51 commit fd3e8a1

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,15 +746,18 @@ extension Unsafe${Mutable}BufferPointer {
746746
if MemoryLayout<T>.stride == MemoryLayout<Element>.stride {
747747
newCount = count
748748
_debugPrecondition(
749-
MemoryLayout<T>.alignment == MemoryLayout<Element>.alignment
749+
Int(bitPattern: .init(base)) & (MemoryLayout<T>.alignment-1) == 0,
750+
"baseAddress must be a properly aligned pointer for types Element and T"
750751
)
751752
} else {
752753
newCount = count * MemoryLayout<Element>.stride / MemoryLayout<T>.stride
753754
_debugPrecondition(
754755
Int(bitPattern: .init(base)) & (MemoryLayout<T>.alignment-1) == 0 &&
755-
MemoryLayout<T>.stride > MemoryLayout<Element>.stride
756-
? MemoryLayout<T>.stride % MemoryLayout<Element>.stride == 0
757-
: MemoryLayout<Element>.stride % MemoryLayout<T>.stride == 0
756+
( MemoryLayout<T>.stride > MemoryLayout<Element>.stride
757+
? MemoryLayout<T>.stride % MemoryLayout<Element>.stride == 0
758+
: MemoryLayout<Element>.stride % MemoryLayout<T>.stride == 0
759+
),
760+
"baseAddress must be a properly aligned pointer for types Element and T"
758761
)
759762
}
760763
let binding = Builtin.bindMemory(base, newCount._builtinWordValue, T.self)

stdlib/public/core/UnsafePointer.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,10 @@ public struct UnsafePointer<Pointee>: _Pointer {
319319
( MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
320320
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
321321
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0
322-
)))
322+
)
323+
),
324+
"self must be a properly aligned pointer for types Pointee and T"
325+
)
323326
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
324327
defer { Builtin.rebindMemory(_rawValue, binding) }
325328
return try body(.init(_rawValue))
@@ -1026,7 +1029,10 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
10261029
( MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
10271030
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
10281031
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0
1029-
)))
1032+
)
1033+
),
1034+
"self must be a properly aligned pointer for types Pointee and T"
1035+
)
10301036
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
10311037
defer { Builtin.rebindMemory(_rawValue, binding) }
10321038
return try body(.init(_rawValue))

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,8 @@ extension Unsafe${Mutable}RawBufferPointer {
868868
return try body(.init(start: nil, count: 0))
869869
}
870870
_debugPrecondition(
871-
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0
871+
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0,
872+
"baseAddress must be a properly aligned pointer for type T"
872873
)
873874
// initializer ensures _end is nil only when _position is nil.
874875
_internalInvariant(_end != nil)

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,8 @@ public struct UnsafeRawPointer: _Pointer {
378378
_ body: (_ pointer: UnsafePointer<T>) throws -> Result
379379
) rethrows -> Result {
380380
_debugPrecondition(
381-
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0
381+
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0,
382+
"self must be a properly aligned pointer for type T"
382383
)
383384
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
384385
defer { Builtin.rebindMemory(_rawValue, binding) }
@@ -934,7 +935,8 @@ public struct UnsafeMutableRawPointer: _Pointer {
934935
_ body: (_ pointer: UnsafeMutablePointer<T>) throws -> Result
935936
) rethrows -> Result {
936937
_debugPrecondition(
937-
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0
938+
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0,
939+
"self must be a properly aligned pointer for type T"
938940
)
939941
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
940942
defer { Builtin.rebindMemory(_rawValue, binding) }

0 commit comments

Comments
 (0)