I am writing a Rust implementation of Lua, and I came to a point where I had a bunch of function definitions which were more or less the same thing over and over again. I decided to try my luck at writing a macro to do the work for me:
#![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);
}
When I do this
lua_func!(toboolean, bool, idx, int);
I expect it to expand to
pub fn lua_toboolean (L: *mut lua_State, idx: int) -> bool;
but running rustc --pretty expanded
to test it empties my computer's RAM in a few seconds if I don't immediately do a ^C
.
I say that this macro is the cause of the problem because when I removed the usage of the macro, the command ran successfully and outputted the text with all macros expanded.
What am I doing wrong?
rustc --version --verbose
output:
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
With this version, the command rustc --pretty expanded -Z unstable-options
with the MCVE file reproduces the problem on my computer.