# Plans for quasi-quoting libsyntax

**URL:** <https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598>\
**Category:** internals\
**Created:** [July 19, 2019, 4:40am UTC](https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598 "2019-07-19T04:40:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhynes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nhynes/32/5886_2.png) [@nhynes](https://internals.rust-lang.org/u/nhynes)\
**Post date:** [July 19, 2019, 4:40am UTC](https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598/1 "2019-07-19T04:40:59Z")

</div>

Is there any plan on having quasi-quoting for libsyntax? It'd be useful both for codegen within the compiler ([example](https://github.com/rust-lang/rust/blob/e13fe7ff5e482b626714ac6ec41b8d44e7c22ed1/src/libsyntax/test.rs#L300)) and compiler plugins. Right now, one either constructs the AST item manually or creates it from a string using `parse_*` (but really just dumping it into a file because the spans are all wrong). Ideally, this would be done using existing quasi-quoting machinery.

"Why not just use the normal `quote`?", you ask. Unfortunately (but with good reason) the conversion from `proc_macro::TokenTree` to `syntax::tokenstream::TokenStream` is private, so there'd need to be a special `quote` for libsyntax to avoid re-parsing the stream.

My hunch is that it'll be a long time before this happens (if ever), but I was wondering if it's on the roadmap. In any case, I'd like to express support to any existing efforts!

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [July 19, 2019, 4:42am UTC](https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598/2 "2019-07-19T04:42:58Z")

</div>

My understanding is that there's a strong desire to do so, but that doing so would require great care to avoid accidentally making some key bit of how Rust parses syntax into forever-stable API.

---

<div class="post-metadata">

**Author:** ![nhynes](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nhynes/32/5886_2.png) [@nhynes](https://internals.rust-lang.org/u/nhynes)\
**Post date:** [July 19, 2019, 4:48am UTC](https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598/3 "2019-07-19T04:48:36Z")

</div>

> [@josh](#):
>
> avoid accidentally making some key bit of how Rust parses syntax into forever-stable API.

Would it not be just as unstable as all of the "public" methods in `syntax::parse`\*? Probably the _easiest_ thing to do would be to expose [`ToInternal`](https://github.com/rust-lang/rust/blob/9210359b18185f46af9c0f30cc3d2678394930ef/src/libsyntax_ext/proc_macro_server.rs#L194) else have to reimplement the whole thing in terms of libsyntax `TokenTree`. That'd make a really weird dependency loop, though.

\* RIP `pprust` 😉

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [July 19, 2019, 9:31am UTC](https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598/4 "2019-07-19T09:31:44Z")

</div>

Quasi-quoting machinery generating AST existed, but was removed in [https://github.com/rust-lang/rust/pull/51285](https://github.com/rust-lang/rust/pull/51285).  
I don't know why it wasn't used in the compiler's built-in macros (perhaps it was at some point?).

The general long term plan is to migrate built-in macros to the same token-based model that proc macros use, which will obsolete AST builder interfaces entirely.  
Some form of quasi-quoting would be useful to do that migration.  
Not sure about details, perhaps it will be based on `syntax::tokenstream::TokenStream` rather than on `proc_macro::TokenStream` because we may want to avoid the proc macro server/client overhead in this case.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 17, 2019, 9:37am UTC](https://internals.rust-lang.org/t/plans-for-quasi-quoting-libsyntax/10598/5 "2019-10-17T09:37:53Z")

</div>

Closed on October 17, 2019, 9:37am UTC

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