# Macro generated export

I was writing a utility macro to easily reexport from inside a crate. The problem is the macro seems to not be generated at the same time as the module, as the error `error[E0432]: unresolved import \$importname` appears constantly (\$importname is just whatever i tried to import).

The macro is:

``````macro_rules! reexport{
{\$(mod \$name:ident;)+} => {
\$(
mod \$name;
pub use self::\$name::*;
)+
};

{\$(mod \$name:ident : \$gate:expr;)+} => {
\$(
#[cfg(feature = \$gate)]
mod \$name;
#[cfg(feature = \$gate)]
pub use self::\$name::*;
)+
};
}
``````

And is used like this:

``````reexport!{
mod module1 : "feature";
mod module2;
}
``````

When trying to use something that is reexported with this macro, the error [E0432] appears.

Is there something wrong with the macro or simply the macros are expanded after the modules??
Is there any way to get the desired behaviour?

This seems to work fine with a simple example: Playground.

However, the macro you posted is not compatible with this example you gave:

``````// doesn't work
reexport!{
mod module1 : "feature";
mod module2;
}
``````

Your macro doesn't accept a mix of feature-gated and non-feature-gated modules. It works only if all of the modules have features or none of them do:

``````// works
reexport!{
mod module1 : "feature1";
mod module2 : "feature2";
}

// also works
reexport!{
mod module1;
mod module2;
}
``````

Using both styles in a single call causes this error, which might be what you are seeing (Playground):

``````error: no rules expected the token `;`
--> src/main.rs:21:16
|
1  | macro_rules! reexport{
| --------------------- when calling this macro
...
21 |     mod module2;
|                ^ no rules expected this token in macro call

error[E0432]: unresolved import `module1`
--> src/main.rs:24:5
|
24 | use module1::bar;
|     ^^^^^^^ use of undeclared type or module `module1`

error: aborting due to 2 previous errors
``````

Yes it was mixing both that generated the error. Thanks

One way to make this macro work with mixed input is to rewrite it as a recursive "tt muncher". There may be other solutions too, but I'm not an expert on macro techniques.

1 Like