Skip to content

Add a macro non-terminal for the interior of a block #11928

Closed
@huonw

Description

@huonw

e.g. (using the currently-invalid brace syntax from #11892)

macro_rules! spawn {
     ($b: block_internals) => {
        spawn(proc() { $b })
     }
}

spawn! {
     use foo::bar;
     fn baz() {}

     if x == 0 { bar() }
     let a = 1 + 2;
     foo();
     baz()
}

I don't believe this is currently possible to emulate. e.g. with $($s: stmt);* you have to write fn foo() {}; if x == 0 { bar() }; ... and without the ; you end up with foo() bar() being valid (and expr doesn't allow for statements like let and fn bar() {}). The best solution I've found is just writing ($e: expr) => { ... } but this requires double braces:

spawn! { foo() } // ok

spawn! { foo(); bar() } // not ok! :(

spawn! {{ foo(); bar() }} // ok :(

This would make sugary control-flow-y macros like spawn and scope/finally from #11905 more natural.

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