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

Does this work with custom derives? #2

Closed
thomaseizinger opened this issue May 4, 2023 · 4 comments
Closed

Does this work with custom derives? #2

thomaseizinger opened this issue May 4, 2023 · 4 comments

Comments

@thomaseizinger
Copy link

I am trying to use this with a custom-derive and it doesn't seem to work. Here is a MRE: https://github.com/thomaseizinger/proc-macro-warning-MRE

Any ideas?

@ggwpez
Copy link
Owner

ggwpez commented May 4, 2023

Thanks for investigating and the MRE!
I actually observed the same thing, but did not look closer since it worked in Substrate.

Looks like it is important to pass in a span - otherwise it wont trigger (was not actively aware of that).
Your MRE can be fixed by putting the span of the parsed input:

#[proc_macro_derive(MyDerive)]
pub fn my_derive(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as syn::DeriveInput);

    let warning = proc_macro_warning::Warning::new_deprecated("test")
        .old("foo").new("bar")
        .span(input.span()) // <- here
        .build();

    quote::quote!{ #warning }.into()
}

produces:
image

Please let me know if this fixes your issue. I will add an example specifically for derive macros and maybe make the .span call mandatory.

@thomaseizinger
Copy link
Author

@tcoratger Mind trying this on libp2p/rust-libp2p#3848?

@thomaseizinger
Copy link
Author

Thanks for investigating and the MRE!
I actually observed the same thing, but did not look closer since it worked in Substrate.

Looks like it is important to pass in a span - otherwise it wont trigger (was not actively aware of that).
Your MRE can be fixed by putting the span of the parsed input:

#[proc_macro_derive(MyDerive)]
pub fn my_derive(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as syn::DeriveInput);

    let warning = proc_macro_warning::Warning::new_deprecated("test")
        .old("foo").new("bar")
        .span(input.span()) // <- here
        .build();

    quote::quote!{ #warning }.into()
}

produces:
image

Please let me know if this fixes your issue. I will add an example specifically for derive macros and maybe make the .span call mandatory.

Awesome, thank you! I hope this works :)

@thomaseizinger
Copy link
Author

Thanks! This now works.

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