title: Swift compiler segmentation fault with generic recursive function
body: <p>I've run into an interesting issue with the Swift compiler, which seems to be caused by a very simple generic function. I've got a workaround, but I'd really like to understand what the underlying issue is.</p>
  
  <p>In my app I have a requirement to fade in some UICollectionViewCells in a given order, sequentially, with a slight overlap between the animations. 
  I've implemented this using the methods below. The <code>revealCells</code> method takes an array of cells. If the collection is empty, it simply returns. Otherwise it animates the fade-in on the first cell in the array, and then waits a certain amount of time before making a recursive call to itself, passing all the cells except the one it just animated.</p>
  
  <p>Code below:</p>
  
  <pre><code>func revealCells(cells:[UICollectionViewCell],animationTime:NSTimeInterval,overlap:Double) {
    if cells.count &gt; 0 {
      let firstCell = cells.first
        UIView.animateWithDuration(0.3, animations: { () -&gt; Void in
          firstCell?.alpha = 1.0
          let timeMinusOverlap = animationTime - overlap
          let delayTime = dispatch_time(DISPATCH_TIME_NOW,Int64(timeMinusOverlap * Double(NSEC_PER_SEC)))
          dispatch_after(delayTime, dispatch_get_main_queue(), { () -&gt; Void in
            self.revealCells(self.cdr(cells),animationTime: animationTime,overlap: overlap)
          })
        })
      }
    }
  }
  
  //Returns the collection passed into the function with its first element removed
  //i.e the traditional LISP cdr function
  
  func cdr&lt;T&gt;(input:[T]) -&gt; [T] {
    var rVal = [T]()
    if input.count == 1 {
      rVal.append(input.first!)
    } else {
      rVal = [T](input[1...input.count-1])
    }
    return rVal
  }
  </code></pre>
  
  <p>All this works fine in the simulator. But when I try to archive the build, the swift compiler crashes with the message <code>Swift Compiler Error Command failed due to signal: Segmentation fault 11</code>. My setup is Xcode 6.3.1 (iOSSDK 8.3), and my min deployment target is 8.3.</p>
  
  <p>Fixing the problem is trivial - if I just replace the code inside the <code>dispatch_after</code> with:</p>
  
  <pre><code> let newCells = [UICollectionViewCell](cells[1...cells.count-1])
   self.revealCells(newCells,animationTime: animationTime,overlap: overlap)
  </code></pre>
  
  <p>the problem goes away. So it seems to be something to do with the generic function, or possibly something block related.</p>
  
  <p>Stack trace is:</p>
  
  <pre><code>0  swift                    0x000000010105ba18     llvm::sys::PrintStackTrace(__sFILE*) + 40
  1  swift                    0x000000010105bef4 SignalHandler(int) + 452
  2  libsystem_platform.dylib 0x00007fff8725ef1a _sigtramp + 26
  3  libsystem_platform.dylib 0x000000000000000f _sigtramp + 2027557135
  4  swift                    0x00000001021a98cb llvm::AsmPrinter::EmitFunctionBody() + 4379
  5  swift                    0x000000010116c84c llvm::ARMAsmPrinter::runOnMachineFunction(llvm::MachineFunction&amp;) + 220
  6  swift                    0x0000000100d81d13 llvm::MachineFunctionPass::runOnFunction(llvm::Function&amp;) + 99
  7  swift                    0x00000001024d5edf llvm::FPPassManager::runOnFunction(llvm::Function&amp;) + 495
  8  swift                    0x00000001024d60cb llvm::FPPassManager::runOnModule(llvm::Module&amp;) + 43
  9  swift                    0x00000001024d658f llvm::legacy::PassManagerImpl::run(llvm::Module&amp;) + 975
  10 swift                    0x0000000100a09b41 performIRGeneration(swift::IRGenOptions&amp;, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&amp;, swift::SourceFile*, unsigned int) + 4369
  11 swift                    0x0000000100a09cb3 swift::performIRGeneration(swift::IRGenOptions&amp;, swift::SourceFile&amp;, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&amp;, unsigned int) + 51
  12 swift                    0x0000000100945687 frontend_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) + 6647
  13 swift                    0x0000000100943ae6 main + 1814
  14 libdyld.dylib            0x00007fff8a46b5c9 start + 1
  </code></pre>
  
  <p>(a long list of program arguments, which are mostly the names of file being compiled)</p>
  
  <pre><code>1.  Running pass 'Function Pass Manager' on module    '/Users/tolleyr/Library/Developer/Xcode/DerivedData/ParseTestQuiz-fgnfjkxxlyqfnrfrfntgtsjnrcfv/Build/Intermediates/ArchiveIntermediates/ParseTestQuiz/IntermediateBuildFilesPath/ParseTestQuiz.build/Release-iphoneos/ParseTestQuiz.build/Objects-normal/armv7/QuizQuestionViewController.o'.
  2.  Running pass 'ARM Assembly / Object Emitter' on function '@_TFC13ParseTestQuiz26QuizQuestionViewController13viewDidAppearfS0_FSbT_'
  </code></pre>
  
  <p>(the last part enabled me to figure out what code was causing the problem). The command being run was <code>CompileSwift normal armv7</code></p>
  
  <p>I'm going to file a radar for this, since the compiler itself is crashing , but thought I'd post it here in case anyone has an idea of what might be going on, or has run into the same issue.</p>