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

attribute macro's call_site() hygiene should come from the macro's name, not the #[] #134882

Open
programmerjake opened this issue Dec 29, 2024 · 0 comments
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-hygiene Area: Macro hygiene A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@programmerjake
Copy link
Member

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ade138e8d44ac15e91fb351253675a23

my-macros/lib.rs:

use proc_macro2::TokenStream;
use quote::ToTokens;

#[proc_macro_attribute]
pub fn my_attr(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    match main(attr.into(), item.into()) {
        Ok(retval) => retval.into(),
        Err(err) => err.into_compile_error().into(),
    }
}

fn main(_attr: TokenStream, item: TokenStream) -> syn::Result<TokenStream> {
    let mut item: syn::ItemFn = syn::parse2(item)?;
    item.sig = syn::parse_quote! {
        fn my_test_fn(arg: u8)
    };
    if cfg!(feature = "reset_hygiene") {
        Ok(syn::parse_str(&item.to_token_stream().to_string())?)
    } else {
        Ok(item.to_token_stream())
    }
}

my-code/lib.rs:

macro_rules! m {
    (
        $(#[$meta:meta])*
        $vis:vis fn $($rest:tt)*
    ) => {
        $(#[$meta])*
        $vis fn $($rest)*
    };
}

m! {
    #[my_macros::my_attr]
    pub fn my_test_fn() {
        dbg!(arg);
    }
}

I expected to see this happen: compile without error because the attribute macro adds the arg argument to the function definition and the hygiene used is from my_attr in the invocation of m!

Instead, this happened: got an error:

error[E0425]: cannot find value `arg` in this scope
  --> src/lib.rs:15:14
   |
15 |         dbg!(arg);
   |              ^^^ not found in this scope

Meta

rustc version 1.83.0 on playground

also has same issue on 1.85.0-nightly (2024-12-28 8742e0556dee3c64f714) on playground

@programmerjake programmerjake added the C-bug Category: This is a bug. label Dec 29, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 29, 2024
@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-hygiene Area: Macro hygiene A-attributes Area: Attributes (`#[…]`, `#![…]`) T-libs Relevant to the library team, which will review and decide on the PR/issue. A-proc-macros Area: Procedural macros labels Dec 29, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-hygiene Area: Macro hygiene A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants