How to get trait definition from compiler plugin?

I want to create compiler plugin which may be used like this:

trait A { fn do_a() }
my_macro!{A}

where my_macro does something based on definition of A. I know how to create compiler plugin and get passed identifier, but I can't find a way to get A's members from compiler plugin. Is it possible?

1 Like

No.

Macros are expanded before that sort of information is constructed. This is why you can't use derive except as an attribute; it has to be provided the struct/enum's AST to work.

You can, however, write a MultiModifier that works like:

#[my_macro]
trait A {
 fo do_a()
}

Cool, I will definitely look into MultiModifier. However, I need my macro to work for traits from other crates too. It is possible to create macro which may be used like this:

my_macro!{
  trait A {
    fn do_a()
  }
}

and get parsed trait definition inside?

Um, those macros (the exclamation mark ones) work on token trees, so I'm not sure. You may have to manually call the parser methods.

However, using an attribute (the #[..] thing) which is a MultiDecorator or MultiModifier will give you a parsed trait def, and the ability to replace or augment it.