How to span error with AttrProcMacro


#1

Hello.
I wonder how to span errors in an AttrProcMacro.
If we look at the example:

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {  
    reg.register_syntax_extension(token::intern("foo"),
                                  SyntaxExtension::AttrProcMacro(Box::new(foo_impl)));
    reg.register_syntax_extension(token::intern("bar"),
                                  SyntaxExtension::ProcMacro(Box::new(bar)));
}

fn foo_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {  
    let _source = item.to_string();
    lex("fn f() { println!(\"Good bye!\"); }")
}

The foo_impl function does not receive the context, so how can I span an error?
Thanks for your help.


#2

I found out why:
the implementor of AttrProcMacro is impl<F> AttrProcMacro for F where F: Fn(TokenStream, TokenStream) -> TokenStream.
So I have to do something like:

struct ExpandSqlTable {
}

impl AttrProcMacro for ExpandSqlTable {
    fn expand(&self, cx: &mut ExtCtxt, span: Span, annotatable: TokenStream, annotated: TokenStream) -> TokenStream {
        annotated
    }
}

Any idea why it was implemented differently that the other syntax extension trait?