How to get fully qualified path to a type with syntex_syntax?

syn is the modern alternative to syntex. It also compiles a lot faster.

TokenStream implements FromStr, meaning that if you have a String or str you can write string.parse(). That can be given tosyn::parse, so you can write syn::parse(string.parse().unwrap()) to parse anything which implements Synom.


But as for your main problem... you will still have difficulty. The primary uses of syn are:

  • Parsing Rust code
  • Performing lexical transforms
  • (often with the help of quote) Emitting rust code

Its primary use case is in procedural macros, which are usually only given a small bit of input (not an entire library's source code!), and even then are something that run before perfect path resolution is even possible (macro calls in the input need to be expanded before the items they generate can be known, for instance). So I do not believe it provides facilities for path resolution.

You could write something that performs your own path resolution, by making one or more passes with e.g. a Folder that inlines mod foo; files and a Visitor that builds namespace tables. But this seems immensely difficult. Depending on what the source crate contains you would need to reimplement much of the compiler!

(Of course, there are things you could do in the source crate to make this more feasible, such as restricting yourself to absolute use paths, making sure not to re-export a re-export (e.g. use a::b; where a has use c::b), and not using macros that generate items. Then you could perhaps rely on local use statements.)

But I am not sure what to truly recommend as a solution, honestly.

2 Likes