Skip to content

Commit 461f66d

Browse files
committed
[docs] document _fixLifetime
1 parent e29916d commit 461f66d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

docs/StandardLibraryProgrammersManual.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ These three functions are assertions that will trigger a run time trap if violat
7373
* `_debugPrecondition` will execute when **user code** is built with assertions enabled. Use this for invariant enforcement that's useful while debugging, but might be prohibitively expensive when user code is configured without assertions.
7474
* `_sanityCheck` will execute when **standard library code** is built with assertions enabled. Use this for internal only invariant checks that useful for debugging the standard library itself.
7575

76+
#### `_fixLifetime`
77+
78+
A call to `_fixLifetime` is considered a use of its argument, meaning that the argument is guaranteed live at least up until the call. It is otherwise a nop. This is useful for guaranteeing the lifetime of a value while inspecting its physical layout. Without a call to `_fixLifetime`, the last formal use may occur while the value's bits are still being munged.
79+
80+
*Example:*
81+
82+
```swift
83+
var x = ...
84+
defer { _fixLifetime(x) } // Guarentee at least lexical lifetime for x
85+
let theBits = unsafeBitCast(&x, ...)
86+
... // use of theBits in ways that may outlive x if it weren't for the _fixLifetime call
87+
```
88+
7689

7790
### Annotations
7891

0 commit comments

Comments
 (0)