Skip to content

Commit d7806eb

Browse files
committed
Span: Remove ownership modifiers and explicit copies from pointers.
Without this fix, the standard library source will break with shortly upcoming compiler toolchain. Never explicitly copy a pointer before passing it to an argument that is the source of a lifetime dependency on the function's return value. That will always raise a diagnostic error: depending on a temporary value is not the same as depending on a variable. A temporary value's scope is only the current expression. Also avoid using ownership modifiers for UnsafePointer. We don't want to treat them like noncopyable types. They are simply values. Treating them like noncopyable types creates a lot of overhead in the representation, which is likely to interfere with diagnostics and optimization.
1 parent ec85589 commit d7806eb

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

stdlib/public/core/Span/RawSpan.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public struct RawSpan: ~Escapable, Copyable, BitwiseCopyable {
7070
@inline(__always)
7171
@lifetime(borrow pointer)
7272
internal init(
73-
_unchecked pointer: borrowing UnsafeRawPointer?,
73+
_unchecked pointer: UnsafeRawPointer?,
7474
byteCount: Int
7575
) {
76-
_pointer = copy pointer
76+
_pointer = pointer
7777
_count = byteCount
7878
}
7979
}
@@ -95,7 +95,7 @@ extension RawSpan {
9595
@_alwaysEmitIntoClient
9696
@lifetime(borrow buffer)
9797
public init(
98-
_unsafeBytes buffer: borrowing UnsafeRawBufferPointer
98+
_unsafeBytes buffer: UnsafeRawBufferPointer
9999
) {
100100
self.init(
101101
_unchecked: buffer.baseAddress, byteCount: buffer.count
@@ -129,7 +129,7 @@ extension RawSpan {
129129
@_alwaysEmitIntoClient
130130
@lifetime(borrow buffer)
131131
public init(
132-
_unsafeBytes buffer: borrowing UnsafeMutableRawBufferPointer
132+
_unsafeBytes buffer: UnsafeMutableRawBufferPointer
133133
) {
134134
self.init(_unsafeBytes: UnsafeRawBufferPointer(buffer))
135135
}
@@ -155,11 +155,11 @@ extension RawSpan {
155155
@_alwaysEmitIntoClient
156156
@lifetime(borrow pointer)
157157
public init(
158-
_unsafeStart pointer: borrowing UnsafeRawPointer,
158+
_unsafeStart pointer: UnsafeRawPointer,
159159
byteCount: Int
160160
) {
161161
_precondition(byteCount >= 0, "Count must not be negative")
162-
self.init(_unchecked: copy pointer, byteCount: byteCount)
162+
self.init(_unchecked: pointer, byteCount: byteCount)
163163
}
164164

165165
/// Unsafely create a `RawSpan` over initialized memory.
@@ -173,7 +173,7 @@ extension RawSpan {
173173
@_alwaysEmitIntoClient
174174
@lifetime(borrow buffer)
175175
public init<T: BitwiseCopyable>(
176-
_unsafeElements buffer: borrowing UnsafeBufferPointer<T>
176+
_unsafeElements buffer: UnsafeBufferPointer<T>
177177
) {
178178
self.init(_unsafeBytes: UnsafeRawBufferPointer(buffer))
179179
}
@@ -207,7 +207,7 @@ extension RawSpan {
207207
@_alwaysEmitIntoClient
208208
@lifetime(borrow buffer)
209209
public init<T: BitwiseCopyable>(
210-
_unsafeElements buffer: borrowing UnsafeMutableBufferPointer<T>
210+
_unsafeElements buffer: UnsafeMutableBufferPointer<T>
211211
) {
212212
self.init(_unsafeElements: UnsafeBufferPointer(buffer))
213213
}
@@ -243,12 +243,12 @@ extension RawSpan {
243243
@_alwaysEmitIntoClient
244244
@lifetime(borrow pointer)
245245
public init<T: BitwiseCopyable>(
246-
_unsafeStart pointer: borrowing UnsafePointer<T>,
246+
_unsafeStart pointer: UnsafePointer<T>,
247247
count: Int
248248
) {
249249
_precondition(count >= 0, "Count must not be negative")
250250
self.init(
251-
_unchecked: copy pointer, byteCount: count * MemoryLayout<T>.stride
251+
_unchecked: pointer, byteCount: count * MemoryLayout<T>.stride
252252
)
253253
}
254254

stdlib/public/core/Span/Span.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ public struct Span<Element: ~Copyable & ~Escapable>
7272
@inline(__always)
7373
@lifetime(borrow pointer)
7474
internal init(
75-
_unchecked pointer: borrowing UnsafeRawPointer?,
75+
_unchecked pointer: UnsafeRawPointer?,
7676
count: Int
7777
) {
78-
_pointer = copy pointer
78+
_pointer = pointer
7979
_count = count
8080
}
8181
}
@@ -97,7 +97,7 @@ extension Span where Element: ~Copyable {
9797
@_alwaysEmitIntoClient
9898
@lifetime(borrow buffer)
9999
public init(
100-
_unsafeElements buffer: borrowing UnsafeBufferPointer<Element>
100+
_unsafeElements buffer: UnsafeBufferPointer<Element>
101101
) {
102102
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
103103
let baseAddress = buffer.baseAddress
@@ -120,7 +120,7 @@ extension Span where Element: ~Copyable {
120120
@_alwaysEmitIntoClient
121121
@lifetime(borrow buffer)
122122
public init(
123-
_unsafeElements buffer: borrowing UnsafeMutableBufferPointer<Element>
123+
_unsafeElements buffer: UnsafeMutableBufferPointer<Element>
124124
) {
125125
self.init(_unsafeElements: UnsafeBufferPointer(buffer))
126126
}
@@ -138,11 +138,11 @@ extension Span where Element: ~Copyable {
138138
@_alwaysEmitIntoClient
139139
@lifetime(borrow pointer)
140140
public init(
141-
_unsafeStart pointer: borrowing UnsafePointer<Element>,
141+
_unsafeStart pointer: UnsafePointer<Element>,
142142
count: Int
143143
) {
144144
_precondition(count >= 0, "Count must not be negative")
145-
self.init(_unsafeElements: .init(start: copy pointer, count: count))
145+
self.init(_unsafeElements: .init(start: pointer, count: count))
146146
}
147147
}
148148

@@ -200,7 +200,7 @@ extension Span where Element: BitwiseCopyable {
200200
@_alwaysEmitIntoClient
201201
@lifetime(borrow buffer)
202202
public init(
203-
_unsafeBytes buffer: borrowing UnsafeRawBufferPointer
203+
_unsafeBytes buffer: UnsafeRawBufferPointer
204204
) {
205205
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
206206
let baseAddress = buffer.baseAddress
@@ -232,7 +232,7 @@ extension Span where Element: BitwiseCopyable {
232232
@_alwaysEmitIntoClient
233233
@lifetime(borrow buffer)
234234
public init(
235-
_unsafeBytes buffer: borrowing UnsafeMutableRawBufferPointer
235+
_unsafeBytes buffer: UnsafeMutableRawBufferPointer
236236
) {
237237
self.init(_unsafeBytes: UnsafeRawBufferPointer(buffer))
238238
}
@@ -254,11 +254,11 @@ extension Span where Element: BitwiseCopyable {
254254
@_alwaysEmitIntoClient
255255
@lifetime(borrow pointer)
256256
public init(
257-
_unsafeStart pointer: borrowing UnsafeRawPointer,
257+
_unsafeStart pointer: UnsafeRawPointer,
258258
byteCount: Int
259259
) {
260260
_precondition(byteCount >= 0, "Count must not be negative")
261-
self.init(_unsafeBytes: .init(start: copy pointer, count: byteCount))
261+
self.init(_unsafeBytes: .init(start: pointer, count: byteCount))
262262
}
263263

264264
/// Unsafely create a `Span` over initialized memory.

0 commit comments

Comments
 (0)