danielhenrymantilla/macro_rules_attribute-rs

Macro Expansion Testing only working for Default Feature

Opened this issue · 1 comments

"verbose-expansions" during testing works only if the macros were used in a default feature.

So, I'm using macro_rules_attribute::apply in a feature of a small library I'm working on and I came across this issue.

Even though I enable your feature "verbose-expansions" in the dev-deps I only get the output during cargo test if I declare the feature using your crate a default feature in Cargo.toml. It's not supposed to be a default feature though. Could you have a look at this?

std structure:

Cargo.toml
src/
    lib.rs
    panic.rs

Cargo.toml

...

[features]
default = ["send", "panic"] # panic dep needed for testing
send = ["dep:tokio"]
panic = ["send", "dep:tokio", "dep:macro_rules_attribute"]

[dependencies]
macro_rules_attribute = { version = "0.2", optional = true }
tokio = { version = "1", features = ["rt"], optional = true }

[dev-dependencies]
macro_rules_attribute = { version = "0.2", features = ["verbose-expansions"], optional = false }

...

lib.rs

pub mod panic;

// rest

panic.rs

#![cfg(feature = "panic")]

extern crate macro_rules_attribute;
pub use macro_rules_attribute::apply;

#[macro_export]
macro_rules! log_and_display {
    (
        $( #[$attrs:meta] )*
        $pub:vis $( $async:ident )?
        fn main $( < $($gen:tt),* > )? ( $($arg:tt)* ) $( -> $ret:ty )? $body:block
    ) => (
        // do macro stuff
    );
    (
        $( #[$attrs:meta] )*
        $pub:vis $( $async:ident )?
        fn $NAME:ident $( < $($gen:tt),* > )? ( $($arg:tt)* ) $( -> $ret:ty )? $body:block
    ) => (compile_error!("function needs to be main!"));
}

// successful-ish test
#[test]
fn tokio_log_and_display() {

    #[apply(log_and_display)]
    async fn main() {
        println!("success!");
    }

    main();
}

output on cargo test

this_macro_is_private!
{
    macro_rules! ඞ_nested_derive
    {
        (#[derive($($Derives : tt) *)] $($rest : tt) *) =>
        (#[$crate :: derive($($Derives) *)]
        #[$crate :: apply($crate :: ඞ_dalek_EXTERMINATE!)] $($rest) *) ;
    }
}
this_macro_is_private!
{ macro_rules! ඞ_dalek_EXTERMINATE { ($it : item) => () } }
   Compiling pop-launcher-plugin-helper v0.1.0 (/media/data/projects/pop-launcher-plugin-helper-rs)

ONLY IF I MAKE "panic" A DEFAULT FEATURE

log_and_display!
{
    #[tokio :: main(flavor = "current_thread")] async fn main()
    { println! ("success!") ; }
}

Rest is the same

    Finished test [unoptimized + debuginfo] target(s) in 2.94s
     Running unittests src/lib.rs (target/debug/deps/helper-57dd50e161b347ff)

... normal rust test output ...

  • no compiler warnings
  • no clippy warnings
  • crate version: "verbose-expansions"
  • rust version: 1.75.0
  • cargo version 1.75.0

Things I've tried without success

  • made "verbose-expansions" non-optional / required

KR
Christoph

Hey, sorry about the delay in my reply, I missed this notification 😅

I think your issue stems from the #![cfg(feature = "panic")] on the test file whence the debugged macros are being invoked.

You could achieve the same with cargo test --features panic (non-default feature, but enabled for the file to be compiled).