Opened 16 years ago

Closed 16 years ago

Last modified 48 years ago

#578 closed bug (Fixed)

fromDynamic crashes ghci

Reported by: apfeffer Owned by: nobody
Priority: normal Milestone:
Component: None Version: None
Keywords: Cc:
Operating System: Architecture:
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Loading the following code into ghci and evaluating x
causes a segmentation fault:

data Foo a = Foo a

instance Typeable (Foo a) where
  typeOf x = mkAppTy (mkTyCon "BAD") []

x :: Maybe (Foo Char)
x = fromDynamic (toDyn (Foo "abc"))

This is not a problem for ghc.

The version of ghci is 5.00.2

I'm running on linux, not sure what vintage I'm afraid.

By the way, the reason I wrote this code was because I
was trying to figure out what role the type string
plays in the Dynamic library.  The documentation seems
to indicate that it is checked for equality every time
fromDyn or fromDynamic is called, but that doesn't seem
to be the case.  More documentation on what the string
does, and how to construct it correctly, would be
appreciated.  I was trying to figure out if I could
make this type Typeable:

data Name a = String

Change History (1)

comment:1 Changed 16 years ago by simonmar

Status: assignedclosed
Logged In: YES 

Your instance of Typeable for Foo is incorrect, with the 
result that fromDynamic is wrongly returning a successful 
result when called from 'x' in the example code, leading to 
a crash.  Note that the type signature on 'x' is wrong, it 
should be 'Maybe (Foo String)'.

The instance should look something like:

fooTc = mkTyCon "Foo"
instance Typeable a => Typeable (Foo a) where
   typeOf foo = mkAppTc fooTc 
                  [(undefined :: Foo a -> a) foo]

Note that fooTc must be defined at the top level.

OUr documentation for Dynamic is sadly lacking; that's the 
real bug!

Note: See TracTickets for help on using tickets.