知识卡片
生成名称与诊断跨度分离
生成辅助项的名称与诊断的来源可以分别决定。附录为默认值检查生成内部结构体。原先直接用字段的 Ident 拼接辅助名,会把字段名的 span 继承到新名字上,辅助项的命名样式警告就可能指向用户写的字段。
练习改为先取字段的名字文本,将首字符大写,再用 format_ident! 生成结构体名。对需要由用户修复的 Default trait 约束,生成器仍用 quote_spanned! 将相关检查定位到字段类型。这让内部命名与输入类型错误各有合适的位置。
相关卡片 [[外部操作错误进入编译诊断]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 8 章练习:辅助属性扩展 · 查看原书依据
This is a minor change. We turn the name in optional_default_asserts into a string, uppercase it, and give it to format_ident. The change also solves another minor bug: the warning was pointing back to the offending field because we were passing its identifier (including its span) to format_ident.
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p129)
- 结论依据:原文明确区分字段标识符对生成名称的 span 继承与字段类型的约束定位,清单给出了分别构造的方法。
- 原始内容:This is a minor change. We turn the name in optional_default_asserts into a string, uppercase it, and give it to format_ident. The change also solves another minor bug: the warning was pointing back to the offending field because we were passing its identifier (including its span) to format_ident.