Skip to main content

match_needs_ifchain

Function match_needs_ifchain 

Source
pub fn match_needs_ifchain(arms: &[AIRNode]) -> bool
Expand description

Returns true if a match’s arms require the if/else-if-chain lowering (JS, TS, Go) rather than the value/tag switch fast-path.

The value/tag switch those backends emit can express only a flat dispatch on a single discriminant (a literal value, or an ADT ._tag). It structurally cannot express:

  • guards — a failed guard must fall through to the next arm, but a break inside a switch exits the whole switch;
  • or-patterns (1 | 2 | 3 => …) — one arm, several discriminants;
  • tuple patterns ((a, b) => …) — no single discriminant;
  • nested constructor / record patterns (Some(Ok(v)) => …) — the inner pattern must itself be tested and its bindings extracted recursively.

When any arm needs one of these, the backend lowers the whole match to an if (<test> && <guard?>) { <binds>; <body> } else if … chain (see each backend’s emit_match_ifchain). Otherwise the existing switch fast-path is kept, so the proven Optional / Result / user-enum / value lowerings do not regress.

A constructor / record field counts as “nested” only when its sub-pattern is itself refutable or structured — another constructor, record, tuple, or-pattern, or literal. A bare bind (Some(x)) or wildcard (Some(_)) field is not nested: the flat switch already extracts those correctly.