Swift EXC_BAD_INSTRUCTION 调用 typealias / typed / defined 闭包 w/o 编译器错误或警告

Swift EXC_BAD_INSTRUCTION on call to typealias / typed / defined closure w/o compiler error or warning

给定一个结构,一个 class,并键入闭包:

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) -> Vector3d?

以下代码因 EXC_BAD_INSTRUCTION 而崩溃:

// 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) -> 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)

调试器显示 projectileball 的两个不同值。 projectile 中的字段 mass 无效。但是 mass 对于闭包内部和外部的 ball 都有效。没有编译器错误或警告,但在执行 projectile.mass 时抛出 EXC_BAD_INSTRUCTION

尽管调试数据具有误导性,但问题与 ForceComputation 闭包的参数无关。问题是 returned 结构在 typealias:

中定义为可选
typealias ForceComputation =
    (Sprite) -> Vector3d?

但使用非可选 return 类型构造(注意 Vector3d 后缺少 ?):

// Create an instance of closure
var gravity:ForceComputation = { (projectile:Sprite) -> Vector3d in
    ....
}

更改上面的代码以删除输入:

// Create an instance of closure
var gravity = { (projectile:Sprite) -> Vector3d in
    ....
}

将修复代码或确保 return 类型是可选的(注意 Vector3d 之后的 ?):

// Create an instance of closure
var gravity:ForceComputation = { (projectile:Sprite) -> Vector3d? in
    ....
}

也将使这项工作。我的问题是,这是否是一个编译器错误并且应该报告给 Apple,或者 EXC_BAD_INSTRUCTION 抛出的代码是否应该编译?

这是一个编译器错误(已通过 Twitter here 确认),但是已经有许多关于 returns 和 Swift 的可选性的已知问题。