@@ -62,11 +62,17 @@ import qualified Data.HashMap.Strict as HashMap
6262import Data.IntMap (IntMap )
6363import Data.IORef
6464import Data.List.Extra
65+ #if MIN_VERSION_ghc(9,11,0)
66+ import qualified Data.List.NonEmpty as NE
67+ #endif
6568import qualified Data.Map.Strict as Map
6669import Data.Maybe
6770import Data.Proxy (Proxy (Proxy ))
6871import qualified Data.Text as T
6972import Data.Time (UTCTime (.. ))
73+ #if MIN_VERSION_ghc(9,11,0)
74+ import Data.Time (getCurrentTime )
75+ #endif
7076import Data.Tuple.Extra (dupe )
7177import Debug.Trace
7278import Development.IDE.Core.FileStore (resetInterfaceStore )
@@ -132,6 +138,10 @@ import Development.IDE.Core.FileStore (shareFilePath)
132138#endif
133139
134140import Development.IDE.GHC.Compat.Driver (hscTypecheckRenameWithDiagnostics )
141+ #if MIN_VERSION_ghc(9,11,0)
142+ import GHC.Unit.Module.ModIface
143+ import GHC.Unit.Finder (initFinderCache )
144+ #endif
135145
136146-- Simple constants to make sure the source is consistently named
137147sourceTypecheck :: T. Text
@@ -210,7 +220,7 @@ typecheckModule (IdeDefer defer) hsc tc_helpers pm = do
210220lookupCache :: HscEnv -> InstalledModule -> IO (Maybe InstalledFindResult )
211221lookupCache hsc_env installedMod = do
212222#if MIN_VERSION_ghc(9,11,0)
213- lookupFinderCache (hsc_FC hsc_env) installedMod
223+ lookupFinderCache (hsc_FC hsc_env) ( GWIB installedMod NotBoot )
214224#else
215225 ; moduleLocs <- readIORef (fcModuleCache $ hsc_FC hsc_env)
216226 ; return $ lookupInstalledModuleEnv moduleLocs installedMod
@@ -279,7 +289,11 @@ captureSplicesAndDeps TypecheckHelpers{..} env k = do
279289 ; bcos <- byteCodeGen hsc_env
280290 (icInteractiveModule ictxt)
281291 stg_expr
282- [] Nothing
292+ []
293+ Nothing
294+ #if MIN_VERSION_ghc(9,11,0)
295+ []
296+ #endif
283297
284298 -- Exclude wired-in names because we may not have read
285299 -- their interface files, so getLinkDeps will fail
@@ -319,9 +333,16 @@ captureSplicesAndDeps TypecheckHelpers{..} env k = do
319333 ; let hsc_env' = loadModulesHome (map linkableHomeMod lbs) hsc_env
320334
321335 {- load it -}
336+ #if MIN_VERSION_ghc(9,11,0)
337+ -- ; u <- uniqFromTag 'I'
338+ ; let this_mod = mkInteractiveModule " interactive"
339+ ; bco_time <- getCurrentTime
340+ ; (fv_hvs, lbss, pkgs) <- loadDecls (hscInterp hsc_env') hsc_env' srcspan $ Linkable bco_time this_mod $ NE. singleton $ BCOs bcos
341+ #else
322342 ; (fv_hvs, lbss, pkgs) <- loadDecls (hscInterp hsc_env') hsc_env' srcspan bcos
323- ; let hval = (expectJust " hscCompileCoreExpr' " $ lookup (idName binding_id) fv_hvs, lbss, pkgs)
343+ #endif
324344
345+ ; let hval = (expectJust " hscCompileCoreExpr'" $ lookup (idName binding_id) fv_hvs, lbss, pkgs)
325346 ; modifyIORef' var (flip extendModuleEnvList [(mi_module $ hm_iface hm, linkableHash lb) | lb <- lbs, let hm = linkableHomeMod lb])
326347 ; return hval }
327348
@@ -445,6 +466,7 @@ mkHiFileResultNoCompile session tcm = do
445466 iface' <- mkIfaceTc hsc_env_tmp sf details ms Nothing tcGblEnv
446467#if MIN_VERSION_ghc(9,11,0)
447468 let iface = set_mi_top_env Nothing iface'
469+
448470 -- todo: 9.12, since usages are not expose anymore, we can't update mi_usages.
449471#else
450472 let iface = iface' {
@@ -470,25 +492,37 @@ mkHiFileResultCompile se session' tcm simplified_guts = catchErrs $ do
470492 (guts, details) <- tidyProgram tidy_opts simplified_guts
471493 pure (details, guts)
472494
495+ -- (tcg_import_decls tc_result)
496+
473497 let ! partial_iface = force $ mkPartialIface session
474498#if MIN_VERSION_ghc(9,5,0)
475499 (cg_binds guts)
476500#endif
477501 details
478502 ms
503+ #if MIN_VERSION_ghc(9,11,0)
504+ (tcg_import_decls $ tmrTypechecked tcm)
505+ #endif
479506 simplified_guts
507+ let (iface_stubs, iface_files)
508+ | gopt Opt_WriteIfSimplifiedCore dflags = (cg_foreign guts, cg_foreign_files guts)
509+ | otherwise = (NoStubs , [] )
480510
481511 final_iface' <- mkFullIface session partial_iface Nothing
482512#if MIN_VERSION_ghc(9,4,2)
483513 Nothing
484514#endif
485- let final_iface = final_iface' {
486515#if MIN_VERSION_ghc(9,11,0)
487- mi_top_env = Nothing
516+ iface_stubs iface_files
517+ #endif
518+
519+ #if MIN_VERSION_ghc(9,11,0)
520+ let final_iface = set_mi_top_env Nothing final_iface'
488521#else
522+ let final_iface = final_iface' {
489523 mi_globals = Nothing
490- #endif
491524 , mi_usages = filterUsages (mi_usages final_iface')} -- See Note [Clearing mi_globals after generating an iface]
525+ #endif
492526
493527 -- Write the core file now
494528 core_file <- do
@@ -652,10 +686,14 @@ generateObjectCode session summary guts = do
652686 case obj of
653687 Nothing -> throwGhcExceptionIO $ Panic " compileFile didn't generate object code"
654688 Just x -> pure x
689+ #if MIN_VERSION_ghc(9,11,0)
690+ let unlinked = DotO dot_o_fp ModuleObject
691+ #else
655692 let unlinked = DotO dot_o_fp
693+ #endif
656694 -- Need time to be the modification time for recompilation checking
657695 t <- liftIO $ getModificationTime dot_o_fp
658- let linkable = LM t mod [ unlinked]
696+ let linkable = LM t mod ( pure unlinked)
659697
660698 pure (map snd warnings, linkable)
661699
@@ -665,15 +703,24 @@ generateByteCode :: CoreFileTime -> HscEnv -> ModSummary -> CgGuts -> IO (IdeRes
665703generateByteCode (CoreFileTime time) hscEnv summary guts = do
666704 fmap (either (, Nothing ) (second Just )) $
667705 catchSrcErrors (hsc_dflags hscEnv) " bytecode" $ do
706+ #if MIN_VERSION_ghc(9,11,0)
707+ (warnings, (_, bytecode)) <-
708+ #else
668709 (warnings, (_, bytecode, sptEntries)) <-
710+ #endif
669711 withWarnings " bytecode" $ \ _tweak -> do
670712 let session = _tweak (hscSetFlags (ms_hspp_opts summary) hscEnv)
671713 -- TODO: maybe settings ms_hspp_opts is unnecessary?
672714 summary' = summary { ms_hspp_opts = hsc_dflags session }
673715 hscInteractive session (mkCgInteractiveGuts guts)
674716 (ms_location summary')
717+ #if MIN_VERSION_ghc(9,11,0)
718+ let unlinked = BCOs bytecode
719+ let linkable = LM time (ms_mod summary) (pure unlinked)
720+ #else
675721 let unlinked = BCOs bytecode sptEntries
676722 let linkable = LM time (ms_mod summary) [unlinked]
723+ #endif
677724 pure (map snd warnings, linkable)
678725
679726demoteTypeErrorsToWarnings :: ParsedModule -> ParsedModule
@@ -774,21 +821,35 @@ atomicFileWrite se targetPath write = do
774821 (write tempFilePath >>= \ x -> renameFile tempFilePath targetPath >> atomically (resetInterfaceStore se (toNormalizedFilePath' targetPath)) >> pure x)
775822 `onException` cleanUp
776823
824+ #if !MIN_VERSION_ghc(9,11,0)
777825generateHieAsts :: HscEnv -> TcModuleResult -> IO ([FileDiagnostic ], Maybe (HieASTs Type ))
826+ #else
827+ generateHieAsts :: HscEnv -> TcModuleResult -> IO ([FileDiagnostic ], Maybe ((HieASTs Type ), NameEntityInfo ))
828+ #endif
778829generateHieAsts hscEnv tcm =
779830 handleGenerationErrors' dflags " extended interface generation" $ runHsc hscEnv $ do
780831 -- These varBinds use unitDataConId but it could be anything as the id name is not used
781832 -- during the hie file generation process. It's a workaround for the fact that the hie modules
782833 -- don't export an interface which allows for additional information to be added to hie files.
783- let fake_splice_binds = Util. listToBag (map (mkVarBind unitDataConId) (spliceExpressions $ tmrTopLevelSplices tcm))
834+ let
835+ fake_splice_binds =
836+ #if !MIN_VERSION_ghc(9,11,0)
837+ Util. listToBag
838+ #endif
839+ (map (mkVarBind unitDataConId) (spliceExpressions $ tmrTopLevelSplices tcm))
784840 real_binds = tcg_binds $ tmrTypechecked tcm
785841 ts = tmrTypechecked tcm :: TcGblEnv
786842 top_ev_binds = tcg_ev_binds ts :: Util. Bag EvBind
787843 insts = tcg_insts ts :: [ClsInst ]
788844 tcs = tcg_tcs ts :: [TyCon ]
789845
790846 pure $ Just $
847+ #if MIN_VERSION_ghc(9,11,0)
848+ GHC. enrichHie (fake_splice_binds ++ real_binds) (tmrRenamed tcm) top_ev_binds insts tcs
849+ (tcg_type_env $ tmrTypechecked tcm)
850+ #else
791851 GHC. enrichHie (fake_splice_binds `Util.unionBags` real_binds) (tmrRenamed tcm) top_ev_binds insts tcs
852+ #endif
792853 where
793854 dflags = hsc_dflags hscEnv
794855
@@ -876,7 +937,13 @@ indexHieFile se mod_summary srcPath !hash hf = do
876937 toJSON $ fromNormalizedFilePath srcPath
877938 whenJust mdone $ \ _ -> progressUpdate indexProgressReporting ProgressCompleted
878939
879- writeAndIndexHieFile :: HscEnv -> ShakeExtras -> ModSummary -> NormalizedFilePath -> [GHC. AvailInfo ] -> HieASTs Type -> BS. ByteString -> IO [FileDiagnostic ]
940+ writeAndIndexHieFile :: HscEnv -> ShakeExtras -> ModSummary -> NormalizedFilePath -> [GHC. AvailInfo ]
941+ #if MIN_VERSION_ghc(9,11,0)
942+ -> (HieASTs Type , NameEntityInfo )
943+ #else
944+ -> HieASTs Type
945+ #endif
946+ -> BS. ByteString -> IO [FileDiagnostic ]
880947writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =
881948 handleGenerationErrors dflags " extended interface write/compression" $ do
882949 hf <- runHsc hscEnv $
@@ -932,11 +999,10 @@ handleGenerationErrors' dflags source action =
932999-- transitive dependencies will be contained in envs)
9331000mergeEnvs :: HscEnv -> ModuleGraph -> ModSummary -> [HomeModInfo ] -> [HscEnv ] -> IO HscEnv
9341001mergeEnvs env mg ms extraMods envs = do
935- #if !MIN_VERSION_ghc(9,11,0)
9361002 let im = Compat. installedModule (toUnitId $ moduleUnit $ ms_mod ms) (moduleName (ms_mod ms))
9371003 ifr = InstalledFound (ms_location ms) im
9381004 curFinderCache = Compat. extendInstalledModuleEnv Compat. emptyInstalledModuleEnv im ifr
939-
1005+ #if !MIN_VERSION_ghc(9,11,0)
9401006 newFinderCache <- concatFC curFinderCache (map hsc_FC envs)
9411007#endif
9421008 return $! loadModulesHome extraMods $
@@ -957,23 +1023,44 @@ mergeEnvs env mg ms extraMods envs = do
9571023 | HsSrcFile <- mi_hsc_src (hm_iface a) = a
9581024 | otherwise = b
9591025
960- #if !MIN_VERSION_ghc(9,11,0)
9611026 -- Prefer non-boot files over non-boot files
9621027 -- otherwise we can get errors like https://gitlab.haskell.org/ghc/ghc/-/issues/19816
9631028 -- if a boot file shadows over a non-boot file
9641029 combineModuleLocations a@ (InstalledFound ml _) _ | Just fp <- ml_hs_file ml, not (" boot" `isSuffixOf` fp) = a
9651030 combineModuleLocations _ b = b
966-
1031+ #if !MIN_VERSION_ghc(9,11,0)
9671032 concatFC :: FinderCacheState -> [FinderCache ] -> IO FinderCache
9681033 concatFC cur xs = do
9691034 fcModules <- mapM (readIORef . fcModuleCache) xs
9701035 fcFiles <- mapM (readIORef . fcFileCache) xs
9711036 fcModules' <- newIORef $! foldl' (plusInstalledModuleEnv combineModuleLocations) cur fcModules
9721037 fcFiles' <- newIORef $! Map. unions fcFiles
9731038 pure $ FinderCache fcModules' fcFiles'
1039+ #else
1040+ addFinderCacheState :: FinderCacheState -> FinderCache -> IO ()
1041+ addFinderCacheState state cache = mapM_ ((\ (m, r) -> addToFinderCache cache m r)) (first (\ x -> GWIB x NotBoot ) <$> installedModuleEnvElts state)
1042+
1043+ mergeFinderCache :: FinderCache -> FinderCache -> FinderCache
1044+ mergeFinderCache c2 c1 = FinderCache
1045+ { flushFinderCaches = \ u -> flushFinderCaches c1 u
1046+ , addToFinderCache = \ m r -> addToFinderCache c1 m r
1047+ , lookupFinderCache = \ m -> do
1048+ lookupFinderCache c1 m >>= \ case
1049+ Just r -> return (Just r)
1050+ Nothing -> lookupFinderCache c2 m
1051+ , lookupFileCache = \ f -> do
1052+ lookupFileCache c1 f `catchIO` \ _ -> lookupFileCache c2 f
1053+ }
1054+ -- use mergeFinderCache and addFinderCacheState
1055+ concatFC :: FinderCacheState -> [FinderCache ] -> IO FinderCache
1056+ concatFC state caches = do
1057+ finderCache <- initFinderCache
1058+ addFinderCacheState state finderCache
1059+ return $ foldr mergeFinderCache finderCache caches
9741060#endif
9751061
9761062
1063+
9771064withBootSuffix :: HscSource -> ModLocation -> ModLocation
9781065withBootSuffix HsBootFile = addBootSuffixLocnOut
9791066withBootSuffix _ = id
@@ -1453,7 +1540,9 @@ coreFileToCgGuts session iface details core_file = do
14531540 -- Implicit binds aren't saved, so we need to regenerate them ourselves.
14541541 let _implicit_binds = concatMap getImplicitBinds tyCons -- only used if GHC < 9.6
14551542 tyCons = typeEnvTyCons (md_types details)
1456- #if MIN_VERSION_ghc(9,5,0)
1543+ #if MIN_VERSION_ghc(9,11,0)
1544+ pure $ CgGuts this_mod tyCons core_binds [] NoStubs [] mempty Nothing []
1545+ #elif MIN_VERSION_ghc(9,5,0)
14571546 -- In GHC 9.6, the implicit binds are tidied and part of core_binds
14581547 pure $ CgGuts this_mod tyCons core_binds [] NoStubs [] mempty (emptyHpcInfo False ) Nothing []
14591548#else
0 commit comments