Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro Expansion Testing only working for Default Feature #23

Open
nkia-christoph opened this issue Jan 14, 2024 · 1 comment
Open

Macro Expansion Testing only working for Default Feature #23

nkia-christoph opened this issue Jan 14, 2024 · 1 comment

Comments

@nkia-christoph
Copy link

nkia-christoph commented Jan 14, 2024

"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

@nkia-christoph nkia-christoph changed the title Macro Expansion Testing Macro Expansion Testing for Default Feature only Jan 14, 2024
@nkia-christoph nkia-christoph changed the title Macro Expansion Testing for Default Feature only Macro Expansion Testing working for Default Feature only Jan 14, 2024
@nkia-christoph nkia-christoph changed the title Macro Expansion Testing working for Default Feature only Macro Expansion Testing only working for Default Feature Jan 14, 2024
@danielhenrymantilla
Copy link
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants