Programmatically determining whether a given type implements a given trait

I'd like to write a program that accepts a Rust codebase as an input and enumerates every function within that codebase that has at least one parameter that is Arbitrary. This will obviously require full semantic information at runtime. Because of this, I assume that I'll need some type(s) from rustc itself? Any general advice on how to approach this would, of course, be greatly appreciated (as well as any corrections to anything that I've posted thus far). My current thinking is as follows:

  • Find some type that represents a Rust type (with full semantic information; so nothing from syn unfortunately)
  • For each function in the target codebase and for each of its parameters,
  • Somehow determine whether an instance of this type adheres to the Arbitrary trait
  • If so, add the function that it belongs to to the set of candidate functions

One major risk that I see is the lack of stability of types within the compiler itself? This seems resolvable by pinning to a given version of Nightly but it's still not great obviously.

Edit: In hindsight, I realised that this post doesn't actually ask a clear question. Essentially, which type(s) from the Rust compiler code are relevant here?

I'm not an expert on the compiler, I wonder maybe a custom linter can do (most of) the job? I might be wrong, but I believe linter passes have access to type information, not just syntax tokens like macros.

I found this blog post some time ago, but I have not written a linter myself, I can still point you there though, and hopefully you can make it out yourself.

2 Likes

Obtaining Rustdoc JSON might be a better approach, because the point of rustdoc is precisely to tell you about all the functions (including, if you ask, private ones) in a crate that could be called and what their signatures are.

5 Likes

Do note that the rustdoc json format is unstable so you will have to use nightly and adapt to changes in the format as needed. It may be a good idea to abstract away your internal representation from the format, i.e. convert from the json structures to your own format as early as possible.

I know SemverChecks does that kind of queries based on the Trustfall query engine, but I'm not sure where the information comes from.

Cargo-semver-checks uses rustdoc as its data source. And it uses a query engine to be able to abstract away version differences in the rustdoc format. (If that part is published as a separate crate thst could be reused by others that would be nice. I don't know that it is.)

EDIT: trustfall_rustdoc

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.