Skip to content

Commit 3782db5

Browse files
committed
Small fixes for typed throws in withUnsafeTemporaryAllocation
1 parent e96e174 commit 3782db5

File tree

3 files changed

+14
-27
lines changed

3 files changed

+14
-27
lines changed

stdlib/public/core/TemporaryAllocation.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,7 @@ public func withUnsafeTemporaryAllocation<R: ~Copyable, E: Error>(
266266
}
267267
}
268268

269-
switch consume result {
270-
case .success(let resultValue): return resultValue
271-
case .failure(let error): throw error
272-
}
269+
return try result.get()
273270
}
274271

275272
/// Provides scoped access to a raw buffer pointer with the specified byte count
@@ -299,10 +296,7 @@ public func _withUnprotectedUnsafeTemporaryAllocation<R: ~Copyable, E: Error>(
299296
}
300297
}
301298

302-
switch consume result {
303-
case .success(let resultValue): return resultValue
304-
case .failure(let error): throw error
305-
}
299+
return try result.get()
306300
}
307301

308302
/// Provides scoped access to a buffer pointer to memory of the specified type
@@ -362,10 +356,7 @@ public func withUnsafeTemporaryAllocation<
362356
}
363357
}
364358

365-
switch consume result {
366-
case .success(let resultValue): return resultValue
367-
case .failure(let error): throw error
368-
}
359+
return try result.get()
369360
}
370361

371362
/// Provides scoped access to a buffer pointer to memory of the specified type
@@ -399,8 +390,5 @@ public func _withUnprotectedUnsafeTemporaryAllocation<
399390
}
400391
}
401392

402-
switch consume result {
403-
case .success(let resultValue): return resultValue
404-
case .failure(let error): throw error
405-
}
393+
return try result.get()
406394
}

test/IRGen/temporary_allocation/codegen.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %target-swift-frontend -primary-file %s -O -emit-ir | %FileCheck %s
2-
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
2+
// REQUIRES: optimized_stdlib
33

44
@_silgen_name("blackHole")
55
func blackHole(_ value: UnsafeMutableRawPointer?) -> Void
@@ -16,10 +16,10 @@ do {
1616
// CHECK: [[ONE_BYTE_PTR_RAW:%temp_alloc[0-9]*]] = alloca i8, align 1
1717
// CHECK: [[FIVE_BYTE_PTR_RAW:%temp_alloc[0-9]*]] = alloca [5 x i8], align 1
1818
// CHECK: [[ONE_KB_PTR_RAW:%temp_alloc[0-9]*]] = alloca [1024 x i8], align 8
19-
// CHECK: [[ONE_KB_RAND_PTR_RAW:%temp_alloc[0-9]*]] = alloca [1024 x i8], align 16
2019
// CHECK: [[INT_PTR_RAW:%temp_alloc[0-9]*]] = alloca [16 x i8], align 4
2120
// CHECK: [[INT_PTR_RAW2:%temp_alloc[0-9]*]] = alloca [16 x i8], align 4
2221
// CHECK: [[VOID_PTR_RAW:%temp_alloc[0-9]*]] = alloca [2 x i8], align 1
22+
// CHECK: [[ONE_KB_RAND_PTR_RAW:%temp_alloc[0-9]*]] = alloca [1024 x i8], align 16
2323

2424
// CHECK: ptrtoint ptr {{.*}} to [[WORD:i[0-9]+]]
2525

@@ -49,14 +49,6 @@ withUnsafeTemporaryAllocation(byteCount: 1024, alignment: 8) { buffer in
4949
// CHECK: [[ONE_KB_PTR:%[0-9]+]] = ptrtoint ptr [[ONE_KB_PTR_RAW]] to [[WORD]]
5050
// CHECK: call swiftcc void @blackHole([[WORD]] [[ONE_KB_PTR]])
5151

52-
// MARK: Alignment unknown at compile-time
53-
54-
withUnsafeTemporaryAllocation(byteCount: 1024, alignment: Int.random(in: 0 ..< 16)) { buffer in
55-
blackHole(buffer.baseAddress)
56-
}
57-
// CHECK: [[ONE_KB_RAND_PTR:%[0-9]+]] = ptrtoint ptr [[ONE_KB_RAND_PTR_RAW]] to [[WORD]]
58-
// CHECK: call swiftcc void @blackHole([[WORD]] [[ONE_KB_RAND_PTR]])
59-
6052
// MARK: Typed buffers
6153

6254
withUnsafeTemporaryAllocation(of: Int32.self, capacity: 4) { buffer in
@@ -77,3 +69,10 @@ withUnsafeTemporaryAllocation(of: Void.self, capacity: 2) { buffer in
7769
// CHECK: [[VOID_PTR:%[0-9]+]] = ptrtoint ptr [[VOID_PTR_RAW]] to [[WORD]]
7870
// CHECK: call swiftcc void @blackHole([[WORD]] [[VOID_PTR]])
7971

72+
// MARK: Alignment unknown at compile-time
73+
74+
withUnsafeTemporaryAllocation(byteCount: 1024, alignment: Int.random(in: 0 ..< 16)) { buffer in
75+
blackHole(buffer.baseAddress)
76+
}
77+
// CHECK: [[ONE_KB_RAND_PTR:%[0-9]+]] = ptrtoint ptr [[ONE_KB_RAND_PTR_RAW]] to [[WORD]]
78+
// CHECK: call swiftcc void @blackHole([[WORD]] [[ONE_KB_RAND_PTR]])

test/stdlib/TemporaryAllocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ TemporaryAllocationTestSuite.test("typedAllocationIsAligned") {
157157

158158
// MARK: Typed throws
159159
enum HomeworkError: Error, Equatable {
160-
case dogAtIt
160+
case dogAteIt
161161
case forgot
162162
}
163163

0 commit comments

Comments
 (0)