|
7 | 7 | // RUN: -enable-experimental-lifetime-dependence-inference | %FileCheck %s
|
8 | 8 | // REQUIRES: asserts
|
9 | 9 |
|
10 |
| - |
11 |
| -import Builtin |
12 |
| - |
13 | 10 | struct BufferView : ~Escapable {
|
14 | 11 | let ptr: UnsafeRawBufferPointer
|
15 | 12 | let c: Int
|
@@ -116,3 +113,50 @@ struct Container2 : ~Copyable {
|
116 | 113 | }
|
117 | 114 | }
|
118 | 115 |
|
| 116 | + |
| 117 | +struct FakeRange<Bound> { |
| 118 | + public let lowerBound: Bound |
| 119 | + public let upperBound: Bound |
| 120 | +} |
| 121 | + |
| 122 | +struct GenericBufferView<Element> : ~Escapable { |
| 123 | + typealias Index = Int |
| 124 | + typealias Pointer = UnsafeRawPointer |
| 125 | + |
| 126 | + let baseAddress: Pointer |
| 127 | + let count: Int |
| 128 | + |
| 129 | +// CHECK: sil hidden @$s28implicit_lifetime_dependence17GenericBufferViewV11baseAddress5count9dependsOnACyxGSVYls_Siqd__htclufC : $@convention(method) <Element><Storage> (UnsafeRawPointer, Int, @in_guaranteed Storage, @thin GenericBufferView<Element>.Type) -> _scope(1) @owned GenericBufferView<Element> { |
| 130 | + init<Storage>(baseAddress: Pointer, |
| 131 | + count: Int, |
| 132 | + dependsOn: borrowing Storage) { |
| 133 | + self = GenericBufferView<Element>(baseAddress: baseAddress, |
| 134 | + count: count) |
| 135 | + } |
| 136 | + // unsafe private API |
| 137 | + @_unsafeNonescapableResult |
| 138 | + init(baseAddress: Pointer, count: Int) { |
| 139 | + precondition(count >= 0, "Count must not be negative") |
| 140 | + self.baseAddress = baseAddress |
| 141 | + self.count = count |
| 142 | + } |
| 143 | + subscript(position: Pointer) -> Element { |
| 144 | + get { |
| 145 | + if _isPOD(Element.self) { |
| 146 | + return position.loadUnaligned(as: Element.self) |
| 147 | + } |
| 148 | + else { |
| 149 | + return position.load(as: Element.self) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +// CHECK: sil hidden @$s28implicit_lifetime_dependence17GenericBufferViewVyACyxGAA9FakeRangeVySVGcig : $@convention(method) <Element> (FakeRange<UnsafeRawPointer>, @guaranteed GenericBufferView<Element>) -> _scope(0) @owned GenericBufferView<Element> { |
| 154 | + subscript(bounds: FakeRange<Pointer>) -> Self { |
| 155 | + get { |
| 156 | + GenericBufferView( |
| 157 | + baseAddress: UnsafeRawPointer(bounds.lowerBound), |
| 158 | + count: bounds.upperBound.distance(to:bounds.lowerBound) / MemoryLayout<Element>.stride |
| 159 | + ) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments