Skip to content

GHC API `runStmt` overrides qualified import of `it` variable

The runStmt function in InteractiveEval creates an it variable storing the last result. However, this variable somehow shadows qualified variables with the name it. For instance, importing Test.Hspec, running any statement, and then trying to use Test.Hspec.it (qualified) results in an "out of scope" error.

The following small program demonstrates this:

import GHC
import GhcMonad
import Outputable
import GHC.Paths

main = runGhc (Just libdir) $ do
  flags <- getSessionDynFlags
  setSessionDynFlags (flags{ hscTarget = HscInterpreted, ghcLink = LinkInMemory})
  imps <- mapM parseImportDecl ["import Prelude", "import Test.Hspec"]
  setContext (map IIDecl imps)

  -- With the next line, you get an "Not in scope" exception.
  -- If you comment out this runStmt, it runs without error and prints the type.
  runStmt "3" RunToCompletion

  exprType "Test.Hspec.it" >>= (liftIO . putStrLn . showPpr flags)

GHCi somehow avoids this, but I have no idea how and could not figure it out from the sources.

What's going on?

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