← 返回刚才阅读的位置

知识卡片

问题卡 · 6074.d.1.a.3.c.11.a.1.b · 生成模型:GPT-6

叠加属性宏的名称冲突

理解等级:中级

多个属性宏作用于同一个项时,每个宏生成的额外定义可能进入共同作用域。附录中的 Rocket 例子对 world 同时加路由和捕获属性,调用报出 world 重复定义。展开后能看到两个名为 world 的代理结构体,分别移除某个属性可验证该定义由哪个宏生成。

只供局部使用的生成物可以移入更窄的作用域。必须共享作用域时,生成名可以区分宏种类,仅使用原函数名作为前缀仍会重名。若这一组合本身就无效,宏也可以检查重复或互斥属性,并明确指出冲突。

相关卡片 [[生成代码使用完整路径]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Exercise solutions 第 6 章练习:测试生成物与叠加冲突 · 查看原书依据

      When you have two (attribute) macros and get an error regarding a duplicate definition, your first guess should be that both macros are generating custom things (functions, structs, traits) in too broad a scope, and that these definitions are colliding with each other. You can confirm your guess by expanding the code, which will in this case reveal two structs named world being created. If you remove one macro, one definition disappears. Clearly, each macro is generating a single struct.

    参考来源

    - 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p96) - 结论依据:原书用展开输出及逐个移除属性定位 Rocket 同名类型,后续对比缩小作用域、区分名称与显式拦截。 - 原始内容:When you have two (attribute) macros and get an error regarding a duplicate definition, your first guess should be that both macros are generating custom things (functions, structs, traits) in too broad a scope, and that these definitions are colliding with each other. You can confirm your guess by expanding the code, which will in this case reveal two structs named world being created. If you remove one macro, one definition disappears. Clearly, each macro is generating a single struct.