rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Support macro invocations in match branch position

Open canndrew opened this issue 6 years ago • 4 comments

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?

canndrew avatar Mar 03 '19 03:03 canndrew

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.

petrochenkov avatar Mar 03 '19 07:03 petrochenkov

Multiple match arms would be nice, yeah.

canndrew avatar Mar 07 '19 00:03 canndrew

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

Centril avatar Mar 07 '19 00:03 Centril

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
  };

mcmah309 avatar Jul 23 '24 16:07 mcmah309