Skip to content

Commit 71a97dd

Browse files
authored
Merge pull request #60632 from glessard/rdar98826137-wMR-error-messages
[5.7][stdlib] add explanations to _debugPrecondition calls
2 parents 57c7317 + 72cec9e commit 71a97dd

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,19 +742,21 @@ extension Unsafe${Mutable}BufferPointer {
742742
return try body(.init(start: nil, count: 0))
743743
}
744744

745+
_debugPrecondition(
746+
Int(bitPattern: .init(base)) & (MemoryLayout<T>.alignment-1) == 0,
747+
"baseAddress must be a properly aligned pointer for types Element and T"
748+
)
749+
745750
let newCount: Int
746751
if MemoryLayout<T>.stride == MemoryLayout<Element>.stride {
747752
newCount = count
748-
_debugPrecondition(
749-
MemoryLayout<T>.alignment == MemoryLayout<Element>.alignment
750-
)
751753
} else {
752754
newCount = count * MemoryLayout<Element>.stride / MemoryLayout<T>.stride
753755
_debugPrecondition(
754-
Int(bitPattern: .init(base)) & (MemoryLayout<T>.alignment-1) == 0 &&
755756
MemoryLayout<T>.stride > MemoryLayout<Element>.stride
756757
? MemoryLayout<T>.stride % MemoryLayout<Element>.stride == 0
757-
: MemoryLayout<Element>.stride % MemoryLayout<T>.stride == 0
758+
: MemoryLayout<Element>.stride % MemoryLayout<T>.stride == 0,
759+
"Buffer must contain a whole number of Element instances"
758760
)
759761
}
760762
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
@@ -866,7 +866,8 @@ extension Unsafe${Mutable}RawBufferPointer {
866866
return try body(.init(start: nil, count: 0))
867867
}
868868
_debugPrecondition(
869-
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0
869+
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0,
870+
"baseAddress must be a properly aligned pointer for type T"
870871
)
871872
// initializer ensures _end is nil only when _position is nil.
872873
_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)