File: issue-60514.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (54 lines) | stat: -rw-r--r-- 2,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// RUN: %target-swift-frontend -emit-silgen %s | %FileCheck %s

// https://github.com/apple/swift/issues/60514
// Make sure that `makeIterator` witness is picked over the non-witness.

public protocol VectorSectionReader: Sequence where Element == Result<Item, Error> {
  associatedtype Item
  var count: UInt32 { get }
  mutating func read() throws -> Item
}

public struct VectorSectionIterator<Reader: VectorSectionReader>: IteratorProtocol {
  private(set) var reader: Reader
  private(set) var left: UInt32

  init(reader: Reader, count: UInt32) {
      self.reader = reader
      self.left = count
  }

  private var end: Bool = false
  public mutating func next() -> Reader.Element? {
    guard !end else { return nil }
    guard left != 0 else { return nil }
    let result = Result(catching: { try reader.read() })
    left -= 1
    switch result {
    case .success: return result
    case .failure:
      end = true
      return result
    }
  }
}

extension VectorSectionReader {
  __consuming public func makeIterator() -> VectorSectionIterator<Self> {
    VectorSectionIterator(reader: self, count: count)
  }

  // CHECK: sil [ossa] @$s4main19VectorSectionReaderPAAE7collectSay4ItemQzGyKF
  public func collect() throws -> [Item] {
    var items: [Item] = []
    items.reserveCapacity(Int(count))
    for result in self {
      // CHECK: [[ITERATOR:%.*]] = project_box {{.*}} : $<τ_0_0 where τ_0_0 : VectorSectionReader> { var τ_0_0.Iterator } <Self>, 0
      // CHECK-NEXT: [[SELF:%.*]] = alloc_stack $Self
      // CHECK: [[MAKE_ITERATOR_REF:%.*]] = witness_method $Self, #Sequence.makeIterator : <Self where Self : Sequence> (__owned Self) -> () -> Self.Iterator : $@convention(witness_method: Sequence) <τ_0_0 where τ_0_0 : Sequence> (@in τ_0_0) -> @out τ_0_0.Iterator
      // CHECK-NEXT: apply [[MAKE_ITERATOR_REF]]<Self>([[ITERATOR]], [[SELF]]) : $@convention(witness_method: Sequence) <τ_0_0 where τ_0_0 : Sequence> (@in τ_0_0) -> @out τ_0_0.Iterator
      try items.append(result.get())
    }
    return items
  }
}