From 2d71599faf5e93d4ee848043ddd68f10dd295260 Mon Sep 17 00:00:00 2001 From: Matthew Pickering Date: Wed, 12 Feb 2020 12:09:47 +0000 Subject: [PATCH] Run simplifier before generating ByteCode (#410) Running the simplifier is necessary to do things like inline data constructor wrappers. Fixes #256 and #393 --- src/Development/IDE/Core/Compile.hs | 12 +++++++++--- test/exe/Main.hs | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Development/IDE/Core/Compile.hs b/src/Development/IDE/Core/Compile.hs index 793d9d13a5..fa775871d5 100644 --- a/src/Development/IDE/Core/Compile.hs +++ b/src/Development/IDE/Core/Compile.hs @@ -47,9 +47,10 @@ import qualified GHC import GhcMonad import GhcPlugins as GHC hiding (fst3, (<>)) import qualified HeaderInfo as Hdr -import HscMain (hscInteractive) +import HscMain (hscInteractive, hscSimplify) import MkIface import StringBuffer as SB +import TcRnMonad (tcg_th_coreplugins) import TidyPgm import Control.Monad.Extra @@ -148,9 +149,14 @@ compileModule packageState deps tmr = let pm' = pm{pm_mod_summary = tweak $ pm_mod_summary pm} let tm' = tm{tm_parsed_module = pm'} GHC.dm_core_module <$> GHC.desugarModule tm' - + let tc_result = fst (tm_internals_ (tmrModule tmr)) + -- Have to call the simplifier on the code even if we are at + -- -O0 as otherwise the code generation fails which leads to + -- errors like #256 + plugins <- liftIO $ readIORef (tcg_th_coreplugins tc_result) + desugared_guts <- liftIO $ hscSimplify session plugins desugar -- give variables unique OccNames - (guts, details) <- liftIO $ tidyProgram session desugar + (guts, details) <- liftIO $ tidyProgram session desugared_guts return (map snd warnings, (mg_safe_haskell desugar, guts, details)) generateByteCode :: HscEnv -> [TcModuleResult] -> TcModuleResult -> CgGuts -> IO (IdeResult Linkable) diff --git a/test/exe/Main.hs b/test/exe/Main.hs index 1e375ada2f..fdc63b4a65 100644 --- a/test/exe/Main.hs +++ b/test/exe/Main.hs @@ -1374,6 +1374,27 @@ thTests = _ <- openDoc' "A.hs" "haskell" sourceA _ <- openDoc' "B.hs" "haskell" sourceB expectDiagnostics [ ( "B.hs", [(DsError, (6, 29), "Variable not in scope: n")] ) ] + , testSessionWait "newtype-closure" $ do + let sourceA = + T.unlines + [ "{-# LANGUAGE DeriveDataTypeable #-}" + ,"{-# LANGUAGE TemplateHaskell #-}" + ,"module A (a) where" + ,"import Data.Data" + ,"import Language.Haskell.TH" + ,"newtype A = A () deriving (Data)" + ,"a :: ExpQ" + ,"a = [| 0 |]"] + let sourceB = + T.unlines + [ "{-# LANGUAGE TemplateHaskell #-}" + ,"module B where" + ,"import A" + ,"b :: Int" + ,"b = $( a )" ] + _ <- openDoc' "A.hs" "haskell" sourceA + _ <- openDoc' "B.hs" "haskell" sourceB + return () ] completionTests :: TestTree