E4220

E4220#

Compiler diagnostic name: regex_match_binding_not_supported.

A lexical case uses a binding that is not available in its matching mode.

For String and StringView inputs, first-match lexmatch supports both before= and after=. Longest-match mode starts at the beginning of the input, so it supports after= but not before=. lexscan does not support either binding: streaming targets consume their input buffers, while an in-memory @lexbuf.StringScanner stores the next scan position in its cursor field.

Erroneous example#

///|
pub fn first_word(input : String) -> StringView {
  lexmatch input with longest {
    (re"^[a-z]+" as word, before=_) => word
    _ => ""
  }
}

Suggestion#

For lexscan, remove before= or after= and anchor the regex at ^. For lexmatch, remove before=, anchor the regex at ^, and use after= when the unmatched suffix is needed.

///|
pub fn first_word(input : String) -> StringView {
  lexmatch input with longest {
    (re"^[a-z]+" as word, after=_) => word
    _ => ""
  }
}