How to retrieve the function signature of a function name passed into a procedural macro?

Is it possible to get the function signature of a attr param that is passed into a procedural macro?

Let's imagine that I have an function "foo" fn foo(u32) -> u64.

If I passed in "foo" to the procedural macro attr as shown below, is there a way for me to somehow determine that it represents a function with the signature fn(u32) -> f64?

#[proc_macro_attribute] 
pub fn macro(attr: TokenStream, item: TokenStream) -> TokenStream {}

I would appreciate the help, as I am trying to use this to be able to downcast a reference of Box<Any> to its original function signature.

Thanks.

You're not showing exactly how you intend to use your macro, but I think this is the information you want:

Macro expansion is done before type information is available. You don't have more information to work with than if you were using macro_rules!. What's different about procedural macros is that you can write the transformation with code instead of with the macro language. This all means that if you want to use the type information in your macro, you'll need to pass it in directly.

1 Like

Thank you for the answer. This would have made an API I am working on much easier, but I will have to work around it.

The syn crate should be able to help you out. It parses a token stream into inspectable rust structures. The annotated function should be parsed as an Item from which you can inspect the ItemKind which will let you unwrap the FnDecl which provides the list of of FnArg inputs and the FunctionRetTy output. To check a type, you can use e.g. syn::parse_type(u64) which will give you a Ty instance that you can use to check equality.

1 Like

I'll give this a try later when I get back from work, thanks.

Please open a new thread.