rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.93k stars 1.57k forks source link

Support macro invocations in match branch position #2654

Open canndrew opened 5 years ago

canndrew commented 5 years ago

Sorry if there's already and issue for this, but I couldn't find one.

On many occasions I've wanted to use macros to generate match branches. eg.

match x {
    macro_which_outputs_some_branches!(),
    Foo::Bar => 23,
}

Is there a reason not to support this?

petrochenkov commented 5 years ago

Do you mean macros expanding to multiple match arms? For a single branch this will be automatically supported by https://github.com/rust-lang/rust/issues/54883.

canndrew commented 5 years ago

Multiple match arms would be nice, yeah.

Centril commented 5 years ago

@petrochenkov That just supports or-patterns; but afaik you cannot generate match arms (pat (if expr?) => expr) today...?

mcmah309 commented 2 months ago

This would be useful for this crate: https://github.com/mcmah309/error_set

Currently users have to do this if they want to handle disjointedness with a macro

let val = coerce!(setx => {
      Ok(val) => val,
      Err(SetX::X) => {}, // handle disjointedness
      { Err(SetX) => return Err(SetY) } // terminal coercion
  });

But this would be a lot better if it was possible

let val = match setx {
      Ok(val) => val,
      Err(SetX::X) => {}, // handle disjointedness
      coerce!(Err(SetX) => return Err(SetY)) // terminal coercion
  };