title: Swift EXC_BAD_INSTRUCTION on call to typealias / typed / defined closure w/o compiler error or warning
body: <p>Given a struct, a class, and typed closure:</p>
  
  <pre><code>struct Vector3d {
      var X:Double
      var Y:Double
      var Z:Double
  }
  
  class Sprite {
  
      var mass: Double = 0.0
  
      init(mass: Double) {
          self.mass = mass
      }
  }
  
  typealias ForceComputation =
      (NSTimeInterval, Sprite) -&gt; Vector3d?
  </code></pre>
  
  <p>The following code crashes with <code>EXC_BAD_INSTRUCTION</code>:</p>
  
  <pre><code>// Construct an instance of the class to call back with
  var ball = Sprite(mass: 3.0)
  
  // Create an instance of closure
  var gravity:ForceComputation = { (projectile:Sprite) -&gt; Vector3d in
      // use this line to close ball so it's available in debugger
      var mass1 = ball.mass
  
      // use mass1 in following line to ensure not optimized out
      // (ignore invalid gravity formula)
      var verticleAcceleration = -9.8 * projectile.mass * mass1
  
      return Vector3d(X:0.0, Y:verticleAcceleration, Z:0.0)
  }
  
  // activate the closure
  gravity(ball)
  </code></pre>
  
  <p>The debugger shows two different values for <code>projectile</code> and <code>ball</code>. The field <code>mass</code> in <code>projectile</code> is invalid. However <code>mass</code> is valid for <code>ball</code> both inside and outside the closure. There are no compiler errors or warnings but <code>EXC_BAD_INSTRUCTION</code> is thrown when <code>projectile.mass</code> is executed?</p>
  
  <p>Despite the misleading debug data, the problem has nothing to do with the parameter to the <code>ForceComputation</code> closure. The problem is the returned struct is defined as optional in the <code>typealias</code>:</p>
  
  <pre><code>typealias ForceComputation =
      (Sprite) -&gt; Vector3d?
  </code></pre>
  
  <p>but constructed with a non-optional return type (note the lack of a <code>?</code> after <code>Vector3d</code>):</p>
  
  <pre><code>// Create an instance of closure
  var gravity:ForceComputation = { (projectile:Sprite) -&gt; Vector3d in
      ....
  }
  </code></pre>
  
  <p>Changing the above code to remove the typing:</p>
  
  <pre><code>// Create an instance of closure
  var gravity = { (projectile:Sprite) -&gt; Vector3d in
      ....
  }
  </code></pre>
  
  <p>will fix the code or ensuring that the return type is optional (note <code>?</code> after <code>Vector3d</code>):</p>
  
  <pre><code>// Create an instance of closure
  var gravity:ForceComputation = { (projectile:Sprite) -&gt; Vector3d? in
      ....
  }
  </code></pre>
  
  <p>will also make this work. My question is if this is a compiler bug and should be reported to Apple or is there a reason the <code>EXC_BAD_INSTRUCTION</code> throwing code should compile?</p>