You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[12.x] Switch arms returning coerced variant or record constants are merged, returning the wrong value #9385
Is it already fixed in master? Yes, master (02a99b7) compiles this correctly. It reproduces on maintenance/12.x and in 12.3.1.
Summary
When switch arms return constant blocks, the compiler merges arms whose blocks have the same tag and fields, even if their constructor names or record field names differ. After a coercion, two such blocks compile to different JavaScript, so the merged function returns the wrong value for some inputs.
A block's tag is its constructor's position in its type. Second is at position 0 in small, as First is in big, and xy and yx records are both tag 0. Two equality checks compare the tag and the fields but ignore tag_info, which holds the constructor name and record field names the block is printed with:
Lam_constant.eq_approx (Const_block), which Lam_compile.group_apply uses to merge switch arms, as above;
Js_analyzer.eq_expression (Caml_block), which Js_stmt_make.if_ uses to merge if/else branches. For example, (b: bool): big => b ? First(1) : (Second(1): small :> big) returns First(1) for false.
Activity