Files
swift-mirror/test/Parse/lifetime_attr.swift
Andrew Trick 080b68292d Fix a compiler crash with '@'_lifetime(inout x), add diagnostic
This is a common mistake made more common be suggestions of existing diagnostic
that tell users not to use a 'copy' dependency.

Report a diagnostic error rather than crashing the compiler. Fix the diagnostic
output to make sense relative to the source location.

Fixes rdar://154136015 ([nonescapable] compiler assertion with @_lifetime(x: inout x))
2025-06-25 16:34:43 -07:00

36 lines
1.1 KiB
Swift

// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature Lifetimes
// REQUIRES: swift_feature_Lifetimes
struct E {}
struct NE : ~Escapable {}
@_lifetime(copy ne)
func derive(_ ne: NE) -> NE {
ne
}
@_lifetime(borrow ne1, copy ne2)
func derive(_ ne1: NE, _ ne2: NE) -> NE {
if (Int.random(in: 1..<100) < 50) {
return ne1
}
return ne2
}
@_lifetime // expected-error{{expected '(' after lifetime dependence specifier}}
func testMissingLParenError(_ ne: NE) -> NE { // expected-error{{cannot infer the lifetime dependence scope on a function with a ~Escapable parameter, specify '@_lifetime(borrow ne)' or '@_lifetime(copy ne)'}}
ne
}
@_lifetime() // expected-error{{expected 'copy', 'borrow', or '&' followed by an identifier, index or 'self' in lifetime dependence specifier}}
func testMissingDependence(_ ne: NE) -> NE { // expected-error{{cannot infer the lifetime dependence scope on a function with a ~Escapable parameter, specify '@_lifetime(borrow ne)' or '@_lifetime(copy ne)'}}
ne
}
@_lifetime(borrow borrow)
func testNameConflict(_ borrow: E) -> NE {
NE()
}