Skip to content

Commit 17f98f0

Browse files
authored
Strict.mapMaybe[WithKey]: Fix strictness for collisions (#385)
Fixes #381.
1 parent 596075f commit 17f98f0

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Data/HashMap/Internal/Strict.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ mapMaybeWithKey f = filterMapAux onLeaf onColl
548548
where onLeaf (Leaf h (L k v)) | Just v' <- f k v = Just (leaf h k v')
549549
onLeaf _ = Nothing
550550

551-
onColl (L k v) | Just v' <- f k v = Just (L k v')
551+
onColl (L k v) | Just !v' <- f k v = Just (L k v')
552552
| otherwise = Nothing
553553
{-# INLINE mapMaybeWithKey #-}
554554

tests/Regressions.hs

+27
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,27 @@ issue379LazyUnionWith = do
187187
touch v -- makes sure that we didn't GC away the combined value
188188
assert $ isNothing res
189189

190+
------------------------------------------------------------------------
191+
-- Issue #381
192+
193+
#ifdef HAVE_NOTHUNKS
194+
195+
issue381mapMaybe :: Assertion
196+
issue381mapMaybe = do
197+
let m0 = HMS.fromList [(KC 1, 10), (KC 2, 20 :: Int)]
198+
let m1 = HMS.mapMaybe (Just . (+ 1)) m0
199+
mThunkInfo <- noThunksInValues mempty (Foldable.toList m1)
200+
assert $ isNothing mThunkInfo
201+
202+
issue381mapMaybeWithKey :: Assertion
203+
issue381mapMaybeWithKey = do
204+
let m0 = HMS.fromList [(KC 1, 10), (KC 2, 20 :: Int)]
205+
let m1 = HMS.mapMaybeWithKey (\(KC k) v -> Just (k + v)) m0
206+
mThunkInfo <- noThunksInValues mempty (Foldable.toList m1)
207+
assert $ isNothing mThunkInfo
208+
209+
#endif
210+
190211
------------------------------------------------------------------------
191212
-- * Test list
192213

@@ -206,4 +227,10 @@ tests = testGroup "Regression tests"
206227
, testCase "Strict.unionWithKey" issue379StrictUnionWithKey
207228
#endif
208229
]
230+
#ifdef HAVE_NOTHUNKS
231+
, testGroup "issue381"
232+
[ testCase "mapMaybe" issue381mapMaybe
233+
, testCase "mapMaybeWithKey" issue381mapMaybeWithKey
234+
]
235+
#endif
209236
]

0 commit comments

Comments
 (0)