[Pitch #2] Called-once function types

It turns out a separate overload isn't needed. Like withoutActuallyEscaping(_:do:), the compiler type checks calls to withoutActuallyDeiniting(_:do:) specially, and it treats the function as async when the body is async. The body closure's isolation is inferred from its context, like any other non-escaping closure, so the call never switches executors. With NonisolatedNonsendingByDefault enabled, the body of a call in a nonisolated(nonsending) function is nonisolated(nonsending) too.

I will clarify this in the proposal.

4 Likes

I have pushed a 3rd revision of the proposal to address feedback in this thread:

  • borrowing is now allowed on @once and @callable(once) types and the implications of inout are discussed.
  • The requirements of linear types are clarified (the must be both Copyable and Deinitable).
  • The invertible marker protocol declarations in the standard library, including Deinitable, are now included.
  • Support for async closures passed to withoutActuallyDeiniting(_:do:) is discussed.
  • The nitty-gritty details of overloading rules have been added to cover important source compatibility considerations. Also, examples of how libraries can successfully offer overloads on execution semantics is now covered in detail.
  • An alternative considered was added for importing nullable called_once parameters as optional @callable(once).

Does this proposal address defer blocks as well? Consuming Noncopyable in defer block · Issue #76388 · swiftlang/swift · GitHub

I never realized that defer blocks were closures. I assumed that they were syntactic blocks (like if blocks). But maybe it's some implementation detail leaking.

Or do we consider it a separate bug, since defer shouldn't be closures?

It's true that an implementation of this proposal would unblock fixing that issue, but I don't think it should be explicitly called out in the proposal. Modeling defer as a closure is an implementation detail of the compiler rather than a language design choice.

1 Like

Actually this reminds me, if you have a local function that is only called and not used as a value:

func f() { ... }

f()

What we do today is basically equivalent to forming a closure and immediately calling it.

Should we say that the type of the closure that we form when you write f there be called-once? It would then follow that f() can consume non-copyable captures and so on, as long as the calls to f() are in the right spots.

It would then be reasonable to just specify the behavior of defer as being like a local function that is called at each exit point. Today the actual behavior of defer is not documented anywhere as far as I'm aware, and it would be easier to document it as a special kind of local function than to have to fully describe every possible interaction with every other language feature.

1 Like

You’re suggesting that we delay checking capture rules of local functions until we can analyze their uses? That’s probably feasible. But we can’t really reason about whether there’s exactly one call site on every path in the typechecker, so I’m not sure how much we could take advantage of the delay.

Probably the more meaningful impact is that we could let type information from use sites flow back into the function — maybe not parameter or result types, but at least things like that the function is required to be escaping or sendable. But again I don’t know how much this would really help because I think we can infer conflicting constraints from captures just as well. Maybe there’s a case I’m not thinking of.

1 Like

Ah, never mind. I thought we already did that for captures, but maybe it's not nearly as general as I'm imagining. At least for escaping vs non-escaping uses, there's a distinction though:

func f(_ fn: () -> ()) -> () -> () {
  func g1() {
    fn()  // this seems to work?
  }

  func g2() {
    fn()  // we diagnose this
  }

  g1()
  return g2
}
1 Like

I think the current rule is that we restrict the type of the function by its captures: if it has an inout or ~Escapable capture, we infer it as non-escaping; if it has non-Sendable captures, we infer it as non-Sendable (regardless of RBI); etc. There’s no reverse flow.

2 Likes