Check that a parameter is a typespec inside a macro?

As the title says: is it possible to verify that a parameter to a macro is a typespec? As I understand it, a macro receives the ASTs as parameters and Elixir does not know anything about typespecs they only have meaning to Dialyzer. In my mental model I guess that Dialyzer analyses the AST directly. Is this a correct understanding?

Typespecs are only understood by a handful of module attributes defined here, so these attriutes are working purely in the context of typespecs. For example, you can’t call String.t() anywhere else. Module attributes are just using the @/1 macro so that is what you would be looking for in AST. I can’t speak exactly to how Dialyzer is implemented but that if you are looking for specs for your own purposes then you’d be looking for {:@, _, [{:spec, _, [...]} etc while walking the AST.

1 Like