PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ghc-7.0.4/compiler/typecheck/TcRnMonad.lhs

http://picorec.googlecode.com/
Haskell | 1195 lines | 881 code | 208 blank | 106 comment | 16 complexity | 11d749096ba53d5266fa5519cf7c6bda MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  1. %
  2. % (c) The University of Glasgow 2006
  3. %
  4. \begin{code}
  5. module TcRnMonad(
  6. module TcRnMonad,
  7. module TcRnTypes,
  8. module IOEnv
  9. ) where
  10. #include "HsVersions.h"
  11. import TcRnTypes -- Re-export all
  12. import IOEnv -- Re-export all
  13. import HsSyn hiding (LIE)
  14. import HscTypes
  15. import Module
  16. import RdrName
  17. import Name
  18. import TcType
  19. import InstEnv
  20. import FamInstEnv
  21. import Var
  22. import Id
  23. import VarSet
  24. import VarEnv
  25. import ErrUtils
  26. import SrcLoc
  27. import NameEnv
  28. import NameSet
  29. import Bag
  30. import Outputable
  31. import UniqSupply
  32. import Unique
  33. import UniqFM
  34. import DynFlags
  35. import StaticFlags
  36. import FastString
  37. import Panic
  38. import Util
  39. import System.IO
  40. import Data.IORef
  41. import qualified Data.Set as Set
  42. import Control.Monad
  43. \end{code}
  44. %************************************************************************
  45. %* *
  46. initTc
  47. %* *
  48. %************************************************************************
  49. \begin{code}
  50. initTc :: HscEnv
  51. -> HscSource
  52. -> Bool -- True <=> retain renamed syntax trees
  53. -> Module
  54. -> TcM r
  55. -> IO (Messages, Maybe r)
  56. -- Nothing => error thrown by the thing inside
  57. -- (error messages should have been printed already)
  58. initTc hsc_env hsc_src keep_rn_syntax mod do_this
  59. = do { errs_var <- newIORef (emptyBag, emptyBag) ;
  60. meta_var <- newIORef initTyVarUnique ;
  61. tvs_var <- newIORef emptyVarSet ;
  62. keep_var <- newIORef emptyNameSet ;
  63. used_rdr_var <- newIORef Set.empty ;
  64. th_var <- newIORef False ;
  65. lie_var <- newIORef emptyWC ;
  66. dfun_n_var <- newIORef emptyOccSet ;
  67. type_env_var <- case hsc_type_env_var hsc_env of {
  68. Just (_mod, te_var) -> return te_var ;
  69. Nothing -> newIORef emptyNameEnv } ;
  70. let {
  71. maybe_rn_syntax :: forall a. a -> Maybe a ;
  72. maybe_rn_syntax empty_val
  73. | keep_rn_syntax = Just empty_val
  74. | otherwise = Nothing ;
  75. gbl_env = TcGblEnv {
  76. tcg_mod = mod,
  77. tcg_src = hsc_src,
  78. tcg_rdr_env = emptyGlobalRdrEnv,
  79. tcg_fix_env = emptyNameEnv,
  80. tcg_field_env = RecFields emptyNameEnv emptyNameSet,
  81. tcg_default = Nothing,
  82. tcg_type_env = emptyNameEnv,
  83. tcg_type_env_var = type_env_var,
  84. tcg_inst_env = emptyInstEnv,
  85. tcg_fam_inst_env = emptyFamInstEnv,
  86. tcg_th_used = th_var,
  87. tcg_exports = [],
  88. tcg_imports = emptyImportAvails,
  89. tcg_used_rdrnames = used_rdr_var,
  90. tcg_dus = emptyDUs,
  91. tcg_rn_imports = [],
  92. tcg_rn_exports = maybe_rn_syntax [],
  93. tcg_rn_decls = maybe_rn_syntax emptyRnGroup,
  94. tcg_binds = emptyLHsBinds,
  95. tcg_imp_specs = [],
  96. tcg_sigs = emptyNameSet,
  97. tcg_ev_binds = emptyBag,
  98. tcg_warns = NoWarnings,
  99. tcg_anns = [],
  100. tcg_insts = [],
  101. tcg_fam_insts = [],
  102. tcg_rules = [],
  103. tcg_fords = [],
  104. tcg_dfun_n = dfun_n_var,
  105. tcg_keep = keep_var,
  106. tcg_doc_hdr = Nothing,
  107. tcg_hpc = False,
  108. tcg_main = Nothing
  109. } ;
  110. lcl_env = TcLclEnv {
  111. tcl_errs = errs_var,
  112. tcl_loc = mkGeneralSrcSpan (fsLit "Top level"),
  113. tcl_ctxt = [],
  114. tcl_rdr = emptyLocalRdrEnv,
  115. tcl_th_ctxt = topStage,
  116. tcl_arrow_ctxt = NoArrowCtxt,
  117. tcl_env = emptyNameEnv,
  118. tcl_tyvars = tvs_var,
  119. tcl_lie = lie_var,
  120. tcl_meta = meta_var,
  121. tcl_untch = initTyVarUnique
  122. } ;
  123. } ;
  124. -- OK, here's the business end!
  125. maybe_res <- initTcRnIf 'a' hsc_env gbl_env lcl_env $
  126. do { r <- tryM do_this
  127. ; case r of
  128. Right res -> return (Just res)
  129. Left _ -> return Nothing } ;
  130. -- Check for unsolved constraints
  131. lie <- readIORef lie_var ;
  132. if isEmptyWC lie
  133. then return ()
  134. else pprPanic "initTc: unsolved constraints"
  135. (pprWantedsWithLocs lie) ;
  136. -- Collect any error messages
  137. msgs <- readIORef errs_var ;
  138. let { dflags = hsc_dflags hsc_env
  139. ; final_res | errorsFound dflags msgs = Nothing
  140. | otherwise = maybe_res } ;
  141. return (msgs, final_res)
  142. }
  143. initTcPrintErrors -- Used from the interactive loop only
  144. :: HscEnv
  145. -> Module
  146. -> TcM r
  147. -> IO (Messages, Maybe r)
  148. initTcPrintErrors env mod todo = do
  149. (msgs, res) <- initTc env HsSrcFile False mod todo
  150. return (msgs, res)
  151. \end{code}
  152. %************************************************************************
  153. %* *
  154. Initialisation
  155. %* *
  156. %************************************************************************
  157. \begin{code}
  158. initTcRnIf :: Char -- Tag for unique supply
  159. -> HscEnv
  160. -> gbl -> lcl
  161. -> TcRnIf gbl lcl a
  162. -> IO a
  163. initTcRnIf uniq_tag hsc_env gbl_env lcl_env thing_inside
  164. = do { us <- mkSplitUniqSupply uniq_tag ;
  165. ; us_var <- newIORef us ;
  166. ; let { env = Env { env_top = hsc_env,
  167. env_us = us_var,
  168. env_gbl = gbl_env,
  169. env_lcl = lcl_env} }
  170. ; runIOEnv env thing_inside
  171. }
  172. \end{code}
  173. %************************************************************************
  174. %* *
  175. Simple accessors
  176. %* *
  177. %************************************************************************
  178. \begin{code}
  179. getTopEnv :: TcRnIf gbl lcl HscEnv
  180. getTopEnv = do { env <- getEnv; return (env_top env) }
  181. getGblEnv :: TcRnIf gbl lcl gbl
  182. getGblEnv = do { env <- getEnv; return (env_gbl env) }
  183. updGblEnv :: (gbl -> gbl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
  184. updGblEnv upd = updEnv (\ env@(Env { env_gbl = gbl }) ->
  185. env { env_gbl = upd gbl })
  186. setGblEnv :: gbl -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
  187. setGblEnv gbl_env = updEnv (\ env -> env { env_gbl = gbl_env })
  188. getLclEnv :: TcRnIf gbl lcl lcl
  189. getLclEnv = do { env <- getEnv; return (env_lcl env) }
  190. updLclEnv :: (lcl -> lcl) -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
  191. updLclEnv upd = updEnv (\ env@(Env { env_lcl = lcl }) ->
  192. env { env_lcl = upd lcl })
  193. setLclEnv :: lcl' -> TcRnIf gbl lcl' a -> TcRnIf gbl lcl a
  194. setLclEnv lcl_env = updEnv (\ env -> env { env_lcl = lcl_env })
  195. getEnvs :: TcRnIf gbl lcl (gbl, lcl)
  196. getEnvs = do { env <- getEnv; return (env_gbl env, env_lcl env) }
  197. setEnvs :: (gbl', lcl') -> TcRnIf gbl' lcl' a -> TcRnIf gbl lcl a
  198. setEnvs (gbl_env, lcl_env) = updEnv (\ env -> env { env_gbl = gbl_env, env_lcl = lcl_env })
  199. \end{code}
  200. Command-line flags
  201. \begin{code}
  202. getDOpts :: TcRnIf gbl lcl DynFlags
  203. getDOpts = do { env <- getTopEnv; return (hsc_dflags env) }
  204. xoptM :: ExtensionFlag -> TcRnIf gbl lcl Bool
  205. xoptM flag = do { dflags <- getDOpts; return (xopt flag dflags) }
  206. doptM :: DynFlag -> TcRnIf gbl lcl Bool
  207. doptM flag = do { dflags <- getDOpts; return (dopt flag dflags) }
  208. -- XXX setOptM and unsetOptM operate on different types. One should be renamed.
  209. setOptM :: ExtensionFlag -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
  210. setOptM flag = updEnv (\ env@(Env { env_top = top }) ->
  211. env { env_top = top { hsc_dflags = xopt_set (hsc_dflags top) flag}} )
  212. unsetOptM :: DynFlag -> TcRnIf gbl lcl a -> TcRnIf gbl lcl a
  213. unsetOptM flag = updEnv (\ env@(Env { env_top = top }) ->
  214. env { env_top = top { hsc_dflags = dopt_unset (hsc_dflags top) flag}} )
  215. -- | Do it flag is true
  216. ifDOptM :: DynFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
  217. ifDOptM flag thing_inside = do { b <- doptM flag;
  218. if b then thing_inside else return () }
  219. ifXOptM :: ExtensionFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
  220. ifXOptM flag thing_inside = do { b <- xoptM flag;
  221. if b then thing_inside else return () }
  222. getGhcMode :: TcRnIf gbl lcl GhcMode
  223. getGhcMode = do { env <- getTopEnv; return (ghcMode (hsc_dflags env)) }
  224. \end{code}
  225. \begin{code}
  226. getEpsVar :: TcRnIf gbl lcl (TcRef ExternalPackageState)
  227. getEpsVar = do { env <- getTopEnv; return (hsc_EPS env) }
  228. getEps :: TcRnIf gbl lcl ExternalPackageState
  229. getEps = do { env <- getTopEnv; readMutVar (hsc_EPS env) }
  230. -- | Update the external package state. Returns the second result of the
  231. -- modifier function.
  232. --
  233. -- This is an atomic operation and forces evaluation of the modified EPS in
  234. -- order to avoid space leaks.
  235. updateEps :: (ExternalPackageState -> (ExternalPackageState, a))
  236. -> TcRnIf gbl lcl a
  237. updateEps upd_fn = do
  238. traceIf (text "updating EPS")
  239. eps_var <- getEpsVar
  240. atomicUpdMutVar' eps_var upd_fn
  241. -- | Update the external package state.
  242. --
  243. -- This is an atomic operation and forces evaluation of the modified EPS in
  244. -- order to avoid space leaks.
  245. updateEps_ :: (ExternalPackageState -> ExternalPackageState)
  246. -> TcRnIf gbl lcl ()
  247. updateEps_ upd_fn = do
  248. traceIf (text "updating EPS_")
  249. eps_var <- getEpsVar
  250. atomicUpdMutVar' eps_var (\eps -> (upd_fn eps, ()))
  251. getHpt :: TcRnIf gbl lcl HomePackageTable
  252. getHpt = do { env <- getTopEnv; return (hsc_HPT env) }
  253. getEpsAndHpt :: TcRnIf gbl lcl (ExternalPackageState, HomePackageTable)
  254. getEpsAndHpt = do { env <- getTopEnv; eps <- readMutVar (hsc_EPS env)
  255. ; return (eps, hsc_HPT env) }
  256. \end{code}
  257. %************************************************************************
  258. %* *
  259. Unique supply
  260. %* *
  261. %************************************************************************
  262. \begin{code}
  263. newMetaUnique :: TcM Unique
  264. -- The uniques for TcMetaTyVars are allocated specially
  265. -- in guaranteed linear order, starting at zero for each module
  266. newMetaUnique
  267. = do { env <- getLclEnv
  268. ; let meta_var = tcl_meta env
  269. ; uniq <- readMutVar meta_var
  270. ; writeMutVar meta_var (incrUnique uniq)
  271. ; return uniq }
  272. newUnique :: TcRnIf gbl lcl Unique
  273. newUnique
  274. = do { env <- getEnv ;
  275. let { u_var = env_us env } ;
  276. us <- readMutVar u_var ;
  277. case splitUniqSupply us of { (us1,_) -> do {
  278. writeMutVar u_var us1 ;
  279. return $! uniqFromSupply us }}}
  280. -- NOTE 1: we strictly split the supply, to avoid the possibility of leaving
  281. -- a chain of unevaluated supplies behind.
  282. -- NOTE 2: we use the uniq in the supply from the MutVar directly, and
  283. -- throw away one half of the new split supply. This is safe because this
  284. -- is the only place we use that unique. Using the other half of the split
  285. -- supply is safer, but slower.
  286. newUniqueSupply :: TcRnIf gbl lcl UniqSupply
  287. newUniqueSupply
  288. = do { env <- getEnv ;
  289. let { u_var = env_us env } ;
  290. us <- readMutVar u_var ;
  291. case splitUniqSupply us of { (us1,us2) -> do {
  292. writeMutVar u_var us1 ;
  293. return us2 }}}
  294. newLocalName :: Name -> TcRnIf gbl lcl Name
  295. newLocalName name -- Make a clone
  296. = do { uniq <- newUnique
  297. ; return (mkInternalName uniq (nameOccName name) (getSrcSpan name)) }
  298. newSysLocalIds :: FastString -> [TcType] -> TcRnIf gbl lcl [TcId]
  299. newSysLocalIds fs tys
  300. = do { us <- newUniqueSupply
  301. ; return (zipWith (mkSysLocal fs) (uniqsFromSupply us) tys) }
  302. instance MonadUnique (IOEnv (Env gbl lcl)) where
  303. getUniqueM = newUnique
  304. getUniqueSupplyM = newUniqueSupply
  305. \end{code}
  306. %************************************************************************
  307. %* *
  308. Debugging
  309. %* *
  310. %************************************************************************
  311. \begin{code}
  312. newTcRef :: a -> TcRnIf gbl lcl (TcRef a)
  313. newTcRef = newMutVar
  314. readTcRef :: TcRef a -> TcRnIf gbl lcl a
  315. readTcRef = readMutVar
  316. writeTcRef :: TcRef a -> a -> TcRnIf gbl lcl ()
  317. writeTcRef = writeMutVar
  318. updTcRef :: TcRef a -> (a -> a) -> TcRnIf gbl lcl ()
  319. updTcRef = updMutVar
  320. \end{code}
  321. %************************************************************************
  322. %* *
  323. Debugging
  324. %* *
  325. %************************************************************************
  326. \begin{code}
  327. traceTc :: String -> SDoc -> TcRn ()
  328. traceTc = traceTcN 1
  329. traceTcN :: Int -> String -> SDoc -> TcRn ()
  330. traceTcN level herald doc
  331. | level <= opt_TraceLevel = traceOptTcRn Opt_D_dump_tc_trace $
  332. hang (text herald) 2 doc
  333. | otherwise = return ()
  334. traceRn, traceSplice :: SDoc -> TcRn ()
  335. traceRn = traceOptTcRn Opt_D_dump_rn_trace
  336. traceSplice = traceOptTcRn Opt_D_dump_splices
  337. traceIf, traceHiDiffs :: SDoc -> TcRnIf m n ()
  338. traceIf = traceOptIf Opt_D_dump_if_trace
  339. traceHiDiffs = traceOptIf Opt_D_dump_hi_diffs
  340. traceOptIf :: DynFlag -> SDoc -> TcRnIf m n () -- No RdrEnv available, so qualify everything
  341. traceOptIf flag doc = ifDOptM flag $
  342. liftIO (printForUser stderr alwaysQualify doc)
  343. traceOptTcRn :: DynFlag -> SDoc -> TcRn ()
  344. -- Output the message, with current location if opt_PprStyle_Debug
  345. traceOptTcRn flag doc = ifDOptM flag $ do
  346. { loc <- getSrcSpanM
  347. ; let real_doc
  348. | opt_PprStyle_Debug = mkLocMessage loc doc
  349. | otherwise = doc -- The full location is
  350. -- usually way too much
  351. ; dumpTcRn real_doc }
  352. dumpTcRn :: SDoc -> TcRn ()
  353. dumpTcRn doc = do { rdr_env <- getGlobalRdrEnv
  354. ; dflags <- getDOpts
  355. ; liftIO (printForUser stderr (mkPrintUnqualified dflags rdr_env) doc) }
  356. debugDumpTcRn :: SDoc -> TcRn ()
  357. debugDumpTcRn doc | opt_NoDebugOutput = return ()
  358. | otherwise = dumpTcRn doc
  359. dumpOptTcRn :: DynFlag -> SDoc -> TcRn ()
  360. dumpOptTcRn flag doc = ifDOptM flag (dumpTcRn doc)
  361. \end{code}
  362. %************************************************************************
  363. %* *
  364. Typechecker global environment
  365. %* *
  366. %************************************************************************
  367. \begin{code}
  368. getModule :: TcRn Module
  369. getModule = do { env <- getGblEnv; return (tcg_mod env) }
  370. setModule :: Module -> TcRn a -> TcRn a
  371. setModule mod thing_inside = updGblEnv (\env -> env { tcg_mod = mod }) thing_inside
  372. tcIsHsBoot :: TcRn Bool
  373. tcIsHsBoot = do { env <- getGblEnv; return (isHsBoot (tcg_src env)) }
  374. getGlobalRdrEnv :: TcRn GlobalRdrEnv
  375. getGlobalRdrEnv = do { env <- getGblEnv; return (tcg_rdr_env env) }
  376. getRdrEnvs :: TcRn (GlobalRdrEnv, LocalRdrEnv)
  377. getRdrEnvs = do { (gbl,lcl) <- getEnvs; return (tcg_rdr_env gbl, tcl_rdr lcl) }
  378. getImports :: TcRn ImportAvails
  379. getImports = do { env <- getGblEnv; return (tcg_imports env) }
  380. getFixityEnv :: TcRn FixityEnv
  381. getFixityEnv = do { env <- getGblEnv; return (tcg_fix_env env) }
  382. extendFixityEnv :: [(Name,FixItem)] -> RnM a -> RnM a
  383. extendFixityEnv new_bit
  384. = updGblEnv (\env@(TcGblEnv { tcg_fix_env = old_fix_env }) ->
  385. env {tcg_fix_env = extendNameEnvList old_fix_env new_bit})
  386. getRecFieldEnv :: TcRn RecFieldEnv
  387. getRecFieldEnv = do { env <- getGblEnv; return (tcg_field_env env) }
  388. getDeclaredDefaultTys :: TcRn (Maybe [Type])
  389. getDeclaredDefaultTys = do { env <- getGblEnv; return (tcg_default env) }
  390. \end{code}
  391. %************************************************************************
  392. %* *
  393. Error management
  394. %* *
  395. %************************************************************************
  396. \begin{code}
  397. getSrcSpanM :: TcRn SrcSpan
  398. -- Avoid clash with Name.getSrcLoc
  399. getSrcSpanM = do { env <- getLclEnv; return (tcl_loc env) }
  400. setSrcSpan :: SrcSpan -> TcRn a -> TcRn a
  401. setSrcSpan loc thing_inside
  402. | isGoodSrcSpan loc = updLclEnv (\env -> env { tcl_loc = loc }) thing_inside
  403. | otherwise = thing_inside -- Don't overwrite useful info with useless
  404. addLocM :: (a -> TcM b) -> Located a -> TcM b
  405. addLocM fn (L loc a) = setSrcSpan loc $ fn a
  406. wrapLocM :: (a -> TcM b) -> Located a -> TcM (Located b)
  407. wrapLocM fn (L loc a) = setSrcSpan loc $ do b <- fn a; return (L loc b)
  408. wrapLocFstM :: (a -> TcM (b,c)) -> Located a -> TcM (Located b, c)
  409. wrapLocFstM fn (L loc a) =
  410. setSrcSpan loc $ do
  411. (b,c) <- fn a
  412. return (L loc b, c)
  413. wrapLocSndM :: (a -> TcM (b,c)) -> Located a -> TcM (b, Located c)
  414. wrapLocSndM fn (L loc a) =
  415. setSrcSpan loc $ do
  416. (b,c) <- fn a
  417. return (b, L loc c)
  418. \end{code}
  419. Reporting errors
  420. \begin{code}
  421. getErrsVar :: TcRn (TcRef Messages)
  422. getErrsVar = do { env <- getLclEnv; return (tcl_errs env) }
  423. setErrsVar :: TcRef Messages -> TcRn a -> TcRn a
  424. setErrsVar v = updLclEnv (\ env -> env { tcl_errs = v })
  425. addErr :: Message -> TcRn () -- Ignores the context stack
  426. addErr msg = do { loc <- getSrcSpanM ; addErrAt loc msg }
  427. failWith :: Message -> TcRn a
  428. failWith msg = addErr msg >> failM
  429. addErrAt :: SrcSpan -> Message -> TcRn ()
  430. -- addErrAt is mainly (exclusively?) used by the renamer, where
  431. -- tidying is not an issue, but it's all lazy so the extra
  432. -- work doesn't matter
  433. addErrAt loc msg = do { ctxt <- getErrCtxt
  434. ; tidy_env <- tcInitTidyEnv
  435. ; err_info <- mkErrInfo tidy_env ctxt
  436. ; addLongErrAt loc msg err_info }
  437. addErrs :: [(SrcSpan,Message)] -> TcRn ()
  438. addErrs msgs = mapM_ add msgs
  439. where
  440. add (loc,msg) = addErrAt loc msg
  441. addWarn :: Message -> TcRn ()
  442. addWarn msg = addReport (ptext (sLit "Warning:") <+> msg) empty
  443. addWarnAt :: SrcSpan -> Message -> TcRn ()
  444. addWarnAt loc msg = addReportAt loc (ptext (sLit "Warning:") <+> msg) empty
  445. checkErr :: Bool -> Message -> TcRn ()
  446. -- Add the error if the bool is False
  447. checkErr ok msg = unless ok (addErr msg)
  448. warnIf :: Bool -> Message -> TcRn ()
  449. warnIf True msg = addWarn msg
  450. warnIf False _ = return ()
  451. addMessages :: Messages -> TcRn ()
  452. addMessages (m_warns, m_errs)
  453. = do { errs_var <- getErrsVar ;
  454. (warns, errs) <- readTcRef errs_var ;
  455. writeTcRef errs_var (warns `unionBags` m_warns,
  456. errs `unionBags` m_errs) }
  457. discardWarnings :: TcRn a -> TcRn a
  458. -- Ignore warnings inside the thing inside;
  459. -- used to ignore-unused-variable warnings inside derived code
  460. -- With -dppr-debug, the effects is switched off, so you can still see
  461. -- what warnings derived code would give
  462. discardWarnings thing_inside
  463. | opt_PprStyle_Debug = thing_inside
  464. | otherwise
  465. = do { errs_var <- newTcRef emptyMessages
  466. ; result <- setErrsVar errs_var thing_inside
  467. ; (_warns, errs) <- readTcRef errs_var
  468. ; addMessages (emptyBag, errs)
  469. ; return result }
  470. \end{code}
  471. %************************************************************************
  472. %* *
  473. Shared error message stuff: renamer and typechecker
  474. %* *
  475. %************************************************************************
  476. \begin{code}
  477. addReport :: Message -> Message -> TcRn ()
  478. addReport msg extra_info = do loc <- getSrcSpanM; addReportAt loc msg extra_info
  479. addReportAt :: SrcSpan -> Message -> Message -> TcRn ()
  480. addReportAt loc msg extra_info
  481. = do { errs_var <- getErrsVar ;
  482. rdr_env <- getGlobalRdrEnv ;
  483. dflags <- getDOpts ;
  484. let { warn = mkLongWarnMsg loc (mkPrintUnqualified dflags rdr_env)
  485. msg extra_info } ;
  486. (warns, errs) <- readTcRef errs_var ;
  487. writeTcRef errs_var (warns `snocBag` warn, errs) }
  488. addLongErrAt :: SrcSpan -> Message -> Message -> TcRn ()
  489. addLongErrAt loc msg extra
  490. = do { traceTc "Adding error:" (mkLocMessage loc (msg $$ extra)) ;
  491. errs_var <- getErrsVar ;
  492. rdr_env <- getGlobalRdrEnv ;
  493. dflags <- getDOpts ;
  494. let { err = mkLongErrMsg loc (mkPrintUnqualified dflags rdr_env) msg extra } ;
  495. (warns, errs) <- readTcRef errs_var ;
  496. writeTcRef errs_var (warns, errs `snocBag` err) }
  497. dumpDerivingInfo :: SDoc -> TcM ()
  498. dumpDerivingInfo doc
  499. = do { dflags <- getDOpts
  500. ; when (dopt Opt_D_dump_deriv dflags) $ do
  501. { rdr_env <- getGlobalRdrEnv
  502. ; let unqual = mkPrintUnqualified dflags rdr_env
  503. ; liftIO (putMsgWith dflags unqual doc) } }
  504. \end{code}
  505. \begin{code}
  506. try_m :: TcRn r -> TcRn (Either IOEnvFailure r)
  507. -- Does try_m, with a debug-trace on failure
  508. try_m thing
  509. = do { mb_r <- tryM thing ;
  510. case mb_r of
  511. Left exn -> do { traceTc "tryTc/recoverM recovering from" $
  512. text (showException exn)
  513. ; return mb_r }
  514. Right _ -> return mb_r }
  515. -----------------------
  516. recoverM :: TcRn r -- Recovery action; do this if the main one fails
  517. -> TcRn r -- Main action: do this first
  518. -> TcRn r
  519. -- Errors in 'thing' are retained
  520. recoverM recover thing
  521. = do { mb_res <- try_m thing ;
  522. case mb_res of
  523. Left _ -> recover
  524. Right res -> return res }
  525. -----------------------
  526. mapAndRecoverM :: (a -> TcRn b) -> [a] -> TcRn [b]
  527. -- Drop elements of the input that fail, so the result
  528. -- list can be shorter than the argument list
  529. mapAndRecoverM _ [] = return []
  530. mapAndRecoverM f (x:xs) = do { mb_r <- try_m (f x)
  531. ; rs <- mapAndRecoverM f xs
  532. ; return (case mb_r of
  533. Left _ -> rs
  534. Right r -> r:rs) }
  535. -----------------------
  536. tryTc :: TcRn a -> TcRn (Messages, Maybe a)
  537. -- (tryTc m) executes m, and returns
  538. -- Just r, if m succeeds (returning r)
  539. -- Nothing, if m fails
  540. -- It also returns all the errors and warnings accumulated by m
  541. -- It always succeeds (never raises an exception)
  542. tryTc m
  543. = do { errs_var <- newTcRef emptyMessages ;
  544. res <- try_m (setErrsVar errs_var m) ;
  545. msgs <- readTcRef errs_var ;
  546. return (msgs, case res of
  547. Left _ -> Nothing
  548. Right val -> Just val)
  549. -- The exception is always the IOEnv built-in
  550. -- in exception; see IOEnv.failM
  551. }
  552. -----------------------
  553. tryTcErrs :: TcRn a -> TcRn (Messages, Maybe a)
  554. -- Run the thing, returning
  555. -- Just r, if m succceeds with no error messages
  556. -- Nothing, if m fails, or if it succeeds but has error messages
  557. -- Either way, the messages are returned; even in the Just case
  558. -- there might be warnings
  559. tryTcErrs thing
  560. = do { (msgs, res) <- tryTc thing
  561. ; dflags <- getDOpts
  562. ; let errs_found = errorsFound dflags msgs
  563. ; return (msgs, case res of
  564. Nothing -> Nothing
  565. Just val | errs_found -> Nothing
  566. | otherwise -> Just val)
  567. }
  568. -----------------------
  569. tryTcLIE :: TcM a -> TcM (Messages, Maybe a)
  570. -- Just like tryTcErrs, except that it ensures that the LIE
  571. -- for the thing is propagated only if there are no errors
  572. -- Hence it's restricted to the type-check monad
  573. tryTcLIE thing_inside
  574. = do { ((msgs, mb_res), lie) <- captureConstraints (tryTcErrs thing_inside) ;
  575. ; case mb_res of
  576. Nothing -> return (msgs, Nothing)
  577. Just val -> do { emitConstraints lie; return (msgs, Just val) }
  578. }
  579. -----------------------
  580. tryTcLIE_ :: TcM r -> TcM r -> TcM r
  581. -- (tryTcLIE_ r m) tries m;
  582. -- if m succeeds with no error messages, it's the answer
  583. -- otherwise tryTcLIE_ drops everything from m and tries r instead.
  584. tryTcLIE_ recover main
  585. = do { (msgs, mb_res) <- tryTcLIE main
  586. ; case mb_res of
  587. Just val -> do { addMessages msgs -- There might be warnings
  588. ; return val }
  589. Nothing -> recover -- Discard all msgs
  590. }
  591. -----------------------
  592. checkNoErrs :: TcM r -> TcM r
  593. -- (checkNoErrs m) succeeds iff m succeeds and generates no errors
  594. -- If m fails then (checkNoErrsTc m) fails.
  595. -- If m succeeds, it checks whether m generated any errors messages
  596. -- (it might have recovered internally)
  597. -- If so, it fails too.
  598. -- Regardless, any errors generated by m are propagated to the enclosing context.
  599. checkNoErrs main
  600. = do { (msgs, mb_res) <- tryTcLIE main
  601. ; addMessages msgs
  602. ; case mb_res of
  603. Nothing -> failM
  604. Just val -> return val
  605. }
  606. ifErrsM :: TcRn r -> TcRn r -> TcRn r
  607. -- ifErrsM bale_out main
  608. -- does 'bale_out' if there are errors in errors collection
  609. -- otherwise does 'main'
  610. ifErrsM bale_out normal
  611. = do { errs_var <- getErrsVar ;
  612. msgs <- readTcRef errs_var ;
  613. dflags <- getDOpts ;
  614. if errorsFound dflags msgs then
  615. bale_out
  616. else
  617. normal }
  618. failIfErrsM :: TcRn ()
  619. -- Useful to avoid error cascades
  620. failIfErrsM = ifErrsM failM (return ())
  621. \end{code}
  622. %************************************************************************
  623. %* *
  624. Context management for the type checker
  625. %* *
  626. %************************************************************************
  627. \begin{code}
  628. getErrCtxt :: TcM [ErrCtxt]
  629. getErrCtxt = do { env <- getLclEnv; return (tcl_ctxt env) }
  630. setErrCtxt :: [ErrCtxt] -> TcM a -> TcM a
  631. setErrCtxt ctxt = updLclEnv (\ env -> env { tcl_ctxt = ctxt })
  632. addErrCtxt :: Message -> TcM a -> TcM a
  633. addErrCtxt msg = addErrCtxtM (\env -> return (env, msg))
  634. addErrCtxtM :: (TidyEnv -> TcM (TidyEnv, Message)) -> TcM a -> TcM a
  635. addErrCtxtM ctxt = updCtxt (\ ctxts -> (False, ctxt) : ctxts)
  636. addLandmarkErrCtxt :: Message -> TcM a -> TcM a
  637. addLandmarkErrCtxt msg = updCtxt (\ctxts -> (True, \env -> return (env,msg)) : ctxts)
  638. -- Helper function for the above
  639. updCtxt :: ([ErrCtxt] -> [ErrCtxt]) -> TcM a -> TcM a
  640. updCtxt upd = updLclEnv (\ env@(TcLclEnv { tcl_ctxt = ctxt }) ->
  641. env { tcl_ctxt = upd ctxt })
  642. -- Conditionally add an error context
  643. maybeAddErrCtxt :: Maybe Message -> TcM a -> TcM a
  644. maybeAddErrCtxt (Just msg) thing_inside = addErrCtxt msg thing_inside
  645. maybeAddErrCtxt Nothing thing_inside = thing_inside
  646. popErrCtxt :: TcM a -> TcM a
  647. popErrCtxt = updCtxt (\ msgs -> case msgs of { [] -> []; (_ : ms) -> ms })
  648. getCtLoc :: orig -> TcM (CtLoc orig)
  649. getCtLoc origin
  650. = do { loc <- getSrcSpanM ; env <- getLclEnv ;
  651. return (CtLoc origin loc (tcl_ctxt env)) }
  652. setCtLoc :: CtLoc orig -> TcM a -> TcM a
  653. setCtLoc (CtLoc _ src_loc ctxt) thing_inside
  654. = setSrcSpan src_loc (setErrCtxt ctxt thing_inside)
  655. \end{code}
  656. %************************************************************************
  657. %* *
  658. Error message generation (type checker)
  659. %* *
  660. %************************************************************************
  661. The addErrTc functions add an error message, but do not cause failure.
  662. The 'M' variants pass a TidyEnv that has already been used to
  663. tidy up the message; we then use it to tidy the context messages
  664. \begin{code}
  665. addErrTc :: Message -> TcM ()
  666. addErrTc err_msg = do { env0 <- tcInitTidyEnv
  667. ; addErrTcM (env0, err_msg) }
  668. addErrsTc :: [Message] -> TcM ()
  669. addErrsTc err_msgs = mapM_ addErrTc err_msgs
  670. addErrTcM :: (TidyEnv, Message) -> TcM ()
  671. addErrTcM (tidy_env, err_msg)
  672. = do { ctxt <- getErrCtxt ;
  673. loc <- getSrcSpanM ;
  674. add_err_tcm tidy_env err_msg loc ctxt }
  675. \end{code}
  676. The failWith functions add an error message and cause failure
  677. \begin{code}
  678. failWithTc :: Message -> TcM a -- Add an error message and fail
  679. failWithTc err_msg
  680. = addErrTc err_msg >> failM
  681. failWithTcM :: (TidyEnv, Message) -> TcM a -- Add an error message and fail
  682. failWithTcM local_and_msg
  683. = addErrTcM local_and_msg >> failM
  684. checkTc :: Bool -> Message -> TcM () -- Check that the boolean is true
  685. checkTc True _ = return ()
  686. checkTc False err = failWithTc err
  687. \end{code}
  688. Warnings have no 'M' variant, nor failure
  689. \begin{code}
  690. addWarnTc :: Message -> TcM ()
  691. addWarnTc msg = do { env0 <- tcInitTidyEnv
  692. ; addWarnTcM (env0, msg) }
  693. addWarnTcM :: (TidyEnv, Message) -> TcM ()
  694. addWarnTcM (env0, msg)
  695. = do { ctxt <- getErrCtxt ;
  696. err_info <- mkErrInfo env0 ctxt ;
  697. addReport (ptext (sLit "Warning:") <+> msg) err_info }
  698. warnTc :: Bool -> Message -> TcM ()
  699. warnTc warn_if_true warn_msg
  700. | warn_if_true = addWarnTc warn_msg
  701. | otherwise = return ()
  702. \end{code}
  703. -----------------------------------
  704. Tidying
  705. We initialise the "tidy-env", used for tidying types before printing,
  706. by building a reverse map from the in-scope type variables to the
  707. OccName that the programmer originally used for them
  708. \begin{code}
  709. tcInitTidyEnv :: TcM TidyEnv
  710. tcInitTidyEnv
  711. = do { lcl_env <- getLclEnv
  712. ; let nm_tv_prs = [ (name, tcGetTyVar "tcInitTidyEnv" ty)
  713. | ATyVar name ty <- nameEnvElts (tcl_env lcl_env)
  714. , tcIsTyVarTy ty ]
  715. ; return (foldl add emptyTidyEnv nm_tv_prs) }
  716. where
  717. add (env,subst) (name, tyvar)
  718. = case tidyOccName env (nameOccName name) of
  719. (env', occ') -> (env', extendVarEnv subst tyvar tyvar')
  720. where
  721. tyvar' = setTyVarName tyvar name'
  722. name' = tidyNameOcc name occ'
  723. \end{code}
  724. -----------------------------------
  725. Other helper functions
  726. \begin{code}
  727. add_err_tcm :: TidyEnv -> Message -> SrcSpan
  728. -> [ErrCtxt]
  729. -> TcM ()
  730. add_err_tcm tidy_env err_msg loc ctxt
  731. = do { err_info <- mkErrInfo tidy_env ctxt ;
  732. addLongErrAt loc err_msg err_info }
  733. mkErrInfo :: TidyEnv -> [ErrCtxt] -> TcM SDoc
  734. -- Tidy the error info, trimming excessive contexts
  735. mkErrInfo env ctxts
  736. = go 0 env ctxts
  737. where
  738. go :: Int -> TidyEnv -> [ErrCtxt] -> TcM SDoc
  739. go _ _ [] = return empty
  740. go n env ((is_landmark, ctxt) : ctxts)
  741. | is_landmark || n < mAX_CONTEXTS -- Too verbose || opt_PprStyle_Debug
  742. = do { (env', msg) <- ctxt env
  743. ; let n' = if is_landmark then n else n+1
  744. ; rest <- go n' env' ctxts
  745. ; return (msg $$ rest) }
  746. | otherwise
  747. = go n env ctxts
  748. mAX_CONTEXTS :: Int -- No more than this number of non-landmark contexts
  749. mAX_CONTEXTS = 3
  750. \end{code}
  751. debugTc is useful for monadic debugging code
  752. \begin{code}
  753. debugTc :: TcM () -> TcM ()
  754. debugTc thing
  755. | debugIsOn = thing
  756. | otherwise = return ()
  757. \end{code}
  758. %************************************************************************
  759. %* *
  760. Type constraints
  761. %* *
  762. %************************************************************************
  763. \begin{code}
  764. newTcEvBinds :: TcM EvBindsVar
  765. newTcEvBinds = do { ref <- newTcRef emptyEvBindMap
  766. ; uniq <- newUnique
  767. ; return (EvBindsVar ref uniq) }
  768. extendTcEvBinds :: TcEvBinds -> EvVar -> EvTerm -> TcM TcEvBinds
  769. extendTcEvBinds binds@(TcEvBinds binds_var) var rhs
  770. = do { addTcEvBind binds_var var rhs
  771. ; return binds }
  772. extendTcEvBinds (EvBinds bnds) var rhs
  773. = return (EvBinds (bnds `snocBag` EvBind var rhs))
  774. addTcEvBind :: EvBindsVar -> EvVar -> EvTerm -> TcM ()
  775. -- Add a binding to the TcEvBinds by side effect
  776. addTcEvBind (EvBindsVar ev_ref _) var rhs
  777. = do { bnds <- readTcRef ev_ref
  778. ; writeTcRef ev_ref (extendEvBinds bnds var rhs) }
  779. chooseUniqueOccTc :: (OccSet -> OccName) -> TcM OccName
  780. chooseUniqueOccTc fn =
  781. do { env <- getGblEnv
  782. ; let dfun_n_var = tcg_dfun_n env
  783. ; set <- readTcRef dfun_n_var
  784. ; let occ = fn set
  785. ; writeTcRef dfun_n_var (extendOccSet set occ)
  786. ; return occ }
  787. getConstraintVar :: TcM (TcRef WantedConstraints)
  788. getConstraintVar = do { env <- getLclEnv; return (tcl_lie env) }
  789. setConstraintVar :: TcRef WantedConstraints -> TcM a -> TcM a
  790. setConstraintVar lie_var = updLclEnv (\ env -> env { tcl_lie = lie_var })
  791. emitConstraints :: WantedConstraints -> TcM ()
  792. emitConstraints ct
  793. = do { lie_var <- getConstraintVar ;
  794. updTcRef lie_var (`andWC` ct) }
  795. emitFlat :: WantedEvVar -> TcM ()
  796. emitFlat ct
  797. = do { lie_var <- getConstraintVar ;
  798. updTcRef lie_var (`addFlats` unitBag ct) }
  799. emitFlats :: Bag WantedEvVar -> TcM ()
  800. emitFlats ct
  801. = do { lie_var <- getConstraintVar ;
  802. updTcRef lie_var (`addFlats` ct) }
  803. emitImplication :: Implication -> TcM ()
  804. emitImplication ct
  805. = do { lie_var <- getConstraintVar ;
  806. updTcRef lie_var (`addImplics` unitBag ct) }
  807. emitImplications :: Bag Implication -> TcM ()
  808. emitImplications ct
  809. = do { lie_var <- getConstraintVar ;
  810. updTcRef lie_var (`addImplics` ct) }
  811. captureConstraints :: TcM a -> TcM (a, WantedConstraints)
  812. -- (captureConstraints m) runs m, and returns the type constraints it generates
  813. captureConstraints thing_inside
  814. = do { lie_var <- newTcRef emptyWC ;
  815. res <- updLclEnv (\ env -> env { tcl_lie = lie_var })
  816. thing_inside ;
  817. lie <- readTcRef lie_var ;
  818. return (res, lie) }
  819. captureUntouchables :: TcM a -> TcM (a, Untouchables)
  820. captureUntouchables thing_inside
  821. = do { env <- getLclEnv
  822. ; low_meta <- readTcRef (tcl_meta env)
  823. ; res <- setLclEnv (env { tcl_untch = low_meta })
  824. thing_inside
  825. ; high_meta <- readTcRef (tcl_meta env)
  826. ; return (res, TouchableRange low_meta high_meta) }
  827. isUntouchable :: TcTyVar -> TcM Bool
  828. isUntouchable tv = do { env <- getLclEnv
  829. ; return (varUnique tv < tcl_untch env) }
  830. getLclTypeEnv :: TcM (NameEnv TcTyThing)
  831. getLclTypeEnv = do { env <- getLclEnv; return (tcl_env env) }
  832. setLclTypeEnv :: TcLclEnv -> TcM a -> TcM a
  833. -- Set the local type envt, but do *not* disturb other fields,
  834. -- notably the lie_var
  835. setLclTypeEnv lcl_env thing_inside
  836. = updLclEnv upd thing_inside
  837. where
  838. upd env = env { tcl_env = tcl_env lcl_env,
  839. tcl_tyvars = tcl_tyvars lcl_env }
  840. \end{code}
  841. %************************************************************************
  842. %* *
  843. Template Haskell context
  844. %* *
  845. %************************************************************************
  846. \begin{code}
  847. recordThUse :: TcM ()
  848. recordThUse = do { env <- getGblEnv; writeTcRef (tcg_th_used env) True }
  849. keepAliveTc :: Id -> TcM () -- Record the name in the keep-alive set
  850. keepAliveTc id
  851. | isLocalId id = do { env <- getGblEnv;
  852. ; updTcRef (tcg_keep env) (`addOneToNameSet` idName id) }
  853. | otherwise = return ()
  854. keepAliveSetTc :: NameSet -> TcM () -- Record the name in the keep-alive set
  855. keepAliveSetTc ns = do { env <- getGblEnv;
  856. ; updTcRef (tcg_keep env) (`unionNameSets` ns) }
  857. getStage :: TcM ThStage
  858. getStage = do { env <- getLclEnv; return (tcl_th_ctxt env) }
  859. setStage :: ThStage -> TcM a -> TcM a
  860. setStage s = updLclEnv (\ env -> env { tcl_th_ctxt = s })
  861. \end{code}
  862. %************************************************************************
  863. %* *
  864. Stuff for the renamer's local env
  865. %* *
  866. %************************************************************************
  867. \begin{code}
  868. getLocalRdrEnv :: RnM LocalRdrEnv
  869. getLocalRdrEnv = do { env <- getLclEnv; return (tcl_rdr env) }
  870. setLocalRdrEnv :: LocalRdrEnv -> RnM a -> RnM a
  871. setLocalRdrEnv rdr_env thing_inside
  872. = updLclEnv (\env -> env {tcl_rdr = rdr_env}) thing_inside
  873. \end{code}
  874. %************************************************************************
  875. %* *
  876. Stuff for interface decls
  877. %* *
  878. %************************************************************************
  879. \begin{code}
  880. mkIfLclEnv :: Module -> SDoc -> IfLclEnv
  881. mkIfLclEnv mod loc = IfLclEnv { if_mod = mod,
  882. if_loc = loc,
  883. if_tv_env = emptyUFM,
  884. if_id_env = emptyUFM }
  885. initIfaceTcRn :: IfG a -> TcRn a
  886. initIfaceTcRn thing_inside
  887. = do { tcg_env <- getGblEnv
  888. ; let { if_env = IfGblEnv { if_rec_types = Just (tcg_mod tcg_env, get_type_env) }
  889. ; get_type_env = readTcRef (tcg_type_env_var tcg_env) }
  890. ; setEnvs (if_env, ()) thing_inside }
  891. initIfaceExtCore :: IfL a -> TcRn a
  892. initIfaceExtCore thing_inside
  893. = do { tcg_env <- getGblEnv
  894. ; let { mod = tcg_mod tcg_env
  895. ; doc = ptext (sLit "External Core file for") <+> quotes (ppr mod)
  896. ; if_env = IfGblEnv {
  897. if_rec_types = Just (mod, return (tcg_type_env tcg_env)) }
  898. ; if_lenv = mkIfLclEnv mod doc
  899. }
  900. ; setEnvs (if_env, if_lenv) thing_inside }
  901. initIfaceCheck :: HscEnv -> IfG a -> IO a
  902. -- Used when checking the up-to-date-ness of the old Iface
  903. -- Initialise the environment with no useful info at all
  904. initIfaceCheck hsc_env do_this
  905. = do let rec_types = case hsc_type_env_var hsc_env of
  906. Just (mod,var) -> Just (mod, readTcRef var)
  907. Nothing -> Nothing
  908. gbl_env = IfGblEnv { if_rec_types = rec_types }
  909. initTcRnIf 'i' hsc_env gbl_env () do_this
  910. initIfaceTc :: ModIface
  911. -> (TcRef TypeEnv -> IfL a) -> TcRnIf gbl lcl a
  912. -- Used when type-checking checking an up-to-date interface file
  913. -- No type envt from the current module, but we do know the module dependencies
  914. initIfaceTc iface do_this
  915. = do { tc_env_var <- newTcRef emptyTypeEnv
  916. ; let { gbl_env = IfGblEnv { if_rec_types = Just (mod, readTcRef tc_env_var) } ;
  917. ; if_lenv = mkIfLclEnv mod doc
  918. }
  919. ; setEnvs (gbl_env, if_lenv) (do_this tc_env_var)
  920. }
  921. where
  922. mod = mi_module iface
  923. doc = ptext (sLit "The interface for") <+> quotes (ppr mod)
  924. initIfaceRules :: HscEnv -> ModGuts -> IfG a -> IO a
  925. -- Used when sucking in new Rules in SimplCore
  926. -- We have available the type envt of the module being compiled, and we must use it
  927. initIfaceRules hsc_env guts do_this
  928. = do { let {
  929. type_info = (mg_module guts, return (mg_types guts))
  930. ; gbl_env = IfGblEnv { if_rec_types = Just type_info } ;
  931. }
  932. -- Run the thing; any exceptions just bubble out from here
  933. ; initTcRnIf 'i' hsc_env gbl_env () do_this
  934. }
  935. initIfaceLcl :: Module -> SDoc -> IfL a -> IfM lcl a
  936. initIfaceLcl mod loc_doc thing_inside
  937. = setLclEnv (mkIfLclEnv mod loc_doc) thing_inside
  938. getIfModule :: IfL Module
  939. getIfModule = do { env <- getLclEnv; return (if_mod env) }
  940. --------------------
  941. failIfM :: Message -> IfL a
  942. -- The Iface monad doesn't have a place to accumulate errors, so we
  943. -- just fall over fast if one happens; it "shouldnt happen".
  944. -- We use IfL here so that we can get context info out of the local env
  945. failIfM msg
  946. = do { env <- getLclEnv
  947. ; let full_msg = (if_loc env <> colon) $$ nest 2 msg
  948. ; liftIO (printErrs (full_msg defaultErrStyle))
  949. ; failM }
  950. --------------------
  951. forkM_maybe :: SDoc -> IfL a -> IfL (Maybe a)
  952. -- Run thing_inside in an interleaved thread.
  953. -- It shares everything with the parent thread, so this is DANGEROUS.
  954. --
  955. -- It returns Nothing if the computation fails
  956. --
  957. -- It's used for lazily type-checking interface
  958. -- signatures, which is pretty benign
  959. forkM_maybe doc thing_inside
  960. = do { unsafeInterleaveM $
  961. do { traceIf (text "Starting fork {" <+> doc)
  962. ; mb_res <- tryM $
  963. updLclEnv (\env -> env { if_loc = if_loc env $$ doc }) $
  964. thing_inside
  965. ; case mb_res of
  966. Right r -> do { traceIf (text "} ending fork" <+> doc)
  967. ; return (Just r) }
  968. Left exn -> do {
  969. -- Bleat about errors in the forked thread, if -ddump-if-trace is on
  970. -- Otherwise we silently discard errors. Errors can legitimately
  971. -- happen when compiling interface signatures (see tcInterfaceSigs)
  972. ifDOptM Opt_D_dump_if_trace
  973. (print_errs (hang (text "forkM failed:" <+> doc)
  974. 2 (text (show exn))))
  975. ; traceIf (text "} ending fork (badly)" <+> doc)
  976. ; return Nothing }
  977. }}
  978. where
  979. print_errs sdoc = liftIO (printErrs (sdoc defaultErrStyle))
  980. forkM :: SDoc -> IfL a -> IfL a
  981. forkM doc thing_inside
  982. = do { mb_res <- forkM_maybe doc thing_inside
  983. ; return (case mb_res of
  984. Nothing -> pgmError "Cannot continue after interface file error"
  985. -- pprPanic "forkM" doc
  986. Just r -> r) }
  987. \end{code}