Skip to content

Commit 3d0d3e1

Browse files
author
Alexis Beingessner
committed
[docs] Add GEP preconditions to UnsafePointer.
Offsets lower to GetElementPointer inbounds, which specifies that it's Undefined Behaviour for the result to be out of bounds, regardless of if the result is actually dereferenced.
1 parent 360ba8a commit 3d0d3e1

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,21 +432,29 @@ public struct ${Self}<Pointee>
432432
}
433433

434434
/// Returns the next consecutive position.
435+
///
436+
/// - Precondition: The result is within the bounds of the same allocation.
435437
public func successor() -> ${Self} {
436438
return self + 1
437439
}
438440

439441
/// Returns the previous consecutive position.
442+
///
443+
/// - Precondition: The result is within the bounds of the same allocation.
440444
public func predecessor() -> ${Self} {
441445
return self - 1
442446
}
443447

444448
/// Returns `end - self`.
445449
public func distance(to x: ${Self}) -> Int {
446450
return x - self
451+
///
452+
/// - Precondition: The result is within the bounds of the same allocation.
447453
}
448454

449455
/// Returns `self + n`.
456+
///
457+
/// - Precondition: The result is within the bounds of the same allocation.
450458
public func advanced(by n: Int) -> ${Self} {
451459
return self + n
452460
}
@@ -499,18 +507,22 @@ public func < <Pointee>(lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Bool {
499507
/// - Note: The following family of operator overloads are redundant
500508
/// with Strideable. However, optimizer improvements are needed
501509
/// before they can be removed without affecting performance.
510+
511+
/// - Precondition: The result is within bounds of the same allocation.
502512
@_transparent
503513
public func + <Pointee>(lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
504514
return ${Self}(Builtin.gep_Word(
505515
lhs._rawValue, rhs._builtinWordValue, Pointee.self))
506516
}
507517

518+
/// - Precondition: The result is within the bounds of the same allocation.
508519
@_transparent
509520
public func + <Pointee>(lhs: Int,
510521
rhs: ${Self}<Pointee>) -> ${Self}<Pointee> {
511522
return rhs + lhs
512523
}
513524

525+
/// - Precondition: The result is within the bounds of the same allocation.
514526
@_transparent
515527
public func - <Pointee>(lhs: ${Self}<Pointee>, rhs: Int) -> ${Self}<Pointee> {
516528
return lhs + -rhs
@@ -524,11 +536,13 @@ public func - <Pointee>(lhs: ${Self}<Pointee>, rhs: ${Self}<Pointee>) -> Int {
524536
/ MemoryLayout<Pointee>.stride
525537
}
526538

539+
/// - Precondition: The result is within the bounds of the same allocation.
527540
@_transparent
528541
public func += <Pointee>(lhs: inout ${Self}<Pointee>, rhs: Int) {
529542
lhs = lhs + rhs
530543
}
531544

545+
/// - Precondition: The result is within the bounds of the same allocation.
532546
@_transparent
533547
public func -= <Pointee>(lhs: inout ${Self}<Pointee>, rhs: Int) {
534548
lhs = lhs - rhs

0 commit comments

Comments
 (0)