Method signatures and args programmatic analysis

Hello Rustaceans!

I have the following task in my project:

GIVEN: Rust library source code, rust application, analyze_lib_api($PATH_TO_PROJECT_WITH_LIB_RS) functionality
WHEN: analyze_lib_api(...) is called
THEN: List of exposed methods is returned, including their names, return types, arguments (with recursive field descriptions if they are structs, fully qualified types, with recursive args/return descriptions if they are lambdas and so on).

I tried to read and experiment a bit with rust compiler, using its code, but I'm a bit overwhelmed by its complexity and API.

As far as I understand, HIR might be the thing I need, but I cannot figure out how I could build only HIR and skip the rest of the pipeline (HIR building process seems to be tightly integrated with the rest of compilation, which I don't need)

Could someone, please, guide me a bit? I would be happy to get some promising direction to dig into. May be there are some libraries, which could be embedded and provide this sort of functionality? Or may be some components of RLS would be easier to reuse? Or may be there is a relatively simple way to call some set of methods in the compiler internal API, which I missed?

Information, returned by this API should be quite strict, as it will be used to create library bindings of specific custom format automatically (surely with long list of limitations and API design agreements).

Thank you in advance!

Maybe rust-analyzer? It is split into several crates, hir_ty might be of interest.

1 Like

Note that rust-analyzer crates are available on, under the names ra_ap_*** (like ra_ap_hir_ty)

1 Like

Thank you for your answers!

I investigated this particular crate you recommended, hir_ty (ra_ap_hir_ty). It definitely provides very useful structs/types/methods, but I still can't figure out how to use it unfortunately.

The main issue I have at the moment is requirement to pass either HirDatabase or something like GlobalContext (if I remember correctly the latter one) to these methods. These arguments, I suppose, come from somewhere else (parser? embedded compiler?) but everything I have as input is just path to (or the lib proj folder).

I will try to continue digging deeper, may be I'm missing something in the lib. May be will try to contact the lib author for deeper insight.

Ah, I finally found the discussion I was looking for ! And as of the version 0.0.35 of the ra_ap_*** crates, it seems to be somewhat up-to-date:

So you need an AnalysisHost (or, if you have one file without dependencies, maybe Analysis is enough ?) from the ra_ap_ide crate.
One you manage to build it, the underlying RootDatabase implements the HirDatabase trait :slightly_smiling_face:

1 Like

Thank you! That looks like a solution! Will try it after work and close the topic as solved if works :slightly_smiling_face:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.