Why does quote_expr!() requires ExtCtxt and expand to large token tree?

While building a compiler plugin for compiler plugins, I found that I can use extctxt from current compiler, but rust’s quote!() macros accept (cx, ) and generate very long code.

Compiler plugin - https://github.com/appcore-project/rust-cg

I invoked cargo rustc --verbose -- -Z unstable-options --pretty=expanded > expanded.rs and below is some of generated code.

Token tree regenerated, and then parsed again?

Looks like there isn’t a good way of accomplishing quasiquoting, so the quote_x! macros just expand to vectors of token trees and insert the $ variables by converting them to token trees. The whole vector is then reparsed and returned.

The ExtCtxt is required because the created parser reuses its data (this might be used for error reporting).

This doesn’t look like a performant solution, but I don’t think there’s a better way to do it, since you can splice in arbitrary token trees from variables (so you have to reparse all tokens). The code could be shortened by using relative paths, but macros don’t generally do that.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.