我正在编写 Lua 的 Rust 实现,我遇到了这样的情况:我有一堆函数定义,它们一遍又一遍地或多或少是相同的东西。我决定尝试编写一个宏来为我完成这项工作:
#![feature(macros_in_extern)]
#![feature(concat_idents)]
macro_rules! lua_func {
($name: ident, $ret: ty, $var: ident, $type: ty) => {
let fn_name = concat_idents!(lua_, $name);
pub fn fn_name (L: *mut lua_State, $var: $type) -> $ret
};
}
extern "C" {
lua_func!(toboolean, bool, idx, int);
}
当我这样做时
lua_func!(toboolean, bool, idx, int);
我预计它会扩展到
pub fn lua_toboolean (L: *mut lua_State, idx: int) -> bool;
但是运行rustc --pretty Expanded
来测试它,如果我不立即执行^C
,它会在几秒钟内清空我的计算机的RAM。
我说这个宏是问题的原因,因为当我删除宏的使用时,命令成功运行并输出所有宏都展开的文本。
我做错了什么?
rustc --version --verbose
输出:
rustc 1.30.0-nightly (3bc2ca7e4 2018-09-20)
binary: rustc
commit-hash: 3bc2ca7e4f8507f82a4c357ee19300166bcd8099
commit-date: 2018-09-20
host: x86_64-unknown-linux-gnu
release: 1.30.0-nightly
LLVM version: 8.0
在此版本中,使用 MCVE 文件的命令 rustc --pretty Expanded -Z不稳定选项
在我的计算机上重现了该问题。
最佳答案
这是一个编译器错误:rust-lang/rust#54441 。从 Rust 1.31 以及 1.31.0-nightly 4efdc04a5 2018-10-06 更新的 nightlies 开始,该问题已得到修复。
关于macros - rustc --pretty Expanded 在解析外部 block 内的宏扩展时使用我的所有 RAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52447164/