"Fossies" - the Fresh Open Source Software Archive

Member "swift-swift-5.1.2-RELEASE/validation-test/compiler_crashers_2_fixed/0003-rdar20564378.swift" (7 Nov 2019, 4262 Bytes) of package /linux/misc/swift-swift-5.1.2-RELEASE.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Swift source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // RUN: not %target-swift-frontend %s -typecheck
    2 
    3 public protocol Q_SequenceDefaults {
    4   typealias Element
    5   typealias Iterator : IteratorProtocol
    6   func makeIterator() -> Iterator
    7 }
    8 
    9 extension Q_SequenceDefaults {
   10   public final var underestimatedCount: Int { return 0 }
   11   public final func preprocessingPass<R>(body: (Self)->R) -> R? {
   12     return nil
   13   }
   14 
   15   /// Create a ContiguousArray containing the elements of `self`,
   16   /// in the same order.
   17   public final func copyToContiguousArray() -> ContiguousArray<Iterator.Element> {
   18     let initialCapacity = underestimatedCount
   19 
   20     var result = _ContiguousArrayBuffer<Iterator.Element>(
   21       count: initialCapacity, minimumCapacity: 0)
   22 
   23     var iter = self.makeIterator()
   24     while let x = iter.next() {
   25       result += CollectionOfOne(x)
   26     }
   27     return ContiguousArray(result)
   28   }
   29 
   30   /// Initialize the storage at baseAddress with the contents of this
   31   /// sequence.
   32   public final func initializeRawMemory(
   33     baseAddress: UnsafeMutablePointer<Iterator.Element>
   34   ) {
   35     var p = baseAddress
   36     var iter = self.makeIterator()
   37     while let element = iter.next() {
   38       p.initialize(to: element)
   39       p += 1
   40     }
   41   }
   42 
   43   public final static func _constrainElement(Iterator.Element) {}
   44 }
   45 
   46 /// A type that can be iterated with a `for`\ ...\ `in` loop.
   47 ///
   48 /// `Sequence` makes no requirement on conforming types regarding
   49 /// whether they will be destructively "consumed" by iteration.  To
   50 /// ensure non-destructive iteration, constrain your *sequence* to
   51 /// `Collection`.
   52 public protocol Q_Sequence : Q_SequenceDefaults {
   53   /// A type that provides the *sequence*\ 's iteration interface and
   54   /// encapsulates its iteration state.
   55   typealias Iterator : IteratorProtocol
   56 
   57   func makeIterator() -> Iterator
   58 
   59   /// Return a value less than or equal to the number of elements in
   60   /// self, **nondestructively**.
   61   ///
   62   /// Complexity: O(N)
   63   var underestimatedCount: Int
   64 
   65   /// If `self` is multi-pass (i.e., a `Collection`), invoke the function
   66   /// on `self` and return its result.  Otherwise, return `nil`.
   67   func preprocessingPass<R>(body: (Self)->R) -> R?
   68 
   69   /// Create a ContiguousArray containing the elements of `self`,
   70   /// in the same order.
   71   func copyToContiguousArray() -> ContiguousArray<Element>
   72 
   73   /// Initialize the storage at baseAddress with the contents of this
   74   /// sequence.
   75   func initializeRawMemory(
   76     baseAddress: UnsafeMutablePointer<Element>
   77   )
   78   
   79   static func _constrainElement(Element)
   80 }
   81 
   82 public extension IteratorProtocol {
   83   typealias Iterator = Self
   84   
   85   public final func makeIterator() -> Iterator {
   86     return self
   87   }
   88 }
   89 
   90 public protocol Q_CollectionDefaults : Q_Sequence {
   91   typealias Index : ForwardIndex
   92   subscript(position: Index) -> Element {get}
   93   var startIndex: Index {get}
   94   var endIndex: Index {get}
   95 }
   96 
   97 extension Q_CollectionDefaults {
   98   public final func count() -> Index.Distance {
   99     return distance(startIndex, endIndex)
  100   }
  101   
  102   public final var underestimatedCount: Int {
  103     let n = count().toIntMax()
  104     return n > IntMax(Int.max) ? Int.max : Int(n)
  105   }
  106   
  107   public final func preprocessingPass<R>(body: (Self)->R) -> R? {
  108     return body(self)
  109   }
  110 
  111   /*
  112   typealias Iterator = Q_IndexingIterator<Self>
  113   public final func makeIterator() -> Q_IndexingIterator<Self> {
  114     return Q_IndexingIterator(pos: self.startIndex, elements: self)
  115   }
  116   */
  117 }
  118 
  119 public struct Q_IndexingIterator<C: Q_CollectionDefaults> : IteratorProtocol {
  120   public typealias Element = C.Element
  121   var pos: C.Index
  122   let elements: C
  123   
  124   public mutating func next() -> Element? {
  125     if pos == elements.endIndex {
  126       return nil
  127     }
  128     let ret = elements[pos]
  129     pos += 1
  130     return ret
  131   }
  132 }
  133 
  134 public protocol Q_Collection : Q_CollectionDefaults {
  135   func count() -> Index.Distance
  136   subscript(position: Index) -> Element {get}
  137 }
  138 
  139 extension Array : Q_Collection {
  140   public func copyToContiguousArray() -> ContiguousArray<Element> {
  141     return ContiguousArray(self~>_copyToNativeArrayBuffer())
  142   }
  143 }
  144 
  145 struct Boo : Q_Collection {
  146   let startIndex: Int = 0
  147   let endIndex: Int = 10
  148 
  149   func makeIterator() -> Q_IndexingIterator<Boo> {
  150     return Q_IndexingIterator(pos: self.startIndex, elements: self)
  151   }
  152   
  153   subscript(i: Int) -> String {
  154     return "Boo"
  155   }
  156 }
  157