Skip to content

Commit

Permalink
Fix discrepancy between mergeWithKey impl and docs (#1025)
Browse files Browse the repository at this point in the history
Don't call the `only2` function with empty trees.
Fixed for both Map and IntMap.
  • Loading branch information
meooow25 authored Aug 14, 2024
1 parent 5723db9 commit 734785d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions containers/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* `Data.Map.Strict.mergeWithKey` now forces the result of the combining function
to WHNF. (Soumik Sarkar)

* Fix an issue where `Data.Map.mergeWithKey`, `Data.Map.Strict.mergeWithKey`,
`Data.IntMap.mergeWithKey`, `Data.IntMap.Strict.mergeWithKey` could call the
provided `only2` function with empty maps, contrary to documentation.
(Soumik Sarkar)

## Unreleased with `@since` annotation for 0.7.1:

### Additions
Expand Down
2 changes: 2 additions & 0 deletions containers/src/Data/IntMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,8 @@ mergeWithKey' bin' f g1 g2 = go
| otherwise = maybe_link k1 (g1 t1) k2 (g2 t2)
merge0 t1 _ Nil = g1 t1

go Nil Nil = Nil

go Nil t2 = g2 t2

maybe_link _ Nil _ t2 = t2
Expand Down
1 change: 1 addition & 0 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,7 @@ mergeWithKey :: Ord k
-> Map k a -> Map k b -> Map k c
mergeWithKey f g1 g2 = go
where
go Tip Tip = Tip
go Tip t2 = g2 t2
go t1 Tip = g1 t1
go (Bin _ kx x l1 r1) t2 =
Expand Down
1 change: 1 addition & 0 deletions containers/src/Data/Map/Strict/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ mergeWithKey :: Ord k
-> Map k a -> Map k b -> Map k c
mergeWithKey f g1 g2 = go
where
go Tip Tip = Tip
go Tip t2 = g2 t2
go t1 Tip = g1 t1
go (Bin _ kx x l1 r1) t2 =
Expand Down

0 comments on commit 734785d

Please sign in to comment.