Skip to content

Commit 9ba7a57

Browse files
authored
Merge pull request #11 from atrick/lifetime-dependency
Update Future Direction: Lifetime dependence for closures
2 parents f16293c + b841b47 commit 9ba7a57

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

proposals/NNNN-lifetime-dependency.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ This is a key requirement for the `Span` type (previously called `BufferView`) b
5151
**Edited** (Aug 13, 2024)
5252
- Revised the same-type rule
5353

54+
**Edited** (Aug 19, 2024)
55+
- Update Future Direction: Lifetime dependence for closures
56+
5457
#### See Also
5558

5659
* [Forum discussion of Non-Escapable Types and Lifetime Dependency](https://forums.swift.org/t/pitch-non-escapable-types-and-lifetime-dependency)
@@ -1068,45 +1071,45 @@ func g1(closure: () -> NEType) // Inferred: NEType depends on 'closure'
10681071
```
10691072

10701073
For closure declarations, lifetime dependencies can be inferred on the combined list of captures and closure parameters
1071-
following the same rule as free standing functions. We can infer a lifetime dependence if the closure's return value is
1072-
nonescapable, and exactly one closure capture or closure parameter satisfies any of the following:
1074+
following inference rules similar to those for freestanding functions:
1075+
1076+
1. For closures where the return value is nonescapable, we infer a copied lifetime dependency on all captures and parameters of the same nonescapable type.
10731077

1074-
- is nonescapable, or
1075-
- is non-BitwiseCopyable and has an explicit `borrowing`, or `inout` convention
1078+
2. For closures that have a nonescapable return value and a single captured value or parameter, we infer dependence on that capture or parameter. If the capture or parameter is nonescapable, then we infer a copying dependency; otherwise, we infer a scoped dependency.
10761079

10771080
A dependence can be inferred on a closure capture as follows:
10781081

10791082
```swift
1080-
func f(arg: borrowing ArgType) -> dependsOn(arg) NEType
1083+
func f(arg: ArgType) -> dependsOn(arg) NEType
10811084

1082-
func foo(source: borrowing ArgType) {
1085+
func foo(source: ArgType) {
10831086
g1 { f(arg: source) } // ✅ Inferred: 'closure' result depends on captured 'source'
10841087
}
10851088
```
10861089

10871090
An explicit dependence on a closure capture can be spelled:
10881091

10891092
```swift
1090-
func foo(source: borrowing ArgType) {
1093+
func foo(source: ArgType) {
10911094
g1 { () -> dependsOn(source) NEType in f(arg: source) }
10921095
}
10931096
```
10941097

10951098
Similarly, a dependence can be inferred on a closure parameter:
10961099

10971100
```swift
1098-
func g2(closure: (borrowing ArgType) -> dependsOn(0) NEType)
1101+
func g2(closure: (ArgType) -> dependsOn(0) NEType)
10991102

11001103
{
1101-
g2 { (source: borrowing ArgType) in f(arg: source) } // ✅ Inferred: 'closure' result depends on 'source' parameter
1104+
g2 { (source: ArgType) in f(arg: source) } // ✅ Inferred: 'closure' result depends on 'source' parameter
11021105
}
11031106
```
11041107

11051108
An explicit dependence on a closure parameter can be spelled:
11061109

11071110
```swift
11081111
{
1109-
g2 { (source: borrowing ArgType) -> dependsOn(source) NEType in f(arg: source) } // ✅ Inferred: 'closure' result depends on 'source' parameter
1112+
g2 { (source: ArgType) -> dependsOn(source) NEType in f(arg: source) }
11101113
}
11111114
```
11121115

0 commit comments

Comments
 (0)