|
1 |
| -// RUN: %target-swift-frontend -emit-sil -O %s | %FileCheck %s |
| 1 | +// RUN: %target-swift-frontend -parse-stdlib -parse-as-library -emit-sil -O %s | %FileCheck %s |
2 | 2 | // REQUIRES: optimized_stdlib,swift_stdlib_no_asserts
|
3 | 3 |
|
| 4 | +import Swift |
| 5 | + |
4 | 6 | // Opaque, unoptimizable functions to call.
|
5 | 7 | @_silgen_name("takesConstRawPointer")
|
6 | 8 | func takesConstRawPointer(_ x: UnsafeRawPointer)
|
@@ -99,3 +101,23 @@ public func arrayLiteralPromotion() {
|
99 | 101 | // CHECK: apply [[FN]]([[PTR]])
|
100 | 102 | }
|
101 | 103 |
|
| 104 | +// Test sil verification at -O with bind_memory and rebind_memory |
| 105 | +// where the rebind is in a defer block. |
| 106 | +// |
| 107 | +// CHECK-LABEL: sil @$s18pointer_conversion21testWithMemoryRebound6rawPtr2to8capacity_q_Bp_xmSiq_SPyxGKXEtKr0_lF |
| 108 | +// CHECK: [[BIND:%.*]] = bind_memory %1 : $Builtin.RawPointer, %{{.*}} : $Builtin.Word to $*T |
| 109 | +// CHECK: rebind_memory %1 : $Builtin.RawPointer to [[BIND]] : $Builtin.Word |
| 110 | +// CHECK-LABEL: } // end sil function '$s18pointer_conversion21testWithMemoryRebound6rawPtr2to8capacity_q_Bp_xmSiq_SPyxGKXEtKr0_lF' |
| 111 | +public func testWithMemoryRebound<T, Result>( |
| 112 | + rawPtr: Builtin.RawPointer, |
| 113 | + to type: T.Type, |
| 114 | + capacity count: Int, |
| 115 | + _ body: (_ pointer: UnsafePointer<T>) throws -> Result |
| 116 | +) rethrows -> Result { |
| 117 | + let binding = |
| 118 | + Builtin.bindMemory(rawPtr, count._builtinWordValue, T.self) |
| 119 | + defer { |
| 120 | + Builtin.rebindMemory(rawPtr, binding) |
| 121 | + } |
| 122 | + return try body(.init(rawPtr)) |
| 123 | +} |
0 commit comments