@@ -119,8 +119,6 @@ internal func _isStackAllocationSafe(byteCount: Int, alignment: Int) -> Bool {
119
119
///
120
120
/// - Returns: Whatever is returned by `body`.
121
121
///
122
- /// - Throws: Whatever is thrown by `body`.
123
- ///
124
122
/// This function encapsulates the various calls to builtins required by
125
123
/// `withUnsafeTemporaryAllocation()`.
126
124
@_alwaysEmitIntoClient @_transparent
@@ -130,13 +128,13 @@ internal func _withUnsafeTemporaryAllocation<
130
128
of type: T . Type ,
131
129
capacity: Int ,
132
130
alignment: Int ,
133
- _ body: ( Builtin . RawPointer ) throws -> R
134
- ) rethrows -> R {
131
+ _ body: ( Builtin . RawPointer ) -> R
132
+ ) -> R {
135
133
// How many bytes do we need to allocate?
136
134
let byteCount = _byteCountForTemporaryAllocation ( of: type, capacity: capacity)
137
135
138
136
guard _isStackAllocationSafe ( byteCount: byteCount, alignment: alignment) else {
139
- return try _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
137
+ return _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
140
138
}
141
139
142
140
// This declaration must come BEFORE Builtin.stackAlloc() or
@@ -154,15 +152,9 @@ internal func _withUnsafeTemporaryAllocation<
154
152
// The multiple calls to Builtin.stackDealloc() are because defer { } produces
155
153
// a child function at the SIL layer and that conflicts with the verifier's
156
154
// idea of a stack allocation's lifetime.
157
- do {
158
- result = try body ( stackAddress)
159
- Builtin . stackDealloc ( stackAddress)
160
- return result
161
-
162
- } catch {
163
- Builtin . stackDealloc ( stackAddress)
164
- throw error
165
- }
155
+ result = body ( stackAddress)
156
+ Builtin . stackDealloc ( stackAddress)
157
+ return result
166
158
#else
167
159
fatalError ( " unsupported compiler " )
168
160
#endif
@@ -175,13 +167,13 @@ internal func _withUnprotectedUnsafeTemporaryAllocation<
175
167
of type: T . Type ,
176
168
capacity: Int ,
177
169
alignment: Int ,
178
- _ body: ( Builtin . RawPointer ) throws -> R
179
- ) rethrows -> R {
170
+ _ body: ( Builtin . RawPointer ) -> R
171
+ ) -> R {
180
172
// How many bytes do we need to allocate?
181
173
let byteCount = _byteCountForTemporaryAllocation ( of: type, capacity: capacity)
182
174
183
175
guard _isStackAllocationSafe ( byteCount: byteCount, alignment: alignment) else {
184
- return try _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
176
+ return _fallBackToHeapAllocation ( byteCount: byteCount, alignment: alignment, body)
185
177
}
186
178
187
179
// This declaration must come BEFORE Builtin.unprotectedStackAlloc() or
@@ -198,23 +190,17 @@ internal func _withUnprotectedUnsafeTemporaryAllocation<
198
190
// The multiple calls to Builtin.stackDealloc() are because defer { } produces
199
191
// a child function at the SIL layer and that conflicts with the verifier's
200
192
// idea of a stack allocation's lifetime.
201
- do {
202
- result = try body ( stackAddress)
203
- Builtin . stackDealloc ( stackAddress)
204
- return result
205
-
206
- } catch {
207
- Builtin . stackDealloc ( stackAddress)
208
- throw error
209
- }
193
+ result = body ( stackAddress)
194
+ Builtin . stackDealloc ( stackAddress)
195
+ return result
210
196
}
211
197
212
198
@_alwaysEmitIntoClient @_transparent
213
- internal func _fallBackToHeapAllocation< R: ~ Copyable> (
199
+ internal func _fallBackToHeapAllocation< R: ~ Copyable, E : Error > (
214
200
byteCount: Int ,
215
201
alignment: Int ,
216
- _ body: ( Builtin . RawPointer ) throws -> R
217
- ) rethrows -> R {
202
+ _ body: ( Builtin . RawPointer ) throws ( E ) -> R
203
+ ) throws ( E ) -> R {
218
204
let buffer = UnsafeMutableRawPointer . allocate (
219
205
byteCount: byteCount,
220
206
alignment: alignment
@@ -259,21 +245,30 @@ internal func _fallBackToHeapAllocation<R: ~Copyable>(
259
245
/// the buffer) must not escape. It will be deallocated when `body` returns and
260
246
/// cannot be used afterward.
261
247
@_alwaysEmitIntoClient @_transparent
262
- public func withUnsafeTemporaryAllocation< R: ~ Copyable> (
248
+ public func withUnsafeTemporaryAllocation< R: ~ Copyable, E : Error > (
263
249
byteCount: Int ,
264
250
alignment: Int ,
265
- _ body: ( UnsafeMutableRawBufferPointer ) throws -> R
266
- ) rethrows -> R {
267
- return try _withUnsafeTemporaryAllocation (
251
+ _ body: ( UnsafeMutableRawBufferPointer ) throws ( E ) -> R
252
+ ) throws ( E ) -> R {
253
+ let result : Result < R , E > = _withUnsafeTemporaryAllocation (
268
254
of: Int8 . self,
269
255
capacity: byteCount,
270
256
alignment: alignment
271
257
) { pointer in
272
- let buffer = unsafe UnsafeMutableRawBufferPointer(
273
- start: . init( pointer) ,
274
- count: byteCount
275
- )
276
- return try unsafe body( buffer)
258
+ do throws ( E) {
259
+ let buffer = unsafe UnsafeMutableRawBufferPointer(
260
+ start: . init( pointer) ,
261
+ count: byteCount
262
+ )
263
+ return . success( try unsafe body( buffer) )
264
+ } catch {
265
+ return . failure( error)
266
+ }
267
+ }
268
+
269
+ switch consume result {
270
+ case . success( let resultValue) : return resultValue
271
+ case . failure( let error) : throw error
277
272
}
278
273
}
279
274
@@ -283,21 +278,30 @@ public func withUnsafeTemporaryAllocation<R: ~Copyable>(
283
278
/// This function is similar to `withUnsafeTemporaryAllocation`, except that it
284
279
/// doesn't trigger stack protection for the stack allocated memory.
285
280
@_alwaysEmitIntoClient @_transparent
286
- public func _withUnprotectedUnsafeTemporaryAllocation< R: ~ Copyable> (
281
+ public func _withUnprotectedUnsafeTemporaryAllocation< R: ~ Copyable, E : Error > (
287
282
byteCount: Int ,
288
283
alignment: Int ,
289
- _ body: ( UnsafeMutableRawBufferPointer ) throws -> R
290
- ) rethrows -> R {
291
- return try _withUnprotectedUnsafeTemporaryAllocation (
284
+ _ body: ( UnsafeMutableRawBufferPointer ) throws ( E ) -> R
285
+ ) throws ( E ) -> R {
286
+ let result : Result < R , E > = _withUnprotectedUnsafeTemporaryAllocation (
292
287
of: Int8 . self,
293
288
capacity: byteCount,
294
289
alignment: alignment
295
290
) { pointer in
296
- let buffer = unsafe UnsafeMutableRawBufferPointer(
297
- start: . init( pointer) ,
298
- count: byteCount
299
- )
300
- return try unsafe body( buffer)
291
+ do throws ( E) {
292
+ let buffer = unsafe UnsafeMutableRawBufferPointer(
293
+ start: . init( pointer) ,
294
+ count: byteCount
295
+ )
296
+ return try unsafe . success ( body ( buffer) )
297
+ } catch {
298
+ return . failure( error)
299
+ }
300
+ }
301
+
302
+ switch consume result {
303
+ case . success( let resultValue) : return resultValue
304
+ case . failure( let error) : throw error
301
305
}
302
306
}
303
307
@@ -334,23 +338,33 @@ public func _withUnprotectedUnsafeTemporaryAllocation<R: ~Copyable>(
334
338
/// cannot be used afterward.
335
339
@_alwaysEmitIntoClient @_transparent
336
340
public func withUnsafeTemporaryAllocation<
337
- T: ~ Copyable, R: ~ Copyable
341
+ T: ~ Copyable, R: ~ Copyable,
342
+ E: Error
338
343
> (
339
344
of type: T . Type ,
340
345
capacity: Int ,
341
- _ body: ( UnsafeMutableBufferPointer < T > ) throws -> R
342
- ) rethrows -> R {
343
- return try _withUnsafeTemporaryAllocation (
346
+ _ body: ( UnsafeMutableBufferPointer < T > ) throws ( E ) -> R
347
+ ) throws ( E ) -> R {
348
+ let result : Result < R , E > = _withUnsafeTemporaryAllocation (
344
349
of: type,
345
350
capacity: capacity,
346
351
alignment: MemoryLayout< T> . alignment
347
352
) { pointer in
348
- Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
349
- let buffer = unsafe UnsafeMutableBufferPointer< T > (
350
- start: . init( pointer) ,
351
- count: capacity
352
- )
353
- return try unsafe body( buffer)
353
+ do throws ( E) {
354
+ Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
355
+ let buffer = unsafe UnsafeMutableBufferPointer< T > (
356
+ start: . init( pointer) ,
357
+ count: capacity
358
+ )
359
+ return try unsafe . success ( body ( buffer) )
360
+ } catch {
361
+ return . failure( error)
362
+ }
363
+ }
364
+
365
+ switch consume result {
366
+ case . success( let resultValue) : return resultValue
367
+ case . failure( let error) : throw error
354
368
}
355
369
}
356
370
@@ -361,22 +375,32 @@ public func withUnsafeTemporaryAllocation<
361
375
/// doesn't trigger stack protection for the stack allocated memory.
362
376
@_alwaysEmitIntoClient @_transparent
363
377
public func _withUnprotectedUnsafeTemporaryAllocation<
364
- T: ~ Copyable, R: ~ Copyable
378
+ T: ~ Copyable, R: ~ Copyable,
379
+ E: Error
365
380
> (
366
381
of type: T . Type ,
367
382
capacity: Int ,
368
- _ body: ( UnsafeMutableBufferPointer < T > ) throws -> R
369
- ) rethrows -> R {
370
- return try _withUnprotectedUnsafeTemporaryAllocation (
383
+ _ body: ( UnsafeMutableBufferPointer < T > ) throws ( E ) -> R
384
+ ) throws ( E ) -> R {
385
+ let result : Result < R , E > = _withUnprotectedUnsafeTemporaryAllocation (
371
386
of: type,
372
387
capacity: capacity,
373
388
alignment: MemoryLayout< T> . alignment
374
389
) { pointer in
375
- Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
376
- let buffer = unsafe UnsafeMutableBufferPointer< T > (
377
- start: . init( pointer) ,
378
- count: capacity
379
- )
380
- return try unsafe body( buffer)
390
+ do throws ( E) {
391
+ Builtin . bindMemory ( pointer, capacity. _builtinWordValue, type)
392
+ let buffer = unsafe UnsafeMutableBufferPointer< T > (
393
+ start: . init( pointer) ,
394
+ count: capacity
395
+ )
396
+ return try unsafe . success ( body ( buffer) )
397
+ } catch {
398
+ return . failure( error)
399
+ }
400
+ }
401
+
402
+ switch consume result {
403
+ case . success( let resultValue) : return resultValue
404
+ case . failure( let error) : throw error
381
405
}
382
406
}
0 commit comments