Skip to content

Formal function attributes is still unsupported? #685

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

Closed
Hexilee opened this issue Aug 1, 2019 · 5 comments
Closed

Formal function attributes is still unsupported? #685

Hexilee opened this issue Aug 1, 2019 · 5 comments

Comments

@Hexilee
Copy link

Hexilee commented Aug 1, 2019

As this example repo

This is the example proc-macro:

#![feature(proc_macro_diagnostic)]

extern crate proc_macro;
use proc_macro::{Diagnostic, Level, TokenStream};
use quote::quote;
use syn::{
    parse::{Parse, Parser},
    FnArg, ItemFn, Pat,
};

#[proc_macro_attribute]
pub fn rename_params(_args: TokenStream, input: TokenStream) -> TokenStream {
    let func = <ItemFn as Parse>::parse.parse(input).unwrap();
    for arg in func.sig.inputs.iter() {
        if let FnArg::Typed(pat) = arg {
            if let Pat::Ident(name) = pat.pat.as_ref() {
                Diagnostic::new(
                    Level::Note,
                    format!(
                        "arg({}), PatType.attrs.len = {}, PatIdent.attrs.len = {}",
                        name.ident,
                        pat.attrs.len(),
                        name.attrs.len()
                    ),
                )
                .emit();
            }
        }
    }
    quote!(#func).into()
}

This is the test:

#![feature(param_attrs)]
use params_attribute_example::rename_params;

#[rename_params]
fn hello(a: i32, #[val] b: i32) {}

run cargo expand:

note: arg(a), PatType.attrs.len = 0, PatIdent.attrs.len = 0
note: arg(b), PatType.attrs.len = 0, PatIdent.attrs.len = 0

fn hello(a: i32, b: i32) {}

I cannot get the attributes of parameter. How can I get it?

Cargo.toml:

[dependencies]
proc-macro2-next = { version = "1.0.0-rc1" }
quote-next = { version = "1.0.0-rc1" }
syn-next = { version = "1.0.0-rc1", features = ["full", "visit-mut"] }

[lib]
proc-macro = true

[patch.crates-io]
syn-next = { git = "https://github.com/dtolnay/syn", branch = "master"}
proc-macro2-next = { git = "https://github.com/alexcrichton/proc-macro2", branch = "next" }
quote-next = { git = "https://github.com/dtolnay/quote" }

toolchain: nightly-x86_64-apple-darwin(8a58268b5 2019-07-31)

@dtolnay
Copy link
Owner

dtolnay commented Aug 1, 2019

This should work as of feed12f, but it looks like the compiler is not passing the attrs in to the macro at all. Try putting a println!("{}", input) or println!("{:#?}", input) at the top of rename_params. The invocation #[rename_params] fn hello(a: i32, #[val] b: i32) {} is passed in as fn hello(a: i32, b: i32) { }. Could you follow up by reporting a compiler bug?

@Hexilee
Copy link
Author

Hexilee commented Aug 2, 2019

@dtolnay Yeah, the result of println!("{}", input) is fn hello(a: i32, b: i32) { }.
Where should I reporting the compiler bug to? Just open an issue under the rust-lang/rust repository?

@dtolnay
Copy link
Owner

dtolnay commented Aug 2, 2019

Yes. You should make sure to include a minimal repro that does not use any crates, since you don't need a crate to demonstrate the buggy behavior.

@Hexilee
Copy link
Author

Hexilee commented Aug 2, 2019

Thx, I have reported it rust-lang/rust#63210.

@dtolnay
Copy link
Owner

dtolnay commented Aug 2, 2019

Nice, let's close this issue and continue to track this over there.

@dtolnay dtolnay closed this as completed Aug 2, 2019
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