forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request swiftlang#78432 from atrick/test-span-prop
Add test cases for Span-providing properties.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// RUN: %target-swift-frontend %s -emit-sil \ | ||
// RUN: -o /dev/null \ | ||
// RUN: -verify \ | ||
// RUN: -sil-verify-all \ | ||
// RUN: -module-name test \ | ||
// RUN: -disable-access-control \ | ||
// RUN: -enable-experimental-feature LifetimeDependence \ | ||
// RUN: -enable-experimental-feature Span | ||
|
||
// REQUIRES: swift_in_compiler | ||
// REQUIRES: swift_feature_LifetimeDependence | ||
// REQUIRES: swift_feature_Span | ||
|
||
// Test dependencies on the standard library Span APIs. | ||
|
||
// ============================================================================= | ||
// Span-providing properties | ||
// ============================================================================= | ||
|
||
extension UnsafeRawBufferPointer { | ||
@available(SwiftStdlib 6.1, *) | ||
public var storage: RawSpan { | ||
@lifetime(borrow self) | ||
get { | ||
let span = RawSpan(_unsafeBytes: self) | ||
return _overrideLifetime(span, borrowing: self) | ||
} | ||
} | ||
} | ||
|
||
@available(SwiftStdlib 6.1, *) | ||
func read(_ span: RawSpan) {} | ||
|
||
@available(SwiftStdlib 6.1, *) | ||
func testUBPStorage(ubp: UnsafeRawBufferPointer) { | ||
// 'span' is valid within the lexical scope of variable 'ubp', which is the entire function. | ||
let span = ubp.storage | ||
read(span) | ||
} | ||
|
||
@available(SwiftStdlib 6.1, *) | ||
@lifetime(borrow ubp) | ||
func testUBPStorageReturn(ubp: UnsafeRawBufferPointer) -> RawSpan { | ||
// 'storage' can be returned since the function's return value also has a dependence on 'ubp'. | ||
return ubp.storage | ||
} | ||
|
||
@available(SwiftStdlib 6.1, *) | ||
@lifetime(borrow ubp) | ||
func testUBPStorageCopy(ubp: UnsafeRawBufferPointer) -> RawSpan { | ||
let localBuffer = ubp | ||
return localBuffer.storage // expected-error {{lifetime-dependent value escapes its scope}} | ||
// expected-note @-2{{it depends on the lifetime of variable 'localBuffer'}} | ||
// expected-note @-2{{this use causes the lifetime-dependent value to escape}} | ||
} | ||
|
||
@available(SwiftStdlib 6.1, *) | ||
func testUBPStorageEscape(array: [Int64]) { | ||
var span = RawSpan() | ||
array.withUnsafeBytes { | ||
span = $0.storage // expected-error {{lifetime-dependent value escapes its scope}} | ||
// expected-note @-2{{it depends on the lifetime of argument '$0'}} | ||
// expected-note @-2{{this use causes the lifetime-dependent value to escape}} | ||
} | ||
read(span) | ||
} |