知识卡片
叠加属性宏的名称冲突
多个属性宏作用于同一个项时,每个宏生成的额外定义可能进入共同作用域。附录中的 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.