Skip to content

Commit f721b9d

Browse files
authored
Strict.alterFEager: Fix strictness (#384)
Fixes #383.
1 parent 26a1c33 commit f721b9d

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Data/HashMap/Internal/Strict.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,13 @@ alterFEager f !k !m = (<$> f mv) $ \fres ->
401401

402402
------------------------------
403403
-- Update value
404-
Just v' -> case lookupRes of
404+
Just !v' -> case lookupRes of
405405

406406
-- Key did not exist before, insert v' under a new key
407407
Absent -> insertNewKey h k v' m
408408

409409
-- Key existed before, no hash collision
410-
Present v collPos -> v' `seq`
410+
Present v collPos ->
411411
if v `ptrEq` v'
412412
-- If the value is identical, no-op
413413
then m

tests/Regressions.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE MagicHash #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
4+
{-# LANGUAGE TypeApplications #-}
45
{-# LANGUAGE UnboxedTuples #-}
56
module Regressions (tests) where
67

@@ -11,6 +12,7 @@ import Data.List (delete)
1112
import Data.Maybe (isJust, isNothing)
1213
import GHC.Exts (touch#)
1314
import GHC.IO (IO (..))
15+
import Numeric.Natural (Natural)
1416
import System.Mem (performGC)
1517
import System.Mem.Weak (deRefWeak, mkWeakPtr)
1618
import System.Random (randomIO)
@@ -225,6 +227,27 @@ issue382 = do
225227
touch v -- makes sure that we didn't GC away the combined value
226228
assert $ isNothing res
227229

230+
------------------------------------------------------------------------
231+
-- Issue #383
232+
233+
#ifdef HAVE_NOTHUNKS
234+
235+
-- Custom Functor to prevent interference from alterF rules
236+
newtype MyIdentity a = MyIdentity a
237+
instance Functor MyIdentity where
238+
fmap f (MyIdentity x) = MyIdentity (f x)
239+
240+
issue383 :: Assertion
241+
issue383 = do
242+
i :: Int <- randomIO
243+
let f Nothing = MyIdentity (Just (fromIntegral @Int @Natural (abs i)))
244+
f Just{} = MyIdentity (error "Impossible")
245+
let (MyIdentity m) = HMS.alterF f () mempty
246+
mThunkInfo <- noThunksInValues mempty (Foldable.toList m)
247+
assert $ isNothing mThunkInfo
248+
249+
#endif
250+
228251
------------------------------------------------------------------------
229252
-- * Test list
230253

@@ -251,4 +274,7 @@ tests = testGroup "Regression tests"
251274
]
252275
#endif
253276
, testCase "issue382" issue382
277+
#ifdef HAVE_NOTHUNKS
278+
, testCase "issue383" issue383
279+
#endif
254280
]

0 commit comments

Comments
 (0)