Skip to content

Commit d8f4cb3

Browse files
committed
New section: Immortal requirements.
1 parent 43dbc15 commit d8f4cb3

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

proposals/NNNN-lifetime-dependency.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ extension Optional {
389389
}
390390
```
391391

392-
Once the escapable instance is constructed, it is limited in scope to the caller's function body since the caller only sees the static nonescapable type. If a dynamically escapable value needs to be returned further up the stack, that can be done by chaining multiple `dependsOn(immortal)` functions.
392+
The constructed instance is returned to the caller without any lifetime dependence. The caller can pass that instance
393+
along as an argument to other functions, but those functions cannot escape it. The instance can only be returned further
394+
up the call stack by chaining multiple `dependsOn(immortal)` functions.
393395

394396
#### Depending on immutable global variables
395397

@@ -403,6 +405,35 @@ func getStaticallyAllocated() -> dependsOn(immortal) BufferReference {
403405
}
404406
```
405407

408+
#### Immortal requirements
409+
410+
`dependsOn(immortal)` requires the programmer to compose the dependent value from something that, in fact, has an immortal lifetime:
411+
412+
```swift
413+
init() dependsOn(immortal) {
414+
self.value = <global constant>
415+
}
416+
```
417+
418+
`<global constant>` must be valid over the entire program.
419+
420+
`dependsOn(immortal)` is not a way to suppress dependence in cases where the source value has unknown
421+
lifetime. Composing the result from a transient value, such as an UnsafePointer, is incorrect:
422+
423+
```swift
424+
init(pointer: UnsafePointer<T>) dependsOn(immortal) {
425+
self.value = pointer // 🛑 Incorrect
426+
}
427+
```
428+
429+
We could run into the same problem with any transient value, like a file descriptor, or even a class object:
430+
431+
```swift
432+
init() dependsOn(immortal) {
433+
self.value = Object() // 🛑 Incorrect
434+
}
435+
```
436+
406437
### Depending on an escapable `BitwiseCopyable` value
407438

408439
The source of a lifetime depenence may be an escapable `BitwiseCopyable` value. This is useful in the implementation of data types that internally use `UnsafePointer`:

0 commit comments

Comments
 (0)