Skip to content

Macros attempting to expand to multiple items silently only expand to the first one #8012

Closed
@paulstansifer

Description

@paulstansifer

Users who want a macro that expands to a collection items are required to wrap the RHS in {} in order to get them to expand to a whole block. However, if they fail to do so, they unexpectedly just get the first item of the block, rather than an error message.

macro_rules! print_if_nonzero(
    ($msg:expr, $($field:ident),*) => ( /*{*/
        $(
            if t.$field != 0 {
                println($msg);
            }
        )*
    /*}*/ )
)

struct T { x: int, y: int }

fn main() {
    let t = T { x: 0, y: 0 };
    print_if_nonzero!("hello", x, y);
}

...expands to...

fn main() { let t = T{x: 0, y: 0,}; if t.x != 0 { println("hello"); }; }

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions