Skip to content

Commit 316079c

Browse files
committed
ghcide and ghcide-test built
1 parent a767490 commit 316079c

File tree

7 files changed

+158
-22
lines changed

7 files changed

+158
-22
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ import qualified Data.HashMap.Strict as HashMap
6262
import Data.IntMap (IntMap)
6363
import Data.IORef
6464
import Data.List.Extra
65+
#if MIN_VERSION_ghc(9,11,0)
66+
import qualified Data.List.NonEmpty as NE
67+
#endif
6568
import qualified Data.Map.Strict as Map
6669
import Data.Maybe
6770
import Data.Proxy (Proxy (Proxy))
6871
import qualified Data.Text as T
6972
import Data.Time (UTCTime (..))
73+
#if MIN_VERSION_ghc(9,11,0)
74+
import Data.Time (getCurrentTime)
75+
#endif
7076
import Data.Tuple.Extra (dupe)
7177
import Debug.Trace
7278
import Development.IDE.Core.FileStore (resetInterfaceStore)
@@ -132,6 +138,10 @@ import Development.IDE.Core.FileStore (shareFilePath)
132138
#endif
133139

134140
import 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
137147
sourceTypecheck :: T.Text
@@ -210,7 +220,7 @@ typecheckModule (IdeDefer defer) hsc tc_helpers pm = do
210220
lookupCache :: HscEnv -> InstalledModule -> IO (Maybe InstalledFindResult)
211221
lookupCache 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
665703
generateByteCode (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

679726
demoteTypeErrorsToWarnings :: 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)
777825
generateHieAsts :: HscEnv -> TcModuleResult -> IO ([FileDiagnostic], Maybe (HieASTs Type))
826+
#else
827+
generateHieAsts :: HscEnv -> TcModuleResult -> IO ([FileDiagnostic], Maybe ((HieASTs Type), NameEntityInfo))
828+
#endif
778829
generateHieAsts 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]
880947
writeAndIndexHieFile 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)
9331000
mergeEnvs :: HscEnv -> ModuleGraph -> ModSummary -> [HomeModInfo] -> [HscEnv] -> IO HscEnv
9341001
mergeEnvs 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+
9771064
withBootSuffix :: HscSource -> ModLocation -> ModLocation
9781065
withBootSuffix HsBootFile = addBootSuffixLocnOut
9791066
withBootSuffix _ = 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

ghcide/src/Development/IDE/Core/PluginUtils.hs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import qualified Development.IDE.Core.Shake as Shake
5252
import Development.IDE.GHC.Orphans ()
5353
import Development.IDE.Graph hiding (ShakeValue)
5454
import Development.IDE.Types.Diagnostics
55-
import Development.IDE.Types.Location (NormalizedFilePath)
55+
import Development.IDE.Types.Location (NormalizedFilePath, Range)
5656
import qualified Development.IDE.Types.Location as Location
5757
import qualified Ide.Logger as Logger
5858
import Ide.Plugin.Error
@@ -62,6 +62,8 @@ import qualified Language.LSP.Protocol.Lens as LSP
6262
import Language.LSP.Protocol.Message (SMethod (..))
6363
import qualified Language.LSP.Protocol.Types as LSP
6464
import qualified StmContainers.Map as STM
65+
import qualified Language.LSP.Protocol.Lens as L
66+
import Ide.Types (FormattingMethod, FormattingHandler, PluginHandlers, PluginMethodHandler, mkPluginHandler, FormattingType (FormatText, FormatRange))
6567

