Since proc-macro has now been stabilized, I am starting to experiment with my very first proc-macro:
#[proc_macro]
pub fn echo(input: TokenStream) -> TokenStream {
input
}
I can get this to compile fine with proc-macro = true
in my Cargo.toml
. I import echo
successfully into another crate, and use the new use
powers to bring the macro into scope. Then I run into trouble when I try to use it:
#[cfg(test)]
mod tests {
use super::{echo};
#[test]
fn test_echo_expression() {
assert_eq!(&echo!(r"Hello world"), r"Hello world");
}
#[test]
fn test_echo_statement() {
echo!(let x = r"Hello world");
assert_eq!(&x, r"Hello world");
}
}
When I compile this I get the following errors:
error[E0658]: procedural macros cannot be expanded to expressions (see issue #38356)
--> src/lib.rs:107:21
|
107 | assert_eq!(&echo!(r"Hello world"), r"Hello world");
| ^^^^^^^^^^^^^^^^^^^^^
error[E0658]: procedural macros cannot be expanded to statements (see issue #38356)
--> src/lib.rs:111:9
|
111 | echo!(let x = r"Hello world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: expected one of `.`, `;`, `?`, or an operator, found `<eof>`
--> src/lib.rs:111:9
|
111 | echo!(let x = r"Hello world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This makes no sense to me. The documentation in the proc-macro crate clearly states that function-like proc-macros can be expended to expressions or statements (or items). What am I doing wrong? Or is proc-macro just not quite ready for actual use?