Skip to content

Creating a macro of generating closure, description of arguments to closure in || with a star only works as an exclamation point. #72652

Open
@denisandroid

Description

@denisandroid

It was expected that I could repeat the syntax of the closure in the macro, I repeated but the rule is not working out correctly.

Code:

macro_rules! test {
    [ | $($args:ident),* | $($block:tt)* ] => {
        | $($args),* | $($block)*
    };
}

fn main() {
    let enc = &test! {
        |a| {
            println!("1");
        }
    } as &'static dyn FnMut(usize);
    
    let enc2 = &test! {
        || {
            println!("2");
        }
    } as &'static dyn FnMut();
}

Output:

error: no rules expected the token `||`
  --> src/main.rs:16:9
   |
2  | macro_rules! test {
   | ----------------- when calling this macro
...
16 |         || {
   |         ^^ no rules expected this token in macro call

Macro code using one or more elements works, but macro code without elements does not work. Although this is precisely the rule with the star.

Play: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=bd27f49b9feeb021af76615321013784

Rust Version: stable 1.43.1 or rustc 1.45.0-nightly (a74d186 2020-05-14)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-grammarArea: The grammar of RustA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-parserArea: The lexing & parsing of Rust source code to an ASTC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions