I'm using a procedural macro attribute which analyzes the contents of the module it's declared on.
As far as i understood from The Unstable Book; the compiler removes the actual tokens of the module and passes them into the macro function call.
My macro analyses the module contents and builds an enumeration with variants matching each struct identifier. The macro also implements a custom FromType<X> trait for the generated enum for each struct. This is similar to the std-prelude From<X> and allows me to produce runtime-equivalent values when (abusing?) generic arguments.
So far my implementation works, except i cannot access the generated module (and contents) within the same crate (= crate X)! This makes it impossible to write unit-tests for the macro within the same crate, but there seems to be no issue accessing the auto-generated content from crates depending on crate X.
Can anyone point me towards a solution?
My code is located here: https://github.com/Bert-Proesmans/medici
The crate of the offending macro is value_from_type_macros (located: '/value_from_type_derive/value_from_type_macros') and can be called like the following snippet:
// Attribute macro must be imported through a use statement
use value_from_type_macros::value_from_type;
// The module is made public by the proc macro
mod default {
// This macro will build enum EnumerationTiming into the this module.
#![value_from_type(EnumerationTiming)]
#[derive(Debug)]
pub struct Pre();
impl Timing for Pre {}
[...]
}
Calling the macro attribute returns the module named default back, which now contains all original defined code + the enum EnumerationTiming and corresponding implementations.
The macro is called twice within the repo:
-
file: /medici_derive/medici_traits/src/timing_traits.rs
This file also contains a test methoddefault_timing_into()that tests the presence of the auto-generated module contents -> This test fails.
The top-level crate also contains a test methodvalue_from_type()for the same purpose -> This test succeeds. -
file: /src/hs_automaton/states/action_states.rs
The code in the top-level crate makes use of the auto-generated code, but compiling this crate fails due to unresolved imports.
Example error code when trying to importing auto-generated code:
error[E0432]: unresolved import `self::custom::EnumerationTrigger`
--> src\hs_automaton\states\action_states.rs:4:33
|
4 | pub use self::custom::{EndTurn, EnumerationTrigger};
| ^^^^^^^^^^^^^^^^^^ no `EnumerationTrigger` in `hs_automaton::states::action_states::custom`. Did you mean to use `EnumerationTrigger`?
Of course, when i follow the compiler advice it still throws an error.
Running cargo build or cargo test --all on the top-level crate will reveal the same error.
This looks to me that the use statement refers to the original module, which is removed by the compiler and re-inserted by the macro attribute. It seems i can't explicitly refer to the re-inserted code.
So far i tried using different combinations of Spans for inserting the module again to no avail. I'm out of ideas to fix this..