06 — 不让错误进入成品
遗漏分支会导致编译错误,忽略失败也是。
我最常见到的智能体修改错误是:给类型增加一个变体,却漏掉某个必须处理它的位置。
type Shape =
| Circle(Float)
| Rect(Float, Float)
| Triangle(Float, Float) // added
fn area(s: Shape) -> Float = match s {
Circle(r) => 3.14159 * r * r,
Rect(w, h) => w * h,
} // not updated
error[E010]: non-exhaustive match: missing Triangle(_, _)
--> shape.almd:6:36
hint: add arms for Triangle(_, _):
Triangle(arg1, arg2) => _
失败也是如此。可能失败的调用会返回 Result,你必须说明如何处理它。直接丢弃它是错误,不是警告。
effect fn load(path: String) -> String = {
let text = fs.read_text(path) // can fail, and nothing says so
text
}
error[E041]: implicit propagation of this binding's value was removed —
this value is a Result
--> load.almd:4:14
hint: Write `expr!` to propagate, or consume the Result as a value
(`??`, `?`, match ok/err).
0.69.0 版本的编译器输出,已节选。
加一个字符就能修复:fs.read_text(path)!。关键在于,作者必须作出决定,而且这个决定在源码中清晰可见。