Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime dependencies #2750

Open
wants to merge 69 commits into
base: main
Choose a base branch
from

Conversation

jckarter
Copy link
Contributor

Propose a @lifetime attribute to explicitly annotate declarations that produce non-Escapable values.

This proposal is also intended to serve as documentation of the state of the compiler implementation of the LifetimeDependence experimental feature for early adopters.

tbkka and others added 30 commits January 24, 2024 18:16
We need to separate description of the dependency itself --
which places a bound on when particular objects can be
destroyed -- from the syntax.
In particular, the syntax specifies a relationship between
two objects, but that relationship is not always a
lifetime dependency itself (because of "copied" dependencies).
Also, fix the proposed syntax for properties
Changed `StorageView` and `BufferReference` to `Span` to match the si…
- Dependent parameters
- Dependent properties
- Conditional dependencies
- Immortal lifetimes
- Depending on immutable global variables
- Depending on an escapable `BitwiseCopyable` value
- Depending on an escapable `BitwiseCopyable` value
Remove Lifetime Dependencies for Computed Properties

Remove Lifetime Dependencies between arguments
- Value component lifetime
- Abstract lifetime components
- Protocol lifetime requirements
- Structural lifetime dependencies
Add several new sections
Add dependent parameters, immortal dependencies, and 'selfDependsOn'.
Update and cleanup the grammar.
atrick and others added 20 commits June 14, 2024 11:02
Update the free-standing function inference rules.
Added future directions: Function type syntax and closures
Add an alternative spelling: `lifetime` vs. `dependsOn`.
This section was a distraction. Changing the position of `dependsOn`
for initializers is not something we need to consider initially.
Added the same-type inference rule.
@Lifetime annotation + simplified implicit lifetime dependencies
To clarify that the annotation refers to the parameter name, not the
label.
Update Future Direction: Lifetime dependence for closures

```swift
let array = getContiguousArrayWithData()
let buff = array.unsafeBufferPointer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that some declarations use @unsafe. Should these snippets be spelled let buff = unsafe array.unsafeBufferPointer?

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.
Generic types can be conditionally `Escapable`, and type parameters to generic functions that are declared `~Escapable` can be given `Escapable` types as arguments.
This proposal refers to types in these situations as "potentially non-`Escapable`" types.
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete sentence

}
```

A dependence may be copied from a mutable (`inout`) variable, in this case the .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete sentence

var a: Element
var b: Element

@lifetime(a, b)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should specify the type of dependence

@jckarter
Copy link
Contributor Author

@apple-fcloutier I think I fixed everything you noted, thanks!

@rjmccall rjmccall added the LSG Contains topics under the domain of the Language Steering Group label Mar 24, 2025
Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some typos

- `unsafeLifetime` is currently implemented as `_overrideLifetime`
- Clarify distinction between using an inout parameter as a source or target,
  and point out what occurs (nothing) if a parameter is both the source and
  target of a dependency
- Give an explicit example of conjoined dependence with multiple lifetimes
  on the same target
@jckarter jckarter force-pushed the tbkka-lifetime-dependency branch from 9ab1df6 to 5bc6202 Compare March 26, 2025 00:45
jckarter and others added 3 commits March 26, 2025 10:09
Thanks @glessard for catching these typos!

Co-authored-by: Guillaume Lessard <[email protected]>
- Parameters are referred to by their internal binding name as target or source
  of a dependency
- No effect on function types yet
- Unsafe dependencies are specifically on `Escapable & BitwiseCopyable` types
- Removed misleading example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LSG Contains topics under the domain of the Language Steering Group
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants