Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
huonw opened this issue Jan 30, 2014 · 2 comments
Closed

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

huonw opened this issue Jan 30, 2014 · 2 comments
Labels
A-syntaxext Area: Syntax extensions

Comments

@huonw
Copy link
Member

huonw commented Jan 30, 2014

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.

@huonw
Copy link
Member Author

huonw commented Jan 31, 2014

@cmr reminded me that the following actually works:

macro_rules! expr { ($e: expr) => { $e } }

macro_rules! spawn {
    ( $($body: tt)* ) => {
        expr!(::std::task::spawn(proc() { $($body)* }))
    }
}

fn main() {
     spawn! { foo(); bar() } // perfectly fine
}

but that's pretty ugly, because tt's can only be passed to macro invocations, so we need the silly no-op expr macro to allow us to interpolate the $body tokens (#5846).

@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#672

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

2 participants