rust-lang / rust-lang/rust

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

Open
#134,882 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-hygiene A-proc-macros C-bug T-compiler T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked Rust Playground reproduction and compare the two paths in my-macros/lib.rs, including the reset_hygiene branch, with the macro_rules! invocation in my-code/lib.rs. Trace how the attribute macro's generated function parameter and the dbg!(arg) reference receive hygiene information. Done means the reproduction compiles with the hygiene taken from my_attr in the m! invocation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.