Skip to content

Proposal: @called(once) as a way to state that a function is called… - #3429

Open
xedin wants to merge 8 commits into
swiftlang:mainfrom
xedin:called_once
Open

xedin wants to merge 8 commits into
swiftlang:mainfrom
xedin:called_once

Conversation

@xedin

@xedin xedin commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

… at-most once

This proposal introduces a @called(once) attribute to model at-most once execution of a function. Making function execution behavior visible to the compiler has two advantages. First, it makes it possible to diagnose incorrect usage of such functions. But, it also allows the compiler to understand how closure captures could affect variable lifetimes. This can improve compatibility with non-Copyable types.

@xwu

xwu commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

FYI: SE-0073.

… at-most once

This proposal introduces a `@called(once)` attribute to model at-most once
execution of a function. Making function execution behavior visible to the
compiler has two advantages. First, it makes it possible to diagnose incorrect
usage of such functions. But, it also allows the compiler to understand how
closure captures could affect variable lifetimes. This can improve
compatibility with non-Copyable types.
@rjmccall rjmccall added LSG Contains topics under the domain of the Language Steering Group new proposal Adds a new proposal document labels Aug 25, 2026
xedin and others added 6 commits August 27, 2026 10:28
…pyable` more prominent in 'Proposed solution' section
…once)` and `consuming` methods and `defer` blocks`
- Rename the at-most-once attribute to `@callable(once)` and add the
  exactly-once `@once` attribute, modeled with a compiler-internal
  `Deinitable` protocol.
- Add `withoutActuallyDiscarding(_:do:)`, storage rules, overriding,
  witness, and overloading rules, and C/ObjC interoperability.
- Describe the limits of the exactly-once guarantee, runtime support,
  and the standard library adoption policy.
- Add prior art, new alternatives considered, and future directions.
- Address review feedback and make editorial fixes.
- Allow `borrowing` and `inout` parameters with `@once` and
  `@callable(once)` types.
- Clarify that linear types suppress both `Copyable` and `Deinitable`,
  and show the declarations of the invertible protocols.
- Describe the `async` and isolation behavior of
  `withoutActuallyDeiniting(_:do:)`.
- Revise the overloading rules for execution semantics, and describe
  how libraries can offer overloads that closure literals resolve to.
- Generalize narrowing and widening of execution semantics in Source
  compatibility.
- Add alternatives considered for importing nullable `called_once`
  parameters as optional `@callable(once)`, give examples of APIs that
  need `@callable(once)`, and revise the arguments about attribute
  spellings.
@xedin
xedin marked this pull request as ready for review November 13, 2026 23:12

This branch has not been deployed

No deployments
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 new proposal Adds a new proposal document

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants