Skip to content

Commit e43a19e

Browse files
committed
Fix typos
1 parent 8979122 commit e43a19e

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

Diff for: proposals/NNNN-lifetime-dependency.md

+27-22
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,8 @@ init() {
473473
`<global constant>` must be valid over the entire program.
474474

475475
`@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:
477478

478479
```swift
479480
@lifetime(immortal)
@@ -493,7 +494,8 @@ init() {
493494

494495
### Depending on an escapable `BitwiseCopyable` value
495496

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`:
497499

498500
```swift
499501
struct Span<T>: ~Escapable {
@@ -506,15 +508,19 @@ struct Span<T>: ~Escapable {
506508
}
507509
```
508510

509-
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.
510516

511517
```swift
512518
var span: Span<T>?
513519
let buffer: UnsafeBufferPointer<T>
514520
do {
515521
let storage = Storage(...)
516522
buffer = storage.buffer
517-
span = Span(unsafeBaseAddress: buffer.baseAddress!, count: buffer.count)
523+
span = unsafe Span(unsafeBaseAddress: buffer.baseAddress!, count: buffer.count)
518524
// 🔥 'storage' may be destroyed
519525
}
520526
decode(span!) // 👿 Undefined behavior: dangling pointer
@@ -524,6 +530,7 @@ Normally, `UnsafePointer` lifetime guarantees naturally fall out of closure-taki
524530

525531
```swift
526532
extension Storage {
533+
@unsafe
527534
public func withUnsafeBufferPointer<R>(
528535
_ body: (UnsafeBufferPointer<Element>) throws -> R
529536
) rethrows -> R {
@@ -532,8 +539,8 @@ extension Storage {
532539
}
533540

534541
let storage = Storage(...)
535-
storage.withUnsafeBufferPointer { buffer in
536-
let span = Span(unsafeBaseAddress: buffer.baseAddress!, count: buffer.count)
542+
unsafe storage.withUnsafeBufferPointer { buffer in
543+
let span = unsafe Span(unsafeBaseAddress: buffer.baseAddress!, count: buffer.count)
537544
decode(span) // ✅ Safe: 'buffer' is always valid within the closure.
538545
}
539546
```
@@ -571,7 +578,7 @@ extension Span {
571578
consuming func dropFirst() -> Span<Element> {
572579
let local = Span(base: self.base + 1, count: self.count - 1)
573580
// 'local' can persist after 'self' is destroyed.
574-
return unsafeLifetime(dependent: local, dependsOn: self)
581+
return unsafe unsafeLifetime(dependent: local, dependsOn: self)
575582
}
576583
}
577584
```
@@ -584,7 +591,7 @@ Since `self.base` is an `Escapable` value, it does not propagate the lifetime de
584591
@lifetime(immortal)
585592
init() {
586593
self.value = getGlobalConstant() // OK: unchecked dependence.
587-
self = unsafeLifetime(dependent: self, dependsOn: ())
594+
self = unsafe unsafeLifetime(dependent: self, dependsOn: ())
588595
}
589596
```
590597

@@ -593,7 +600,7 @@ init() {
593600
### Relation to `Escapable`
594601

595602
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.
597604
In particular, this implies that the initializer for a non-`Escapable` type must have at least one argument or else specify `@lifetime(immortal)`.
598605

599606
```swift
@@ -602,10 +609,9 @@ struct S: ~Escapable {
602609
}
603610
```
604611

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.
607613
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:
609615

610616
```swift
611617
// `Optional` is `Escapable` only when its `Wrapped` type is `Escapable`, and
@@ -618,25 +624,24 @@ func optionalize<T: ~Escapable>(_ value: T) -> T? {
618624
return value
619625
}
620626

621-
// When used with non-Escapable types, `optionalize`'s dependencies are imposed
627+
// When used with non-Escapable types, `optionalize`'s dependencies are imposed.
622628
var maybeSpan: Span<Int>? = nil
623629
do {
624630
let a: ContiguousArray<Int> = ...
625631

626632
maybeSpan = optionalize(a.span)
627-
// maybeSpan is now dependent on borrowing `a`, copying the dependency from
628-
// `a.span` through `optionalize`
633+
// `maybeSpan` is now dependent on borrowing `a`, copying the dependency from
634+
// `a.span` through `optionalize`.
629635
}
630636
print(maybeSpan?[0]) // error, `maybeSpan` used outside of lifetime constraint
631637

632-
// But when used with Escapable types, the dependencies lose their effect
638+
// But when used with Escapable types, the dependencies lose their effect.
633639
var maybeString: String? = 0
634640
do {
635641
let s = "strings are eternal"
636642
maybeString = optionalize(s)
637643
}
638-
print(maybeString) // OK, String? is `Escapable`, no lifetime constraint
639-
644+
print(maybeString) // OK, String? is `Escapable`, so has no lifetime constraint.
640645
```
641646

642647
### Basic Semantics
@@ -656,7 +661,7 @@ The compiler must issue a diagnostic if any of the above cannot be satisfied.
656661

657662
### Grammar
658663

659-
This new syntax adds a `@lifetime` attribute that can be applied to function, initializer, and property accessor declarations:
664+
This proposal adds a `@lifetime` attribute that can be applied to function, initializer, and property accessor declarations:
660665

661666
> *lifetime-attribute***`@`** **`lifetime`** **`(`** *lifetime-dependence-list* **`)`**
662667
>
@@ -674,7 +679,7 @@ This modifier declares a lifetime dependency for the specified target.
674679
If no *lifetime-dependence-target-name* is specified, then the target is the declaration's return value.
675680
Otherwise, the target is the parameter named by the *lifetime-dependence-target-name*.
676681
The target value must be potentially non-`Escapable`.
677-
A parameter used as a target must additionally either be an `inout` parameter, or `self` in a `mutating` method.
682+
Additionally, a parameter used as a target must either be an `inout` parameter or `self` in a `mutating` method.
678683

679684
The source value of the resulting dependency can vary.
680685
For a `borrow` or `inout` dependency, the source value will be the named parameter or `self` directly.
@@ -750,7 +755,7 @@ extension Span {
750755
}
751756
```
752757

753-
A dependence may be copied from a mutable (`inout`) variable, in this case the .
758+
A dependence may be copied from a mutable (`inout`) variable.
754759
When this occurs, the dependence is inherited from whatever value the mutable variable held when the function was invoked.
755760

756761
```swift
@@ -929,7 +934,7 @@ struct Container<Element>: ~Escapable {
929934
var a: Element
930935
var b: Element
931936

932-
@lifetime(a, b)
937+
@lifetime(copy a, copy b)
933938
init(a: Element, b: Element) -> Self {...}
934939
}
935940
```

0 commit comments

Comments
 (0)