E4210

E4210#

Compiler diagnostic name: regex_alias_in_alternation.

Regex alias is not allowed inside an alternation branch.

In a regex match expression, as binds the matched text. When the pattern uses alternation with |, bind the whole alternation instead of binding only one branch. Otherwise the binder would exist only for some alternatives.

Erroneous example#

fn match_text(s : String) -> Unit {
  ignore(s =~ ((re"a" as left) | re"b"))
}

left is bound only when the first branch matches.

Suggestion#

Move the alias outside the alternation so it is defined no matter which branch matches.

fn match_text(s : String) -> Unit {
  ignore(s =~ ((re"a" | re"b") as either))
}