Skip to content

dynCompileExpr breaks repeated runDecls of the same name

Using dynCompileExpr with runDecls from InteractiveEval seems to be incredibly problematic when evaluating declarations for the same name. For instance, consider the following code:

  runDecls "data X = Y Int"
  gtry $ runStmt "print (Y 3)" RunToCompletion :: GhcMonad m => m (Either SomeException RunResult)
  runDecls "data X = Y Int deriving Show"
  runStmt "print (Y 8)" RunToCompletion

As expected, this prints Y 8 once (the first one fails because X is not an instance of Show).

However, if we insert an arbitrary dynCompileExpr, things begin to break:

  runDecls "data X = Y Int"
  gtry $ runStmt "print (Y 3)" RunToCompletion :: GhcMonad m => m (Either SomeException RunResult)
  runDecls "data X = Y Int deriving Show"
  _ <- dynCompileExpr "'x'"
  runStmt "print (Y 8)" RunToCompletion

We then get:

No instance for (GHC.Show.Show :Interactive.X)
  arising from a use of `System.IO.print'
Possible fix:
  add an instance declaration for (GHC.Show.Show :Interactive.X)

Which is clearly incorrect - we haven't done anything to change the data declaration.

I'm not sure what's going on, but I've tried to investigate into the source of dynCompileExpr. It loads Data.Dynamic via a setContext and then unloads it, and that may be the issue.

If we do the following (very similar to the dynCompileExpr source), things work fine:

  runDecls "data X = Y Int"
  gtry $ runStmt "print (Y 3)" RunToCompletion :: GhcMonad m => m (Either SomeException RunResult)
  runDecls "data X = Y Int deriving Show"

  let stmt = "let __expr = 'x'"
  Just (ids, hval, fixenv) <- withSession $ \hsc_env -> 
                          liftIO $ hscStmt hsc_env stmt
  vals <- liftIO (unsafeCoerce hval :: IO [Char])
  liftIO $ print vals -- thish prints "x", as expected

  runStmt "print (Y 8)" RunToCompletion

I have not tested with GHC API other than 7.6.3, and could not find a bug that was similar to this.

Trac metadata
Trac field Value
Version 7.6.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component GHC API
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information