rust-lang / rust-lang/rfcs

A macro fragment matcher analogous to right-hand side of a match arm

Open
#773 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I have some code for dynamically typed error handling, and to provide syntactic sugar over nested match expressions I have a macro with syntax that itself mimics a single match expression:

error_match! {
    (err) {
        (a_err: DomainError<A>) => {
            // ...
        }  // note no comma here
        (b_err: DomainError<B>) => (),  // an expression followed by a comma
        // ...
    }
}

There does not seem to be a way to alternate different matchers in repetition sequences, and furthermore, the current rules disallow block fragments followed by repetition sequences. So in realizing such macro recursively I'm limited to comma-appended expressions at the end of each arm, like so:

(
    ($inp:expr) {
        ($slot:ident : $errtype:ty) => $handler:expr,
        $(($slot_tail:ident : $errtype_tail:ty) => $handler_tail:expr),*
    }
) => { ... }

The need to put a comma after a block is annoying, as the real match statement syntax forbids commas in this position. So I'd like there to be a fragment specifier that would make the fragment match either an expression followed by a comma, or a block. The macro future-proofing rules for fragments of this type would allow them to immediately precede sequence repetition and end a repeating sequence, so the invocation pattern above could be extended as follows:

(
    ($inp:expr) {
        ($slot:ident : $errtype:ty) => $handler:match_handler
        $(($slot_tail:ident : $errtype_tail:ty) => $handler_tail:match_handler)*
    }
) => { ... }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed match_handler fragment syntax and the macro future-proofing constraints described in the issue. Determine whether one fragment can match either a block or a comma-terminated expression before repetition; done means the design is resolved and the shown invocation can omit commas after blocks.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.