You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/NNNN-lifetime-dependency.md
+27-22
Original file line number
Diff line number
Diff line change
@@ -473,7 +473,8 @@ init() {
473
473
`<global constant>` must be valid over the entire program.
474
474
475
475
`@lifetime(immortal)` is not a way to suppress dependence in cases where the source value has unknown
476
-
lifetime. Composing the result from a transient value, such as an UnsafePointer, is incorrect:
476
+
lifetime.
477
+
Composing the result from a transient value, such as an UnsafePointer, is incorrect:
477
478
478
479
```swift
479
480
@lifetime(immortal)
@@ -493,7 +494,8 @@ init() {
493
494
494
495
### Depending on an escapable `BitwiseCopyable` value
495
496
496
-
The source of a lifetime dependence may be an escapable `BitwiseCopyable` value. This is useful in the implementation of data types that internally use `UnsafePointer`:
497
+
The source of a lifetime dependence may be an escapable `BitwiseCopyable` value.
498
+
This is useful in the implementation of data types that internally use `UnsafePointer`:
When the source of a dependence is escapable and `BitwiseCopyable`, then the operation must be marked as `@unsafe` when using strict memory safety as introduced in [SE-0458](0458-strict-memory-safety.md). By convention, the argument label should also include the word `unsafe` in its name, as in `unsafeBaseAddress` above. This communicates to anyone who calls the function that they are reponsible for ensuring that the value that the result depends on is valid over all uses of the result. The compiler can't guarantee safety because `BitwiseCopyable` types do not have a formal point at which the value is destroyed. Specifically, for `UnsafePointer`, the compiler does not know which object owns the pointed-to storage.
511
+
When the source of a dependence is escapable and `BitwiseCopyable`, then the operation must be marked as `@unsafe` when using strict memory safety as introduced in [SE-0458](0458-strict-memory-safety.md).
512
+
By convention, the argument label should also include the word `unsafe` in its name, as in `unsafeBaseAddress` above.
513
+
This communicates to anyone who calls the function that they are reponsible for ensuring that the value that the result depends on is valid over all uses of the result.
514
+
The compiler can't guarantee safety because `BitwiseCopyable` types do not have a formal point at which the value is destroyed.
515
+
Specifically, for `UnsafePointer`, the compiler does not know which object owns the pointed-to storage.
The lifetime dependencies described in this document can be applied only to potentially non-`Escapable` return values.
596
-
Further, any return value that is non-`Escapable` must declare a lifetime dependency.
603
+
Further, any return value that is potentially non-`Escapable` must declare a lifetime dependency.
597
604
In particular, this implies that the initializer for a non-`Escapable` type must have at least one argument or else specify `@lifetime(immortal)`.
598
605
599
606
```swift
@@ -602,10 +609,9 @@ struct S: ~Escapable {
602
609
}
603
610
```
604
611
605
-
In generic contexts, `~Escapable` indicates that a type is *not required* to be `Escapable`, but is *not* a strict indication that a type is not `Escapable` at all.
606
-
Generic types can be conditionally `Escapable`, and type parameters to generic functions that are declared `~Escapable` can be given `Escapable` types as arguments.
612
+
In generic contexts, `~Escapable` indicates that a type is *not required* to be `Escapable`, but the type may be conditionally `Escapable` depending on generic substitutions.
607
613
This proposal refers to types in these situations as "potentially non-`Escapable`" types.
608
-
Return types that are potentially non-`Escapable` require lifetime dependencies to be specified, but when they are used in contexts where a value becomes `Escapable` due to the type parameters used, then those lifetime dependencies lose their effect, since
614
+
Declarations with return types that are potentially non-`Escapable` require lifetime dependencies to be specified, but when those declarations are used in contexts where their result becomes `Escapable` due to the type arguments used, then those lifetime dependencies have no effect:
609
615
610
616
```swift
611
617
// `Optional` is `Escapable` only when its `Wrapped` type is `Escapable`, and
0 commit comments