graydon/rust — Issue #1176: Toplevel macros
Author: Yoric
Created: 2011-11-15 13:22:06+00:00
State: open

At the moment, it seems that macros can only be defined inside a function and for the scope of this function. Among other things, this prevents from writing crate-wide macros or exported macros, which basically makes macros unusable.

For instance, src/test/run-pass/macros.rs contains:

fn main() { #macro[[#m1[a], a * 4]]; assert (#m1[2] == 8); }

If we transform it as follows:

#macro[[#m1[a], a * 4]];
fn main() {assert (#m1[2] == 8); }

we obtain the following error:
a.rs:1:1: 1:6 error: expecting [, found macro
a.rs:1 #macro[[#m1[a], a * 4]];
^~~~~

So, if there is a task that makes it possible to write the macro at toplevel, it should be made more discoverable. If there is none, well, it should be added.