Skip to content

[stdlib]Catch the reasonable mistake 'UnsafeRawMutableBufferPointer' #69881

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

Closed
wants to merge 4 commits into from
Closed
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
24 changes: 14 additions & 10 deletions stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
%import gyb

% for mutable in (True, False):
% Self = 'UnsafeMutableRawBufferPointer' if mutable else 'UnsafeRawBufferPointer'
% Mutable = 'Mutable' if mutable else ''
% Self = 'UnsafeMutableRawBufferPointer' if mutable else 'UnsafeRawBufferPointer'
% if mutable:
@available(*, unavailable, renamed: "UnsafeMutableRawBufferPointer")
typealias UnsafeRawMutableBufferPointer = UnsafeMutableRawBufferPointer
%end

/// A ${Mutable.lower()} nonowning collection interface to the bytes in a
/// region of memory.
Expand Down Expand Up @@ -95,7 +99,7 @@
/// destBytes[0..<n] = someBytes[n..<(n + n)]
% end
@frozen
public struct Unsafe${Mutable}RawBufferPointer {
public struct UnsafeRaw${Mutable}BufferPointer {
@usableFromInline
internal let _position, _end: Unsafe${Mutable}RawPointer?

Expand Down Expand Up @@ -172,7 +176,7 @@ extension UnsafeMutableRawBufferPointer {
}
%end

extension Unsafe${Mutable}RawBufferPointer: Sequence {
extension UnsafeRaw${Mutable}BufferPointer: Sequence {
public typealias SubSequence = Slice<${Self}>

/// Returns an iterator over the bytes of this sequence.
Expand Down Expand Up @@ -202,7 +206,7 @@ extension Unsafe${Mutable}RawBufferPointer: Sequence {
}
}

extension Unsafe${Mutable}RawBufferPointer: ${Mutable}Collection {
extension UnsafeRaw${Mutable}BufferPointer: ${Mutable}Collection {
// TODO: Specialize `index` and `formIndex` and
// `_failEarlyRangeCheck` as in `UnsafeBufferPointer`.
public typealias Element = UInt8
Expand All @@ -218,7 +222,7 @@ extension Unsafe${Mutable}RawBufferPointer: ${Mutable}Collection {
/// The "past the end" position---that is, the position one greater than the
/// last valid subscript argument.
///
/// The `endIndex` property of an `Unsafe${Mutable}RawBufferPointer`
/// The `endIndex` property of an `UnsafeRaw${Mutable}BufferPointer`
/// instance is always identical to `count`.
@inlinable
public var endIndex: Index {
Expand Down Expand Up @@ -318,9 +322,9 @@ extension Unsafe${Mutable}RawBufferPointer: ${Mutable}Collection {
}
}

extension Unsafe${Mutable}RawBufferPointer: RandomAccessCollection { }
extension UnsafeRaw${Mutable}BufferPointer: RandomAccessCollection { }

extension Unsafe${Mutable}RawBufferPointer {
extension UnsafeRaw${Mutable}BufferPointer {
% if mutable:
/// Allocates uninitialized memory with the specified size and alignment.
///
Expand Down Expand Up @@ -1144,7 +1148,7 @@ extension Unsafe${Mutable}RawBufferPointer {
}

@_unavailableInEmbedded
extension Unsafe${Mutable}RawBufferPointer: CustomDebugStringConvertible {
extension UnsafeRaw${Mutable}BufferPointer: CustomDebugStringConvertible {
/// A textual representation of the buffer, suitable for debugging.
public var debugDescription: String {
return "${Self}"
Expand All @@ -1153,8 +1157,8 @@ extension Unsafe${Mutable}RawBufferPointer: CustomDebugStringConvertible {
}

extension ${Self} {
@available(*, unavailable,
message: "use 'Unsafe${Mutable}RawBufferPointer(rebasing:)' to convert a slice into a zero-based raw buffer.")
@available(*, unavailable,
message: "use 'UnsafeRaw${Mutable}BufferPointer(rebasing:)' to convert a slice into a zero-based raw buffer.")
public subscript(bounds: Range<Int>) -> ${Self} {
get { return ${Self}(start: nil, count: 0) }
% if mutable:
Expand Down
1 change: 1 addition & 0 deletions test/stdlib/UnsafePointerDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// Test availability attributes on UnsafePointer initializers.
// Assume the original source contains no UnsafeRawPointer types.

func unsafePointerConversionAvailability(
mrp: UnsafeMutableRawPointer,
rp: UnsafeRawPointer,
Expand Down