git.iko.soy/rowtype-yoga/purescript-backend-optimizer
on main help sign in
Fix record normalization bug when last row pattern is wildcard (#43)

* Add broken snapshot from bug report * Verify that second case row is being dropped * Return accumulated value if no new keys * Correct snapshot

commit6509f5db2a158c7e62cd912b5770b23fad86e6b3 authorJordanMartinez <jordanalex.martinez@protonmail.com> dateMon 13 Mar 2023, 12:24 (4 years ago) parent7a05835
3 files changed+56−3
backend-es/test/snapshots-out/Snapshot.CaseRecord.js+27−1 backend-es/test/snapshots/Snapshot.CaseLitRecord.purs+27 src/PureScript/Backend/Optimizer/Convert.purs+2−2
backend-es/test/snapshots-out/Snapshot.CaseRecord.js+27−1
@@ -1,3 +1,23 @@
1+const test6 = v => {
2+ if (v.a > 0) { return v.a; }
3+ if (v.b > 0) { return v.b; }
4+ return 0;
5+};
6+const test5 = v => {
7+ if (v.a > 0) { return v.a; }
8+ if (v.b > 0) { return v.b; }
9+ return 0;
10+};
11+const test4 = v => {
12+ if (v.a > 0) { return v.a; }
13+ if (v.b > 1) { return v.b; }
14+ return 3;
15+};
16+const test3 = v => {
17+ if (v.a > 0) { return v.a; }
18+ if (v.b > 1) { return v.b; }
19+ return 3;
20+};
121const test2 = v => {
222 if (v.a.c === 2) {
323 if (v.d.e === 1) {
@@ -15,6 +35,12 @@ const test2 = v => {
1535};
1636const test1 = v => {
1737 if (v.a === 1) { return "0"; }
38+ if (v.b === 1) { return "1"; }
39+ if (v.c === 1) { return "2"; }
40+ if (v.a === 2) {
41+ if (v.b === 2) { return "3"; }
42+ return "catch";
43+ }
1844 return "catch";
1945};
20−export {test1, test2};
46+export {test1, test2, test3, test4, test5, test6};
backend-es/test/snapshots/Snapshot.CaseLitRecord.purs+27
@@ -1,5 +1,7 @@
11module Snapshot.CaseRecord where
22
3+import Data.Ord ((>))
4+
35test1 :: { a :: Int, b :: Int, c :: Int } -> String
46test1 = case _ of
57 { a: 1 } -> "0"
@@ -16,3 +18,28 @@ test2 = case _ of
1618 { a: { b: _, c: 2 }, d: { e: 1, f: 2 } } -> 2
1719 { a: { b: 1, c: 2 }, d: _ } -> 3
1820 _ -> 4
21+
22+test3 :: { a :: Int, b :: Int } -> Int
23+test3 = case _ of
24+ { a } | a > 0 -> a
25+ { a: _, b } | b > 1 -> b
26+ _ -> 3
27+
28+test4 :: { a :: Int, b :: Int } -> Int
29+test4 = case _ of
30+ { a, b: _ } | a > 0 -> a
31+ { b } | b > 1 -> b
32+ _ -> 3
33+
34+test5 :: { a :: Int, b :: Int } -> Int
35+test5 = case _ of
36+ { a, b: _ } | a > 0 -> a
37+ { a: _, b } | b > 0 -> b
38+ _ -> 0
39+
40+test6 :: { a :: Int, b :: Int } -> Int
41+test6 = case _ of
42+ { a, b }
43+ | a > 0 -> a
44+ | b > 0 -> b
45+ _ -> 0
src/PureScript/Backend/Optimizer/Convert.purs+2−2
@@ -58,7 +58,7 @@ import Data.Function (on)
5858import Data.FunctorWithIndex (mapWithIndex)
5959import Data.Map (Map, SemigroupMap(..))
6060import Data.Map as Map
61−import Data.Maybe (Maybe(..), fromJust, maybe)
61+import Data.Maybe (Maybe(..), fromJust, fromMaybe, maybe)
6262import Data.Monoid as Monoid
6363import Data.Monoid.Additive (Additive(..))
6464import Data.Newtype (class Newtype, over, unwrap)
@@ -657,7 +657,7 @@ normalizeCaseRows = normalizeProps =<< columnProps
657657 let keys = Set.fromFoldable fields
658658 maybe keys (append keys) acc
659659 _ ->
660− Set.empty
660+ fromMaybe Set.empty acc
661661
662662 normalizeProps :: Array (Set String) -> Array CaseRow -> Array CaseRow
663663 normalizeProps allFieldNames = map \nextRow ->