Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data.TypeRepMap.toList causes segfaulting #64

Closed
meditans opened this issue Jan 21, 2019 · 6 comments · Fixed by #65
Closed

Data.TypeRepMap.toList causes segfaulting #64

meditans opened this issue Jan 21, 2019 · 6 comments · Fixed by #65
Labels

Comments

@meditans
Copy link

Hi,
I am experiencing a bug when writing some code using Data.TypeRepMap (version 0.3.0 on hackage, ghc version 8.6.3); I've distilled the problem to its essence below (load this file in ghci and read the comments):

{-# language ConstraintKinds, GADTs #-}

module CrashTypeRepMap where

import Data.TypeRepMap
import GHC.Exts (toList, fromList)

data C a = C a deriving Show

-- Being unsatisfied with the Show instance for TypeRepMap, I want to bundle
-- show constraints in the map
data Constrained c f a = c a => Constrained (f a)
type Structure = TypeRepMap (Constrained Show C)

-- I intend to use this function to do the printing:
helper :: WrapTypeable (Constrained Show C) -> String
helper (WrapTypeable (Constrained c)) = show c

-- An example of structure I'll use in the repl below
ex_structure :: Structure
ex_structure = fromList
  [ WrapTypeable (Constrained (C 3   :: C Int))
  , WrapTypeable (Constrained (C 'a' :: C Char))]

Now let's test some expressions in the repl:

-- >>> :t ex_structure
-- ex_structure :: Structure

-- >>> toList ex_structure
-- [ e8fa2f3ef0863b7b3e43faa8f94caea7
-- , e8fa2f3ef0863b7b3e43faa8f94caea7
-- ]

Aside question: why are these two fingerprint equal? Is this normal?

You can calculate the type of this expression, but evaluating it crashes ghci:

-- >>> :t map helper (toList ex_structure)
-- map helper (toList ex_structure) :: [String]

-- >>> map helper (toList ex_structure)
-- CRASHES
-- /tmp/nix-shell-24560-0/rc: line 1: 25210 Segmentation fault      ghci

Do you know what causes this problem? Did I not understand how toList/fromList were supposed to be used?

@int-index int-index added the bug label Jan 21, 2019
@int-index
Copy link
Collaborator

Aside question: why are these two fingerprint equal? Is this normal?

No, it's not. I would expect the fingerprints to match those of Int and Char:

ghci> typeRepFingerprint (typeRep @Int)
b1460030427ac0fa458cbf347f168b53
ghci> typeRepFingerprint (typeRep @Char)
913d973ca4cebc0b4829aec8318f79e1

I don't know where e8fa2f3ef0863b7b3e43faa8f94caea7 is coming from.

@int-index
Copy link
Collaborator

int-index commented Jan 21, 2019

Smaller example:

module CrashTypeRepMap where

import Data.TypeRepMap
import Data.Functor.Identity
import GHC.Exts (toList, fromList)

ex_list :: [WrapTypeable Identity]
ex_list = [ WrapTypeable (Identity (3 :: Int)), WrapTypeable (Identity 'a')]

ex_structure :: TypeRepMap Identity
ex_structure = fromList ex_list

Evaluating toList ex_structure segfaults.

@meditans
Copy link
Author

Right, don't you find interesting that in your example you can't even say toList ex_structure, while in mine toList ex_structure prints out the wrong hashes? What's the difference? I'm playing around with typeRepFingerprint (typeRep @Stuff), but I had no luck in understanding where that hash comes from.

@int-index
Copy link
Collaborator

@meditans Check if #65 fixes it for you, the implementation of toList unsafe coerced to WrapTypeable instead of the element type.

@meditans
Copy link
Author

@int-index I tried that pull request both in the example problems and my original code, and it fixed the crashes I was having. Thanks for the speed in reviewing this issue, you may close it!

@chshersh
Copy link
Contributor

@int-index Thanks for fixing the issue so fast!
@vrom911 Thank you very much for reviewing and preparing new release!
@meditans Thank you for reporting this bug! I've just uploaded typerep-map-0.3.1 with bugfix to Hackage:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants