Skip to content

Commit e888d96

Browse files
committed
Lifetime dependence unit tests for '@'_addressable
1 parent af878cc commit e888d96

File tree

1 file changed

+63
-6
lines changed

1 file changed

+63
-6
lines changed

test/SILOptimizer/lifetime_dependence/semantics.swift

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// RUN: -verify \
44
// RUN: -sil-verify-all \
55
// RUN: -module-name test \
6-
// RUN: -enable-experimental-feature LifetimeDependence
6+
// RUN: -enable-experimental-feature LifetimeDependence \
7+
// RUN: -enable-experimental-feature AddressableParameters \
8+
// RUN: -enable-experimental-feature AddressableTypes
79

810
// REQUIRES: swift_in_compiler
911
// REQUIRES: swift_feature_LifetimeDependence
@@ -129,7 +131,7 @@ struct InnerTrivial {
129131
}
130132
}
131133

132-
struct InnerObject {
134+
struct Holder {
133135
let object: AnyObject
134136
var p: UnsafePointer<Int>
135137

@@ -139,19 +141,43 @@ struct InnerObject {
139141
}
140142
}
141143

144+
@_addressableForDependencies
145+
struct AddressableInt {
146+
let object: AnyObject
147+
148+
@lifetime(borrow self)
149+
borrowing func span() -> Span<Int> {
150+
let p = UnsafePointer<Int>(Builtin.unprotectedAddressOfBorrow(self))
151+
let span = Span(base: p, count: 1)
152+
return _overrideLifetime(span, borrowing: self)
153+
}
154+
}
155+
156+
@_addressableForDependencies
157+
struct AddressableObject {
158+
let object: AnyObject
159+
160+
@lifetime(borrow self)
161+
borrowing func span() -> Span<AnyObject> {
162+
let p = UnsafePointer<AnyObject>(Builtin.unprotectedAddressOfBorrow(self))
163+
let span = Span(base: p, count: 1)
164+
return _overrideLifetime(span, borrowing: self)
165+
}
166+
}
167+
142168
struct Outer {
143169
var _innerTrivial: InnerTrivial
144-
var _innerObject: InnerObject
170+
var _innerObject: Holder
145171
let trivialPointer: UnsafePointer<InnerTrivial>
146-
let objectPointer: UnsafePointer<InnerObject>
172+
let objectPointer: UnsafePointer<Holder>
147173

148174
var innerTrivialAddress: InnerTrivial {
149175
unsafeAddress {
150176
trivialPointer
151177
}
152178
}
153179

154-
var innerObjectAddress: InnerObject {
180+
var innerObjectAddress: Holder {
155181
unsafeAddress {
156182
objectPointer
157183
}
@@ -161,7 +187,7 @@ struct Outer {
161187
get { _innerTrivial }
162188
}
163189

164-
var innerObjectTemp: InnerObject {
190+
var innerObjectTemp: Holder {
165191
get { _innerObject }
166192
}
167193

@@ -475,3 +501,34 @@ func testReturnObjectTemp(outer: Outer) -> Span<Int> {
475501
// expected-error @-1{{lifetime-dependent value escapes its scope}}
476502
// expected-note @-2{{it depends on the lifetime of this parent value}}
477503
} // expected-note {{this use causes the lifetime-dependent value to escape}}
504+
505+
// =============================================================================
506+
// Scoped dependence on addressable parameters
507+
// =============================================================================
508+
509+
@lifetime(borrow arg)
510+
func testAddressableInt(arg: AddressableInt) -> Span<Int> {
511+
arg.span()
512+
}
513+
514+
@lifetime(borrow arg)
515+
func testAddressableObject(arg: AddressableObject) -> Span<AnyObject> {
516+
arg.span()
517+
}
518+
519+
@lifetime(borrow arg)
520+
func borrowAddressHelper(arg: @addressable Holder) -> Span<Int> {
521+
arg.span()
522+
}
523+
524+
@lifetime(borrow arg)
525+
func testNonAddressable(arg: Holder) -> Span<Int> {
526+
borrowAddressHelper(arg: arg)
527+
}
528+
529+
/* TODO: rdar://145872854 (SILGen: @addressable inout arguments are copied)
530+
@lifetime(borrow arg)
531+
func test(arg: inout AddressableInt) -> Span<Int> {
532+
arg.span()
533+
}
534+
*/

0 commit comments

Comments
 (0)