6668
-- ----------------------------------------------------------------------------
6769
-- Action wrappers
@@ -180,6 +182,12 @@ fromCurrentRangeE mapping = maybeToExceptT (PluginInvalidUserState "fromCurrentR
180182
fromCurrentRangeMT :: Monad m => PositionMapping -> LSP.Range -> MaybeT m LSP.Range
181183
fromCurrentRangeMT mapping = MaybeT . pure . fromCurrentRange mapping
182184

185+
186+
-- todo:9.12 same as Ide.PluginUtils (rangesOverlap), migrate later
187+
-- import Ide.PluginUtils (rangesOverlap)
188+
rangesOverlap :: Range -> Range -> Bool
189+
rangesOverlap r1 r2 =
190+
r1 ^. L.start <= r2 ^. L.end && r2 ^. L.start <= r1 ^. L.end
183191
-- ----------------------------------------------------------------------------
184192
-- Diagnostics
185193
-- ----------------------------------------------------------------------------

ghcide/src/Development/IDE/Core/Rules.hs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,12 @@ persistentHieFileRule recorder = addPersistentRule GetHieAst $ \file -> runMaybe
519519

520520
getHieAstRuleDefinition :: NormalizedFilePath -> HscEnv -> TcModuleResult -> Action (IdeResult HieAstResult)
521521
getHieAstRuleDefinition f hsc tmr = do
522-
(diags, masts) <- liftIO $ generateHieAsts hsc tmr
522+
#if MIN_VERSION_ghc(9,11,0)
523+
(diags, mastsFull) <- liftIO $ generateHieAsts hsc tmr
524+
let masts = fst <$> mastsFull
525+
#else
526+
(diags, mastsFull@masts) <- liftIO $ generateHieAsts hsc tmr
527+
#endif
523528
se <- getShakeExtras
524529

525530
isFoi <- use_ IsFileOfInterest f
@@ -529,7 +534,7 @@ getHieAstRuleDefinition f hsc tmr = do
529534
LSP.sendNotification (SMethod_CustomMethod (Proxy @"ghcide/reference/ready")) $
530535
toJSON $ fromNormalizedFilePath f
531536
pure []
532-
_ | Just asts <- masts -> do
537+
_ | Just asts <- mastsFull -> do
533538
source <- getSourceFileSource f
534539
let exports = tcg_exports $ tmrTypechecked tmr
535540
modSummary = tmrModSummary tmr
@@ -1063,7 +1068,12 @@ getLinkableRule recorder =
10631068
else pure Nothing
10641069
case mobj_time of
10651070
Just obj_t
1066-
| obj_t >= core_t -> pure ([], Just $ HomeModInfo hirModIface hirModDetails (justObjects $ LM (posixSecondsToUTCTime obj_t) (ms_mod hirModSummary) [DotO obj_file]))
1071+
| obj_t >= core_t -> pure ([], Just $ HomeModInfo hirModIface hirModDetails (justObjects $ LM (posixSecondsToUTCTime obj_t) (ms_mod hirModSummary)
1072+
$ pure (DotO obj_file
1073+
#if MIN_VERSION_ghc(9,11,0)
1074+
ModuleObject
1075+
#endif
1076+
)))
10671077
_ -> liftIO $ coreFileToLinkable linkableType (hscEnv session) hirModSummary hirModIface hirModDetails bin_core (error "object doesn't have time")
10681078
-- Record the linkable so we know not to unload it, and unload old versions
10691079
whenJust ((homeModInfoByteCode =<< hmi) <|> (homeModInfoObject =<< hmi)) $ \(LM time mod _) -> do
@@ -1080,7 +1090,13 @@ getLinkableRule recorder =
10801090
--just before returning it to be loaded. This has a substantial effect on recompile
10811091
--times as the number of loaded modules and splices increases.
10821092
--
1083-
unload (hscEnv session) (map (\(mod', time') -> LM time' mod' []) $ moduleEnvToList to_keep)
1093+
unload (hscEnv session) (map (\(mod', time') -> LM time' mod'
1094+
#if MIN_VERSION_ghc(9,11,0)
1095+
$ pure (DotO obj_file ModuleObject))
1096+
#else
1097+
$ pure (DotO obj_file))
1098+
#endif
1099+
$ moduleEnvToList to_keep)
10841100
return (to_keep, ())
10851101
return (fileHash <$ hmi, (warns, LinkableResult <$> hmi <*> pure fileHash))
10861102

ghcide/src/Development/IDE/GHC/Compat.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,20 @@ type NameCacheUpdater = NameCache
338338

339339
mkHieFile' :: ModSummary
340340
-> [Avail.AvailInfo]
341+
#if MIN_VERSION_ghc(9,11,0)
342+
-> (HieASTs Type, NameEntityInfo)
343+
#else
341344
-> HieASTs Type
345+
#endif
342346
-> BS.ByteString
343347
-> Hsc HieFile
344-
mkHieFile' ms exports asts src = do
348+
mkHieFile' ms exports
349+
#if MIN_VERSION_ghc(9,11,0)
350+
(asts, entityInfo)
351+
#else
352+
asts
353+
#endif
354+
src = do
345355
let Just src_file = ml_hs_file $ ms_location ms
346356
(asts',arr) = compressTypes asts
347357
return $ HieFile

0 commit comments

Comments
 (0)