Skip to content

Commit 2d71599

Browse files
authored
Run simplifier before generating ByteCode (#410)
Running the simplifier is necessary to do things like inline data constructor wrappers. Fixes #256 and #393
1 parent f717cd4 commit 2d71599

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/Development/IDE/Core/Compile.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ import qualified GHC
4747
import GhcMonad
4848
import GhcPlugins as GHC hiding (fst3, (<>))
4949
import qualified HeaderInfo as Hdr
50-
import HscMain (hscInteractive)
50+
import HscMain (hscInteractive, hscSimplify)
5151
import MkIface
5252
import StringBuffer as SB
53+
import TcRnMonad (tcg_th_coreplugins)
5354
import TidyPgm
5455

5556
import Control.Monad.Extra
@@ -148,9 +149,14 @@ compileModule packageState deps tmr =
148149
let pm' = pm{pm_mod_summary = tweak $ pm_mod_summary pm}
149150
let tm' = tm{tm_parsed_module = pm'}
150151
GHC.dm_core_module <$> GHC.desugarModule tm'
151-
152+
let tc_result = fst (tm_internals_ (tmrModule tmr))
153+
-- Have to call the simplifier on the code even if we are at
154+
-- -O0 as otherwise the code generation fails which leads to
155+
-- errors like #256
156+
plugins <- liftIO $ readIORef (tcg_th_coreplugins tc_result)
157+
desugared_guts <- liftIO $ hscSimplify session plugins desugar
152158
-- give variables unique OccNames
153-
(guts, details) <- liftIO $ tidyProgram session desugar
159+
(guts, details) <- liftIO $ tidyProgram session desugared_guts
154160
return (map snd warnings, (mg_safe_haskell desugar, guts, details))
155161

156162
generateByteCode :: HscEnv -> [TcModuleResult] -> TcModuleResult -> CgGuts -> IO (IdeResult Linkable)

test/exe/Main.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,27 @@ thTests =
13741374
_ <- openDoc' "A.hs" "haskell" sourceA
13751375
_ <- openDoc' "B.hs" "haskell" sourceB
13761376
expectDiagnostics [ ( "B.hs", [(DsError, (6, 29), "Variable not in scope: n")] ) ]
1377+
, testSessionWait "newtype-closure" $ do
1378+
let sourceA =
1379+
T.unlines
1380+
[ "{-# LANGUAGE DeriveDataTypeable #-}"
1381+
,"{-# LANGUAGE TemplateHaskell #-}"
1382+
,"module A (a) where"
1383+
,"import Data.Data"
1384+
,"import Language.Haskell.TH"
1385+
,"newtype A = A () deriving (Data)"
1386+
,"a :: ExpQ"
1387+
,"a = [| 0 |]"]
1388+
let sourceB =
1389+
T.unlines
1390+
[ "{-# LANGUAGE TemplateHaskell #-}"
1391+
,"module B where"
1392+
,"import A"
1393+
,"b :: Int"
1394+
,"b = $( a )" ]
1395+
_ <- openDoc' "A.hs" "haskell" sourceA
1396+
_ <- openDoc' "B.hs" "haskell" sourceB
1397+
return ()
13771398
]
13781399

13791400
completionTests :: TestTree

0 commit comments

Comments
 (0)