Syntax.ast how to create an 'as'


#1

What does the following call look like when using syntax.ast:

::method_defined_on_mytrait()

I’m writing a compiler plugin, but I just can’t quite nail outputing the above code. I think I need a QSelf with position 1 as the to_string of that then shows the ‘> as <’, but then the function ‘call_on_mytrait’ is interpreted as an associated type. When I set position:0 on the QSelf and don’t include the final PathSegment then the ‘> as <’ isn’t produced.

TyKind::Path(
Some(QSelf{ty:P(impl_type), position:1}),
Path{
span,
segments:vec![trait_path,
PathSegment { span, parameters: None, identifier: Ident::from_str(“call_on_mytrait”) } ] })

Just not quite there. Alternatively is there a quote! macro for compiler plugins - I got a sniff that such a thing might once have existed on google but I couldn’t seem to import it?

(I’m doing a compiler plugin as it is my understanding that proc macros can only be used for custom derive at this moment - in my case I need to add a function to an existing trait).


#2

It turns out that quote is a feature, not a use_macro imported from a crate:

#![feature(quote)]

That simplifies things!


#3

You can use proc-macros for other things on nightly:


#4

Ok, in proc macro land. When you have a TokenStream coming in to a proc macro as an Item, is there any way to return multiple items without wrapping them in a new module?


#5

(For now I can look at how custom derive works.)