Skip to content

Commit

Permalink
fix: hash is not invalidate when GHC internal change
Browse files Browse the repository at this point in the history
Close tweag#202

- GHC internal fingerprint computation may change, leading to an
  invalidation of (some) of the hash computed. Instead of using the type
  fingerprint, we only use the `show $ typeRef x` (which is roughly a
  string representation of the type). It will be more stable and should
  discriminate enough.
  • Loading branch information
guibou committed Sep 1, 2023
1 parent 04dfed3 commit 239bbc1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cas/hashable/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ main = do

-- This hash should not change, unless the hashing algorithm change or the order of the walk in the structure changes.
let
expected = "ContentHash \"7f953cc9aaf79bac4d02e70dca91da1ee630b4a7894aeca9ed61942cc3a20d8a\""
expected = "ContentHash \"da33d38419fc15489a5435694a5107f07fd62b4659ddd799720f62b9e423e3a0\""
when (show rawHash /= expected) $ error $ "Raw hash is different than expected: " <> show rawHash
10 changes: 7 additions & 3 deletions cas/hashable/src/Data/CAS/ContentHashable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,15 @@ instance (Constructor c, GContentHashable m f) => GContentHashable m (C1 c f) wh
nameCtx = hashUpdate ctx0 $ C8.pack (conName x)

instance (Datatype d, GContentHashable m f) => GContentHashable m (D1 d f) where
gContentHashUpdate ctx0 x = packageCtx `seq` gContentHashUpdate packageCtx (unM1 x)
gContentHashUpdate ctx0 x = datatypeCtx `seq` gContentHashUpdate datatypeCtx (unM1 x)
where
-- Only add the datatype context in the hash
-- Adding the module and package would lead to hash invalidation when
-- moving the type to another module
-- See https://github.com/tweag/funflow/issues/202
datatypeCtx = hashUpdate ctx0 $ C8.pack (datatypeName x)
moduleCtx = hashUpdate datatypeCtx $ C8.pack (datatypeName x)
packageCtx = hashUpdate moduleCtx $ C8.pack (datatypeName x)
-- moduleCtx = hashUpdate datatypeCtx $ C8.pack (datatypeName x)
-- packageCtx = hashUpdate moduleCtx $ C8.pack (datatypeName x)

instance GContentHashable m f => GContentHashable m (S1 s f) where
gContentHashUpdate ctx x = gContentHashUpdate ctx (unM1 x)
Expand Down

0 comments on commit 239bbc1

Please sign in to comment.