Skip to content

Commit f7fb099

Browse files
authored
Merge pull request #78432 from atrick/test-span-prop
Add test cases for Span-providing properties.
2 parents 2d17294 + bdd4f1f commit f7fb099

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// RUN: %target-swift-frontend %s -emit-sil \
2+
// RUN: -o /dev/null \
3+
// RUN: -verify \
4+
// RUN: -sil-verify-all \
5+
// RUN: -module-name test \
6+
// RUN: -disable-access-control \
7+
// RUN: -enable-experimental-feature LifetimeDependence \
8+
// RUN: -enable-experimental-feature Span
9+
10+
// REQUIRES: swift_in_compiler
11+
// REQUIRES: swift_feature_LifetimeDependence
12+
// REQUIRES: swift_feature_Span
13+
14+
// Test dependencies on the standard library Span APIs.
15+
16+
// =============================================================================
17+
// Span-providing properties
18+
// =============================================================================
19+
20+
extension UnsafeRawBufferPointer {
21+
@available(SwiftStdlib 6.1, *)
22+
public var storage: RawSpan {
23+
@lifetime(borrow self)
24+
get {
25+
let span = RawSpan(_unsafeBytes: self)
26+
return _overrideLifetime(span, borrowing: self)
27+
}
28+
}
29+
}
30+
31+
@available(SwiftStdlib 6.1, *)
32+
func read(_ span: RawSpan) {}
33+
34+
@available(SwiftStdlib 6.1, *)
35+
func testUBPStorage(ubp: UnsafeRawBufferPointer) {
36+
// 'span' is valid within the lexical scope of variable 'ubp', which is the entire function.
37+
let span = ubp.storage
38+
read(span)
39+
}
40+
41+
@available(SwiftStdlib 6.1, *)
42+
@lifetime(borrow ubp)
43+
func testUBPStorageReturn(ubp: UnsafeRawBufferPointer) -> RawSpan {
44+
// 'storage' can be returned since the function's return value also has a dependence on 'ubp'.
45+
return ubp.storage
46+
}
47+
48+
@available(SwiftStdlib 6.1, *)
49+
@lifetime(borrow ubp)
50+
func testUBPStorageCopy(ubp: UnsafeRawBufferPointer) -> RawSpan {
51+
let localBuffer = ubp
52+
return localBuffer.storage // expected-error {{lifetime-dependent value escapes its scope}}
53+
// expected-note @-2{{it depends on the lifetime of variable 'localBuffer'}}
54+
// expected-note @-2{{this use causes the lifetime-dependent value to escape}}
55+
}
56+
57+
@available(SwiftStdlib 6.1, *)
58+
func testUBPStorageEscape(array: [Int64]) {
59+
var span = RawSpan()
60+
array.withUnsafeBytes {
61+
span = $0.storage // expected-error {{lifetime-dependent value escapes its scope}}
62+
// expected-note @-2{{it depends on the lifetime of argument '$0'}}
63+
// expected-note @-2{{this use causes the lifetime-dependent value to escape}}
64+
}
65+
read(span)
66+
}

0 commit comments

Comments
 (0)