[rust-dev] Macros expanding to multiple statements

Ashish Myles marcianx at gmail.com
Sat Jan 11 19:13:59 PST 2014


Ah, I didn't realize the distinction.  I am comparing the code in the first
comment in the bug you linked against the test suite example I linked.  I
guess the distinction between items and statements is that items correspond
to code outside any method, whereas statements are defined as code within a
method, and macro expansions in the latter case seem to be broken even in
the case of a single statement.  Please correct me if I am wrong.

Ashish



On Sat, Jan 11, 2014 at 9:43 PM, Huon Wilson <dbau.pp at gmail.com> wrote:

>  That test is for multiple *items*, not statements.
>
> For the moment, you just have to wrap the interior of a macro expanding to
> an expression in a set of braces, so that it becomes a single statement.
>
>
> macro_rules! my_print(
>     ($a:expr, $b:expr) => (
>         {
>             println!("{:?}", a);
>             println!("{:?}", b);
>         }
>     );
> )
>
> Multi-statement macros are covered by
> https://github.com/mozilla/rust/issues/10681 .
>
>
> Huon
>
>
>
> On 12/01/14 13:40, Ashish Myles wrote:
>
>  Rust 0.9 indicates that support for expansion of macros into multiple
> statements is now supported, and the following example from the test suite
> works for me.
>
> https://github.com/mozilla/rust/blob/master/src/test/run-pass/macro-multiple-items.rs
>
>  However, I receive an error for the following code
>
> #[feature(macro_rules)];
>
> macro_rules! my_print(
>     ($a:expr, $b:expr) => (
>         println!("{:?}", a);
>         println!("{:?}", b);
>     );
> )
>
> fn main() {
>     let a = 1;
>     let b = 2;
>     my_print!(a, b);
> }
>
>  (Note that the ^~~~~~~ below points at println.)
>
> $ rustc macro_ignores_second_line.rs
> macro_ignores_second_line.rs:6:9: 6:16 error: macro expansion ignores
> token `println` and any following
> macro_ignores_second_line.rs:6         println!("{:?}", b);
>                                        ^~~~~~~
> error: aborting due to previous error
> task 'rustc' failed at 'explicit failure',
> /home/marcianx/devel/rust/checkout/rust/src/libsyntax/diagnostic.rs:75
> task '<main>' failed at 'explicit failure',
> /home/marcianx/devel/rust/checkout/rust/src/librustc/lib.rs:453
>
>
>  What's the right way to do this?
>
> Ashish
>
>
> _______________________________________________
> Rust-dev mailing listRust-dev at mozilla.orghttps://mail.mozilla.org/listinfo/rust-dev
>
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140111/e37709c2/attachment.html>


More information about the Rust-dev mailing list