# Ownership Annotations

**URL:** <https://forums.swift.org/t/ownership-annotations/11276>\
**Category:** Pitches\
**Created:** [March 22, 2018, 6:07pm UTC](https://forums.swift.org/t/ownership-annotations/11276 "2018-03-22T18:07:02Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/huon/32/1887_2.png) [@huon](https://forums.swift.org/u/huon)\
**Post date:** [March 24, 2018, 2:29am UTC](https://forums.swift.org/t/ownership-annotations/11276/8 "2018-03-24T02:29:21Z")

</div>

> [@codafi](#):
>
> This means that the restriction on ownership-annotation mismatches could be lifted

I don't this restriction was actually mentioned above? But I'll assume it's something like

> Ownership annotations on witnesses satisfying protocol requirements must match the annotations on the protocol declaration, e.g.
> 
> ```swift
> protocol Foo {
> associatedtype T
> consuming func f() 
> func g(x: consume T, y: borrow T)
> }
> struct Bar: Foo {
> func f() {} // error: missing 'consuming'
> func g(x: borrow T, y: consume T) {} // error: 'x' should be 'consume', 'y' should be 'borrow'
> }
> 
> ```

Sound reasonable?

> [@codafi](#):
>
> Unannotated witnesses to a protocol requirement that specifies ownership shall inherit the ownership annotations of the requirement. In short, a protocol vendor is free to change ownership annotations without fear of an API-breaking change for clients that did not explicitly opt-in to working with ownership annotations.

The proposal doesn't actually say if a witness can be annotated. In fact, I don't actually think there's a way to get to make this API-compatible with the annotations-must-match restriction:

- if they _can_ be annotated, then the protocol cannot have its annotations changed in a API-compatible way, as type that conforms may have annotations matching the old declaration, but these won't match (and will violate the restriction)
- if they can't be annotated, adding even a defaulted requirement may break code along the lines of @orobio's example, where there's a (previously) unrelated method that has annotations: adding the requirement makes this method a witness, but it has annotations!

This, along with not wanting to silently change behaviour as @orobio's example demonstrates, pushes me more towards no restriction. It seems feasible to have tooling (or a compiler flag/Xcode setting) that will flag mismatches between witnesses and protocol requirements on an opt-in basis.

> [@codafi](#):
>
> Though the user’s annotation specifies their expectation of the ownership-convention of a given parameter, a protocol with explicit ownership annotations indicates the API vendor expects a certain kind of usage to follow naturally from its definition. Allowing user overrides dilutes that expectation.

The abstract idea of a protocol may not match all concrete implementations of it, because they may be more specialised or have "surprising" trade-offs (e.g. optimise one function to be O(1) at the cost of another becoming O(n2)). For instance, imagine a `Bag` protocol (ala `Collection`):

```swift
protocol Bag {
    associatedtype Element
    // presumably 'add' will usually store the value somewhere
    func add(_ value: consume Element)
    ...
}

extension Set: Bag {
    // 'add' does store the value into memory somewhere, and so 
    // benefits from 'consume'
    func add(_ value: consume Element) { ... }
    ...
}
extension Array: Bag {
    // Same as 'Set'
    func add(_ value: consume Element) { ... }
    ...
}

struct StringTrie: Bag {
    typealias Element = String
    // this is a trie, and so never actually stores 'value': it is
    // broken into parts so there's no point in consuming the value
    func add(_ value: borrow String) { ... }
    ...
}

```

Concrete users of `StringTrie` avoid retains and releases, and generic users pay "only" the cost of the extra function call of the thunk, since `consume`-\>`borrow` is cheap. I think the protocol has the correct annotation here, as it is optimising for the most common case (and, in a moveonly world, maintaining maximum generality of what types can conform to it), but the concrete type has extra constraints that allow it to relax the requirements.

> [@codafi](#):
>
> Mixing ownership annotations introduces thunking - thunking is not free. If a requirement is annotated borrow and its witness annotated consume the compiler will emit a thunk between these conventions that copies anyways. In addition to the overhead of the copy, if you are in a non-inlineable context, you also incur the cost of the thunk.

In a world where distinctions between `inout`, `consume` and `borrow` are enforced strictly there's a hierarchy: an owned value (`consume`) can always have a mutable pointer (`inout`) created to it for free and a mutable pointer can always be downgraded to an immutable one (`borrow`) for free . This implies that concerns about the performance of copying don't apply to satisfying a `consume` requirement with an `inout` witness (definitely a bit weird, but it's theoretically okay), an `inout` requirement with a `borrow` witness (i.e. protocol is giving the option for mutation, but the concrete implementation doesn't need it) and, transitively, a `consume` requirement with a `borrow` one.

Of course, the cost of the thunk call itself could be problematic enough (although these very small thunks seem like they'd be pretty much just the cost of a call to a function in another dylib?).

> [@codafi](#):
>
> Mixing ownership annotations will cause headaches in a future world where moveonly types play a role. If a witness that mixes ownership annotations comes under the scope of a moveonly context, an API and ABI-breaking change is required to correct it. If we allow for this transparently, it would enable users to void the consumption contract with moveonly types accidentally.

I don't think this is a particular concern: it will always be statically known if a type _could_ be a moveonly type (due to source compatibility, there can't be a way to retroactively make a type moveonly, or instantiate a generic parameter with a moveonly type unless the parameter is marked as "possibly moveonly" in some form), and so statically known when ownership annotations must match. I don't think there's much value in maintaining consistency with behaviour with copyable types, because this doesn't seem particularly different to, say, `func f(_: consume T) {} func g(x: borrow T) { f(x) }` (that's also a mismatch in `borrow`/`consume`: that works with copyable `T` and can't if it's moveonly), and there's numerous other ways in which moveonly types different in behaviour (e.g. `let t = (x, x)`).

> [@codafi](#):
>
> consume function parameters are “consumed” by that function; the existing default for function parameters

> [@codafi](#):
>
> a compiler-provided default will be selected - currently the owned convention, corresponding to the consume annotation

> [@codafi](#):
>
> The consume attribute, being the existing default, has no effect

(This changed to `borrow` very recently: [Plan to change the convention of passed "Normal Parameters" from +1 to +0 - #9 by Michael\_Gottesman](https://forums.swift.org/t/plan-to-change-the-convention-of-passed-normal-parameters-from-1-to-0/10779/9))

---

_[View the full topic](https://forums.swift.org/t/ownership-annotations/11276)._
