Hi, I was experimenting with new proc macros recently and wondered if it's possible to reexport them. Consider the following scenario:
Let's say there's a crate called definition containing a foo
macro:
#![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::TokenStream;
#[proc_macro]
pub fn foo(_input: TokenStream) -> TokenStream { unimplemented!() }
I've created a reexport crate:
#![feature(proc_macro)]
extern crate definition;
pub use definition::foo;
Now, when I try to use the reexported macro:
#[feature(proc_macro)]
// #[macro_use]
extern crate reexport;
use reexport::foo;
fn main() {
foo!()
}
It fails with unresolved import:
error[E0432]: unresolved import `reexport::foo`
--> src/main.rs:6:5
|
6 | use reexport::foo;
| ^^^^^^^^^^^^^ no `foo` in the root
I've created a repository with such a setup, if anybody wants to experiment.
Now, the fun part is that if I comment out the use
line and uncomment the macro_use
instead, the reexported macro runs, ie. the message proc macro paniced is printed. But if I change the macro not to panic, I get the following error:
error: procedural macros cannot be imported with `#[macro_use]`
--> src/main.rs:9:5
|
9 | foo!()
| ^^^
help: instead, import the procedural macro like any other item
|
8 | use definition::foo;
|
Which is exactly what I was trying in the first place!
So, is it actually possible to reexport a procedural macro?
In the case I've fallen into some XY-problem trap: My original problem was "how to export both macro_rules
and a proc-macro macros in the same crate" and I've tried to put the proc macro in separate definition
crate (to sidestep the limitation that proc-macro = true
crate can't contain macro_rules
macros).