Procedural macro & trait inference


#1

Hello,

in a #[derive()] proc macro, is there a way to know all the traits that are implemented by a struct without having to use attributes?

As an example, with the below code, can I get somehow the fact that Foo implements Foo1, Foo2?

trait Foo1 {
   fn foo1(&self);
}

trait Foo2 {
   fn foo2(&self);
}

#[derive(MyType)]
struct Foo;
impl Foo1 for Foo { /* ... */ }
impl Foo2 for Foo { /* ... */ }

Thanks a lot :slight_smile:


#2

Unfortunately not. Any macro (whether 1.0 or 2.0) is only about syntax. They know nothing about semantics. At the point when the macro handler for #[derive(MyType)] is invoked Rust knows nothing about the later impl blocks. Also the compiler has no idea about types at this stage of the parsing/transformation process, i.e., no reflection mechanism is available here.

This can be a real drawback in some situations.


#3

A real shame indeed, thanks a lot for the prompt answer @mindsbackyard