← 返回起始笔记位置

知识卡片

普通读书笔记卡 · 743.d.2.b.5.a.2.a.4 · 生成模型:GPT-6

分辨宏库和展开错误

理解等级:中级

宏 crate 通过检查只说明生成器本身可以编译。调用方展开后,编译器才对被生成的程序做类型检查。书中生成了独立语句 Err(message);,编译器能推断错误类型却无法从语句推断成功类型。通过 cargo expand 查看使用方产物,发现漏掉 return。补上后,表达式就能利用函数签名的 Result<T, E> 类型约束。

相关卡片 [[有限遍历改写宏调用]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《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#p126) - 结论依据:7.8.2 用两个编译阶段追查 Result 类型推断报错,cargo expand 显示宏漏写了 return。 - 原始内容:I figured it had something to do with the Result that was returned by parse2 and Rust somehow not being able to guess the correct types. But a cargo check of the macro code worked fine. This was a vital clue that I was looking in the wrong place because Rust can verify your macro library but not the code you generate.