Skip to content

Commit 0804b41

Browse files
authored
Merge pull request #4927 from rescript-lang/fix_4924
fix #4924, better code generated for patter match
2 parents 6f5f968 + 76c6273 commit 0804b41

18 files changed

+816
-959
lines changed

jscomp/core/lam.ml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,11 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
676676
default ()
677677

678678
let not_ loc x : t =
679-
prim ~primitive:Pnot ~args:[x] loc
679+
match x with
680+
| Lprim ({primitive = Pintcomp Cneq } as prim)->
681+
Lprim {prim with primitive = Pintcomp Ceq}
682+
| _ ->
683+
prim ~primitive:Pnot ~args:[x] loc
680684

681685

682686
let has_boolean_type (x : t) =
@@ -769,6 +773,24 @@ let if_ (a : t) (b : t) (c : t) : t =
769773
{ body with sw_failaction = Some b; sw_consts_full = false; })
770774
| _ -> Lifthenelse(a,b,c)
771775
end
776+
| Lprim{primitive = Pisint; args = [Lvar i];_}
777+
->
778+
begin match b with
779+
| Lifthenelse(Lprim{primitive = Pintcomp Ceq ; args = [Lvar j; Lconst _]} , _, b_f)
780+
when Ident.same i j && eq_approx b_f c ->
781+
b
782+
| Lprim{primitive = Pintcomp Ceq ; args = [Lvar j; Lconst _]}
783+
when Ident.same i j && eq_approx false_ c -> b
784+
| Lifthenelse(Lprim({primitive = Pintcomp Cneq ; args = [Lvar j; Lconst _]} as b_pred) , b_t, b_f)
785+
when Ident.same i j && eq_approx b_t c ->
786+
Lifthenelse(Lprim{b_pred with primitive = Pintcomp Ceq}, b_f, b_t)
787+
| Lprim({primitive = Pintcomp Cneq ; args = [Lvar j; Lconst _] as args ; loc} )
788+
| Lprim(
789+
{primitive = Pnot ; args = [Lprim{primitive = Pintcomp Ceq ; args = [Lvar j; Lconst _] as args; loc}]})
790+
when Ident.same i j && eq_approx true_ c
791+
-> Lprim{primitive = Pintcomp Ceq; args; loc}
792+
| _ -> Lifthenelse(a,b,c)
793+
end
772794
| _ -> Lifthenelse (a,b,c))
773795

774796

jscomp/test/build.ninja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ o test/gpr_4494_test.cmi test/gpr_4494_test.cmj : cc test/gpr_4494_test.ml | $st
333333
o test/gpr_4519_test.cmi test/gpr_4519_test.cmj : cc test/gpr_4519_test.ml | test/mt.cmj $stdlib
334334
o test/gpr_459_test.cmi test/gpr_459_test.cmj : cc test/gpr_459_test.ml | test/mt.cmj $stdlib
335335
o test/gpr_4639_test.cmi test/gpr_4639_test.cmj : cc test/gpr_4639_test.ml | $stdlib
336+
o test/gpr_4924_test.cmi test/gpr_4924_test.cmj : cc test/gpr_4924_test.ml | $stdlib
336337
o test/gpr_627_test.cmi test/gpr_627_test.cmj : cc test/gpr_627_test.ml | test/mt.cmj $stdlib
337338
o test/gpr_658.cmi test/gpr_658.cmj : cc test/gpr_658.ml | $stdlib
338339
o test/gpr_858_test.cmi test/gpr_858_test.cmj : cc test/gpr_858_test.ml | $stdlib

0 commit comments

Comments
 (0)