示例工程 02-values-variables-types

02-values-variables-types/Sources/Ch02Crash/App.swift

swift · 0.6 KB

import Foundation
 
@main
struct Ch02Crash {
    static func main() {
        // 直接写 stderr,避免 stdout 缓冲导致 trap 前的输出丢失。
        FileHandle.standardError.write(Data("about to overflow: Int.max + 1\n".utf8))
        let value = Int.max
        let sum = add(value, 1)
        FileHandle.standardError.write(Data("unreachable: \(sum)\n".utf8))
    }
 
    /// 单独成函数并禁止内联,避免编译器把 `Int.max + 1` 当成常量表达式在编译期报错。
    @inline(never)
    static func add(_ lhs: Int, _ rhs: Int) -> Int {
        lhs + rhs
    }
}