Skip to content

Commit c628bc1

Browse files
committed
Add test for lifetime dependence inference in subscripts
1 parent 12ac804 commit c628bc1

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

test/SIL/implicit_lifetime_dependence.swift

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
// RUN: -enable-experimental-lifetime-dependence-inference | %FileCheck %s
88
// REQUIRES: asserts
99

10-
11-
import Builtin
12-
1310
struct BufferView : ~Escapable {
1411
let ptr: UnsafeRawBufferPointer
1512
let c: Int
@@ -116,3 +113,50 @@ struct Container2 : ~Copyable {
116113
}
117114
}
118115

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

Comments
 (0)