But I'm unable to find an explanation or example of how to access the rustc_interface crate. It doesn't seem to be published anywhere. What is the recommended way for an external tool to use rustc_interface?
I think you just need to use nightly and add extern crate rustc_interface, and you may need to add some feature gate or special attribute...
Basically, rustc_interface (and all of the rustc_*) crates are in the sysroot by default, but using them requires special "I know what I'm doing" incantations.
I will be happy to publish a "rustc_interface hello world" github repo if someone who understands the internals can help me figure out how to do this...
I am making progress...it seems that these components were removed from nightly by default and now you need to do rustup component add --toolchain nightly rustc-dev to get access to them.
I'm guessing I need to change some of the options in rustc_interface::interface::Config but there doesn't seem to be any documentation and the way rustc itself uses Config is hard to follow. Perhaps someone can point me in the right direction?
You might need to run expansion (the expansion query) after HIR lowering. IIRC, expansion inserts an implicit extern crate core; extern crate std; at the top of the crate.
The error is happening when I call lower_to_hir, so it's hard to see how calling expansion after I call lower_to_hir is going to fix the error that already happened. But I tried making the order of operations more similar to the compiler:
So now that we're merrily improving the rustc docs, I have a second question: how can I get the type of a span that isn't a definition? Suppose I want to get the type of message inside println("{}", message); in the below program:
You can do this in different ways: you could traverse the AST before or after expansion (before is easier to recognize the println) or you could traverse the HIR. The span should then be stored next to the variable use.
Ok thank you, but how do I get from the span to the type? I'm not seeing any Span -> Ty function in TyCtxt or Queries, the closest thing is type_of: DefId -> Ty, which only seems to make sense for getting types top-level definitions.
Now I'm trying to figure out how to get compiler error messages. I'm guessing I need to use the global_ctx() query to get a TyCtxt, similar to how I get the type of an expression, but it's not clear where I go for a list of errors. Can you point me in the right direction?