yawn
yawn

Reputation: 515

In Rust, is it possible to accept arbitrary syntax in attribute macros? (just like other macros)

In Rust, both declarative and procedural macros (macro_rules!) can accept arbitrary syntax which isn't necessarily valid Rust, provided that it parses. For instance, in the docs, this is used to create a macro which parses SQL queries from an SQL-like syntax.

However, when using attribute macros, this does not seem to be the case. For example:

#[my_attribute_macro]
fn example() {
    impl A {
        impl B {
            // This is invalid Rust code (nested `impl`s) which is parsable
        }
    }
}

Yields a compiler error because impls cannot be nested in valid Rust. Is there any way of working around this limitation, to get attribute macros which work more like other macros?

Upvotes: 2

Views: 709

Answers (1)

user1930596
user1930596

Reputation: 1

As far as I know, this is not possible today. The example you see in the docs is a function-like macro, not an attribute macro (proc-macro).

You can provide custom syntax to macros like this: sql!( CUSTOM SYNTAX )

But not to macros like this: #[sql] fn sql() { Custom Syntax }

Upvotes: 0

Related Questions