Skip to content

Commit 734785d

Browse files
authored
Fix discrepancy between mergeWithKey impl and docs (#1025)
Don't call the `only2` function with empty trees. Fixed for both Map and IntMap.
1 parent 5723db9 commit 734785d

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

containers/changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* `Data.Map.Strict.mergeWithKey` now forces the result of the combining function
1515
to WHNF. (Soumik Sarkar)
1616

17+
* Fix an issue where `Data.Map.mergeWithKey`, `Data.Map.Strict.mergeWithKey`,
18+
`Data.IntMap.mergeWithKey`, `Data.IntMap.Strict.mergeWithKey` could call the
19+
provided `only2` function with empty maps, contrary to documentation.
20+
(Soumik Sarkar)
21+
1722
## Unreleased with `@since` annotation for 0.7.1:
1823

1924
### Additions

containers/src/Data/IntMap/Internal.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,8 @@ mergeWithKey' bin' f g1 g2 = go
14461446
| otherwise = maybe_link k1 (g1 t1) k2 (g2 t2)
14471447
merge0 t1 _ Nil = g1 t1
14481448

1449+
go Nil Nil = Nil
1450+
14491451
go Nil t2 = g2 t2
14501452

14511453
maybe_link _ Nil _ t2 = t2

containers/src/Data/Map/Internal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,7 @@ mergeWithKey :: Ord k
27822782
-> Map k a -> Map k b -> Map k c
27832783
mergeWithKey f g1 g2 = go
27842784
where
2785+
go Tip Tip = Tip
27852786
go Tip t2 = g2 t2
27862787
go t1 Tip = g1 t1
27872788
go (Bin _ kx x l1 r1) t2 =

containers/src/Data/Map/Strict/Internal.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@ mergeWithKey :: Ord k
12401240
-> Map k a -> Map k b -> Map k c
12411241
mergeWithKey f g1 g2 = go
12421242
where
1243+
go Tip Tip = Tip
12431244
go Tip t2 = g2 t2
12441245
go t1 Tip = g1 t1
12451246
go (Bin _ kx x l1 r1) t2 =

0 commit comments

Comments
 (0)