Get a function return type within a proc_macro?

Hi there,

I'm currently having a hard time writing a complex proc_macro.
I'm actually writing a macro that allows annotating a function and than tries to apply some magic to it and restructures it to support async/await like style in my embedded/no_std OS. However, to reach the next level of this implementation I would need to get the return type functions that are called.
An example:

fn foo() {
    let bar = baz();

So I'd like to retrieve the return type of function baz within my proc_macro. The most functions I've used so far are all with respect the AST (like the Visit trait that is really helpful. But I could only see in the AST the stuff that has been parsed from the code and really was written into the function.

So any hint how I could request the return type of this function (which might belong to a different crate) would be awesome and much appreciated :wink:

As far as I have understood this, a proc macro never sees anything outside of its scope. so unless you enclose all of the things that the macro will use within its scope, it will have no knowledge of those things. You would need to define baz within the scope of the macro to use it... something like:

asynchronize! {
    fn baz() -> something { ... }

    fn foo() {
        let bar = baz();

so here, asynchronize would be able to determine the return type of baz with some AST processing, and then will be able to use that knowledge to do something in foo.


I didn't see the last of your question...

One tedious solution would be to define a DSL within which you'd provide the signatures of the functions you want to use...

The reason it works this way is that proc macros run long before the compiler starts doing type inference.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.