知识卡片
宏诊断的中止与继续
书中使用的 proc_macro_error 1.0.4 要求在宏入口应用相应属性。其 abort! 立即停止处理,emit_error! 记录编译诊断并继续检查。能独立处理其他部位时,继续扫描可以合并反馈。书中的空 panic 和返回类型错误分别保留原语句与原签名作为后续处理的占位,避免污染剩余检查。定位明确时附上输入跨度,还可为诊断提供 help 和 note 等上下文。与 syn::Error 的显式 Result 流相比,这种写法用额外依赖换取较少样板。
相关卡片 [[组合独立的编译诊断]]、[[用跨度指向宏输入错误]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- From panic to result: Error handling proc_macro_error的诊断控制 · 查看原书依据
The change in handle_panic_to_result is not too complex. We now use emit_error for both of our error cases, and we have—again—removed every Result wrapper. More interesting is that we are still returning something after emitting an error. That is because we want to go on for as long as possible, emitting errors—but continuing with parsing—until we’ve done everything we could. And because our code will not stop on the first error, we want to give back something sensible.