Skip to content

Commit 2454264

Browse files
committed
[stdlib] documentation clarifications
1 parent c248326 commit 2454264

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,11 @@ extension Unsafe${Mutable}BufferPointer {
660660
/// at runtime.
661661
///
662662
/// Any instance of `T` within the re-bound region may be initialized or
663-
/// uninitialized. If a given instance of `T` overlaps with memory previously
664-
/// bound to an uninitialized `Pointee`, it shall be considered uninitialized
665-
/// when executing `body`.
663+
/// uninitialized. Every instance of `Pointee` overlapping with a given
664+
/// instance of `T` should have the same initialization state (i.e.
665+
/// initialized or uninitialized.) Accessing a `T` whose underlying
666+
/// `Pointee` storage is in a mixed initialization state shall be
667+
/// undefined behaviour.
666668
///
667669
/// Because this buffer's memory is no longer bound to its `Element` type
668670
/// while the `body` closure executes, do not access memory using the

stdlib/public/core/UnsafePointer.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,11 @@ public struct UnsafePointer<Pointee>: _Pointer {
256256
/// The region of memory that starts at this pointer and covers `count`
257257
/// strides of `T` instances must be bound to `Pointee`.
258258
/// Any instance of `T` within the re-bound region may be initialized or
259-
/// uninitialized. If a given instance of `T` overlaps with memory previously
260-
/// bound to an uninitialized `Pointee`, it shall be considered uninitialized
261-
/// when executing `body`.
259+
/// uninitialized. Every instance of `Pointee` overlapping with a given
260+
/// instance of `T` should have the same initialization state (i.e.
261+
/// initialized or uninitialized.) Accessing a `T` whose underlying
262+
/// `Pointee` storage is in a mixed initialization state shall be
263+
/// undefined behaviour.
262264
///
263265
/// The following example temporarily rebinds the memory of a `UInt64`
264266
/// pointer to `Int64`, then accesses a property on the signed integer.
@@ -941,9 +943,11 @@ public struct UnsafeMutablePointer<Pointee>: _Pointer {
941943
/// The region of memory that starts at this pointer and covers `count`
942944
/// strides of `T` instances must be bound to `Pointee`.
943945
/// Any instance of `T` within the re-bound region may be initialized or
944-
/// uninitialized. If a given instance of `T` overlaps with memory previously
945-
/// bound to an uninitialized `Pointee`, it shall be considered uninitialized
946-
/// when executing `body`.
946+
/// uninitialized. Every instance of `Pointee` overlapping with a given
947+
/// instance of `T` should have the same initialization state (i.e.
948+
/// initialized or uninitialized.) Accessing a `T` whose underlying
949+
/// `Pointee` storage is in a mixed initialization state shall be
950+
/// undefined behaviour.
947951
///
948952
/// The following example temporarily rebinds the memory of a `UInt64`
949953
/// pointer to `Int64`, then modifies the signed integer.

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,14 @@ extension Unsafe${Mutable}RawBufferPointer {
741741
/// is undefined.
742742
///
743743
/// Any instance of `T` within the re-bound region may be initialized or
744-
/// uninitialized. If a given instance of `T` within the re-bound region
745-
/// overlaps with previously uninitialized memory, it shall be considered
746-
/// uninitialized when executing `body`.
744+
/// uninitialized. The memory underlying any individual instance of `T`
745+
/// must have the same initialization state (i.e. initialized or
746+
/// uninitialized.) Accessing a `T` whose underlying memory
747+
/// is in a mixed initialization state shall be undefined behaviour.
748+
///
749+
/// If the byte count of the original buffer is not a multiple of
750+
/// the stride of `T`, then the re-bound buffer is shorter
751+
/// than the original buffer.
747752
///
748753
/// After executing `body`, this method rebinds memory back to its original
749754
/// binding state. This can be unbound memory, or bound to a different type.
@@ -753,10 +758,15 @@ extension Unsafe${Mutable}RawBufferPointer {
753758
/// That is, `Int(bitPattern: self.baseAddress) % MemoryLayout<T>.alignment`
754759
/// must equal zero.
755760
///
761+
/// - Note: A raw buffer may represent memory that has been bound to a type.
762+
/// If that is the case, then `T` must be layout compatible with the
763+
/// type to which the memory has been bound. This requirement does not
764+
/// apply if the raw buffer represents memory that has not been bound
765+
/// to any type.
766+
///
756767
/// - Parameters:
757768
/// - type: The type to temporarily bind the memory referenced by this
758769
/// pointer. This pointer must be a multiple of this type's alignment.
759-
/// - count: The number of instances of `T` to bind to `type`.
760770
/// - body: A closure that takes a typed pointer to the
761771
/// same memory as this pointer, only bound to type `T`. The closure's
762772
/// pointer argument is valid only for the duration of the closure's
@@ -788,7 +798,7 @@ extension Unsafe${Mutable}RawBufferPointer {
788798
/// Returns a typed buffer to the memory referenced by this buffer,
789799
/// assuming that the memory is already bound to the specified type.
790800
///
791-
/// Use this method when you have a raw buffer to memory that has *already*
801+
/// Use this method when you have a raw buffer to memory that has already
792802
/// been bound to the specified type. The memory starting at this pointer
793803
/// must be bound to the type `T`. Accessing memory through the returned
794804
/// pointer is undefined if the memory has not been bound to `T`. To bind

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,10 @@ public struct UnsafeRawPointer: _Pointer {
330330
/// is undefined.
331331
///
332332
/// Any instance of `T` within the re-bound region may be initialized or
333-
/// uninitialized. If a given instance of `T` within the re-bound region
334-
/// overlaps with previously uninitialized memory, it shall be considered
335-
/// uninitialized when executing `body`.
333+
/// uninitialized. The memory underlying any individual instance of `T`
334+
/// must have the same initialization state (i.e. initialized or
335+
/// uninitialized.) Accessing a `T` whose underlying memory
336+
/// is in a mixed initialization state shall be undefined behaviour.
336337
///
337338
/// The following example temporarily rebinds a raw memory pointer
338339
/// to `Int64`, then accesses a property on the signed integer.
@@ -351,6 +352,12 @@ public struct UnsafeRawPointer: _Pointer {
351352
/// That is, `Int(bitPattern: self) % MemoryLayout<T>.alignment`
352353
/// must equal zero.
353354
///
355+
/// - Note: The region of memory starting at this pointer may have been
356+
/// bound to a type. If that is the case, then `T` must be
357+
/// layout compatible with the type to which the memory has been bound.
358+
/// This requirement does not apply if the region of memory
359+
/// has not been bound to any type.
360+
///
354361
/// - Parameters:
355362
/// - type: The type to temporarily bind the memory referenced by this
356363
/// pointer. This pointer must be a multiple of this type's alignment.
@@ -763,9 +770,10 @@ public struct UnsafeMutableRawPointer: _Pointer {
763770
/// is undefined.
764771
///
765772
/// Any instance of `T` within the re-bound region may be initialized or
766-
/// uninitialized. If a given instance of `T` within the re-bound region
767-
/// overlaps with previously uninitialized memory, it shall be considered
768-
/// uninitialized when executing `body`.
773+
/// uninitialized. The memory underlying any individual instance of `T`
774+
/// must have the same initialization state (i.e. initialized or
775+
/// uninitialized.) Accessing a `T` whose underlying memory
776+
/// is in a mixed initialization state shall be undefined behaviour.
769777
///
770778
/// The following example temporarily rebinds a raw memory pointer
771779
/// to `Int64`, then modifies the signed integer.
@@ -783,6 +791,12 @@ public struct UnsafeMutableRawPointer: _Pointer {
783791
/// That is, `Int(bitPattern: self) % MemoryLayout<T>.alignment`
784792
/// must equal zero.
785793
///
794+
/// - Note: The region of memory starting at this pointer may have been
795+
/// bound to a type. If that is the case, then `T` must be
796+
/// layout compatible with the type to which the memory has been bound.
797+
/// This requirement does not apply if the region of memory
798+
/// has not been bound to any type.
799+
///
786800
/// - Parameters:
787801
/// - type: The type to temporarily bind the memory referenced by this
788802
/// pointer. This pointer must be a multiple of this type's alignment.

0 commit comments

Comments
 (0)