知识卡片
Rocket组合跨度诊断
Rocket 路由错误处理器的宏会验证签名。书中的历史实现用 proc_macro2_diagnostics 的跨度扩展方法在参数括号处创建错误,再附上允许的参数形式。验证返回 Diagnostic 后,宏边界将其变成可编译阶段消费的输出令牌。这样的接口允许在生成逻辑早期记录定位和修复上下文,出口统一完成诊断转换。
相关卡片 [[用跨度指向宏输入错误]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- From panic to result: Error handling 诊断在真实宏中的用法 · 查看原书依据
Rocket, meanwhile, uses Diagnostics from proc_macro2_diagnostics, which has some neat extension traits for proc_macro2. They add an error and help to a span when checking a signature:
参考来源
- 位置:《Write Powerful Rust Macros》第07章《From panic to result: Error handling》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p193)
- 结论依据:7.10 展示 Rocket 为捕获函数参数数量错误绑定括号跨度,再把带帮助信息的 Diagnostic 变成条目令牌。
- 原始内容:Rocket, meanwhile, uses Diagnostics from proc_macro2_diagnostics, which has some neat extension traits for proc_macro2. They add an error and help to a span when checking a signature: