The “add stubs” fixit uses “__consuming” and “_read” (which requires “yield”)

I just tried conforming a type to Collection in an Xcode 11 playground, and clicked the “add protocol stubs” fix-it. The stubs that were added use underscored keywords. Is this intentional?

Starting with this:

struct Foo<T> {
  var x: T
}

extension Foo: Sequence where T: Sequence {
  typealias Element = T.Element
  typealias Iterator = T.Iterator
}

Clicking the error (Foo does not conform to Sequence) and choosing “Fix” to add protocol stubs, produces the following:

__consuming func makeIterator() -> T.Iterator {
  <#code#>
}

I did not expect to see __consuming here.

• • •

Completing that function, we can then add Collection conformance. The first fixit stub is for the Index type, the next one is for startIndex and endIndex, the third is for the subscript, and the fourth is for index(after:).

With all the fixits clicked to add stubs (and all but the subscript filled in), we have the following:

extension Foo: Collection where T: Collection {
  typealias Index = T.Index
  
  var startIndex: T.Index { x.startIndex }
  var endIndex: T.Index { x.endIndex }
  
  func index(after i: T.Index) -> T.Index {
    return x.index(after: i)
  }
  
  subscript(position: T.Index) -> T.Element {
    _read {
      <#code#>
    }
  }
}

I did not expect to see _read there.

Furthermore, the subscript has an error “Accessor must yield before returning”. If we try to write “return x[position]”, we get another error “Unexpected non-void return value in void function”. We have to write “yield x[position]” instead.

Were these keywords—__consuming, _read, and yield—intended to show up here?

1 Like

Related: [SR-11128] Protocol stubs include __consuming and __owned · Issue #53524 · apple/swift · GitHub

1 Like

This has happened to me too.