Skip to content

[5.7][stdlib] add explanations to _debugPrecondition calls #60632

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
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
12 changes: 7 additions & 5 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -742,19 +742,21 @@ extension Unsafe${Mutable}BufferPointer {
return try body(.init(start: nil, count: 0))
}

_debugPrecondition(
Int(bitPattern: .init(base)) & (MemoryLayout<T>.alignment-1) == 0,
"baseAddress must be a properly aligned pointer for types Element and T"
)

let newCount: Int
if MemoryLayout<T>.stride == MemoryLayout<Element>.stride {
newCount = count
_debugPrecondition(
MemoryLayout<T>.alignment == MemoryLayout<Element>.alignment
)
} else {
newCount = count * MemoryLayout<Element>.stride / MemoryLayout<T>.stride
_debugPrecondition(
Int(bitPattern: .init(base)) & (MemoryLayout<T>.alignment-1) == 0 &&
MemoryLayout<T>.stride > MemoryLayout<Element>.stride
? MemoryLayout<T>.stride % MemoryLayout<Element>.stride == 0
: MemoryLayout<Element>.stride % MemoryLayout<T>.stride == 0
: MemoryLayout<Element>.stride % MemoryLayout<T>.stride == 0,
"Buffer must contain a whole number of Element instances"
)
}
let binding = Builtin.bindMemory(base, newCount._builtinWordValue, T.self)
Expand Down
10 changes: 8 additions & 2 deletions stdlib/public/core/UnsafePointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ public struct UnsafePointer<Pointee>: _Pointer {
( MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0
)))
)
),
"self must be a properly aligned pointer for types Pointee and T"
)
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
defer { Builtin.rebindMemory(_rawValue, binding) }
return try body(.init(_rawValue))
Expand Down Expand Up @@ -1026,7 +1029,10 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
( MemoryLayout<Pointee>.stride > MemoryLayout<T>.stride
? MemoryLayout<Pointee>.stride % MemoryLayout<T>.stride == 0
: MemoryLayout<T>.stride % MemoryLayout<Pointee>.stride == 0
)))
)
),
"self must be a properly aligned pointer for types Pointee and T"
)
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
defer { Builtin.rebindMemory(_rawValue, binding) }
return try body(.init(_rawValue))
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,8 @@ extension Unsafe${Mutable}RawBufferPointer {
return try body(.init(start: nil, count: 0))
}
_debugPrecondition(
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0
Int(bitPattern: s) & (MemoryLayout<T>.alignment-1) == 0,
"baseAddress must be a properly aligned pointer for type T"
)
// initializer ensures _end is nil only when _position is nil.
_internalInvariant(_end != nil)
Expand Down
6 changes: 4 additions & 2 deletions stdlib/public/core/UnsafeRawPointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ public struct UnsafeRawPointer: _Pointer {
_ body: (_ pointer: UnsafePointer<T>) throws -> Result
) rethrows -> Result {
_debugPrecondition(
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0,
"self must be a properly aligned pointer for type T"
)
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
defer { Builtin.rebindMemory(_rawValue, binding) }
Expand Down Expand Up @@ -934,7 +935,8 @@ public struct UnsafeMutableRawPointer: _Pointer {
_ body: (_ pointer: UnsafeMutablePointer<T>) throws -> Result
) rethrows -> Result {
_debugPrecondition(
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0
Int(bitPattern: self) & (MemoryLayout<T>.alignment-1) == 0,
"self must be a properly aligned pointer for type T"
)
let binding = Builtin.bindMemory(_rawValue, count._builtinWordValue, T.self)
defer { Builtin.rebindMemory(_rawValue, binding) }
Expand Down