Skip to content

Commit e24ce26

Browse files
committed
ghcide: Add basic support for GHC-9.0.1
I tried to limit the use of CPP to the Compat module as much as possible by re-exporting the new functions under the old names, but there is still plenty of pragmas all over the code. I'm using ghc-api-compat so the imports doesn't need to be changed as much.
1 parent 26f3232 commit e24ce26

File tree

23 files changed

+472
-136
lines changed

23 files changed

+472
-136
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ library
9696
ghc >= 8.6,
9797
ghc-check >=0.5.0.1,
9898
ghc-paths,
99+
ghc-api-compat,
99100
cryptohash-sha1 >=0.11.100 && <0.12,
100101
hie-bios >= 0.7.1 && < 0.8.0,
101102
implicit-hie-cradle >= 0.3.0.2 && < 0.4,

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ import HscTypes (hsc_IC, hsc_NC,
7979
import Linker
8080
import Module
8181
import NameCache
82-
import Packages
8382

8483
import Control.Concurrent.STM (atomically)
8584
import Control.Concurrent.STM.TQueue
@@ -107,7 +106,7 @@ data SessionLoadingOptions = SessionLoadingOptions
107106
, getCacheDirs :: String -> [String] -> IO CacheDirs
108107
-- | Return the GHC lib dir to use for the 'unsafeGlobalDynFlags'
109108
, getInitialGhcLibDir :: IO (Maybe LibDir)
110-
, fakeUid :: InstalledUnitId
109+
, fakeUid :: GHC.InstalledUnitId
111110
-- ^ unit id used to tag the internal component built by ghcide
112111
-- To reuse external interface files the unit ids must match,
113112
-- thus make sure to build them with `--this-unit-id` set to the
@@ -120,7 +119,7 @@ instance Default SessionLoadingOptions where
120119
,loadCradle = HieBios.loadCradle
121120
,getCacheDirs = getCacheDirsDefault
122121
,getInitialGhcLibDir = getInitialGhcLibDirDefault
123-
,fakeUid = toInstalledUnitId (stringToUnitId "main")
122+
,fakeUid = GHC.toInstalledUnitId (GHC.stringToUnit "main")
124123
}
125124

126125
getInitialGhcLibDirDefault :: IO (Maybe LibDir)
@@ -730,12 +729,12 @@ removeInplacePackages
730729
-> [InstalledUnitId]
731730
-> DynFlags
732731
-> (DynFlags, [InstalledUnitId])
733-
removeInplacePackages fake_uid us df = (df { packageFlags = ps
734-
, thisInstalledUnitId = fake_uid }, uids)
732+
removeInplacePackages fake_uid us df = (setThisInstalledUnitId fake_uid $
733+
df { packageFlags = ps }, uids)
735734
where
736735
(uids, ps) = partitionEithers (map go (packageFlags df))
737-
go p@(ExposePackage _ (UnitIdArg u) _) = if toInstalledUnitId u `elem` us
738-
then Left (toInstalledUnitId u)
736+
go p@(ExposePackage _ (UnitIdArg u) _) = if GHC.toInstalledUnitId u `elem` us
737+
then Left (GHC.toInstalledUnitId u)
739738
else Right p
740739
go p = Right p
741740

@@ -776,7 +775,7 @@ setOptions (ComponentOptions theOpts compRoot _) dflags = do
776775
-- initPackages parses the -package flags and
777776
-- sets up the visibility for each component.
778777
-- Throws if a -package flag cannot be satisfied.
779-
(final_df, _) <- liftIO $ wrapPackageSetupException $ initPackages dflags''
778+
final_df <- liftIO $ wrapPackageSetupException $ initUnits dflags''
780779
return (final_df, targets)
781780

782781

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ import MkIface
8181
import StringBuffer as SB
8282
import TcIface (typecheckIface)
8383
import TcRnMonad hiding (newUnique)
84+
#if MIN_GHC_API_VERSION(9,0,1)
85+
import GHC.Builtin.Names
86+
import GHC.Iface.Recomp
87+
import GHC.Tc.Gen.Splice
88+
import GHC.Tc.Types.Evidence (EvBind)
89+
#else
90+
import PrelNames
8491
import TcSplice
92+
#endif
8593
import TidyPgm
8694

8795
import Bag
@@ -104,7 +112,6 @@ import qualified GHC.LanguageExtensions as LangExt
104112
import HeaderInfo
105113
import Linker (unload)
106114
import Maybes (orElse)
107-
import PrelNames
108115
import System.Directory
109116
import System.FilePath
110117
import System.IO.Extra (fixIO, newTempFileWithin)
@@ -144,10 +151,10 @@ computePackageDeps
144151
-> IO (Either [FileDiagnostic] [InstalledUnitId])
145152
computePackageDeps env pkg = do
146153
let dflags = hsc_dflags env
147-
case lookupInstalledPackage dflags pkg of
154+
case oldLookupInstalledPackage dflags pkg of
148155
Nothing -> return $ Left [ideErrorText (toNormalizedFilePath' noFilePath) $
149156
T.pack $ "unknown package: " ++ show pkg]
150-
Just pkgInfo -> return $ Right $ depends pkgInfo
157+
Just pkgInfo -> return $ Right $ unitDepends pkgInfo
151158

152159
typecheckModule :: IdeDefer
153160
-> HscEnv
@@ -268,7 +275,10 @@ mkHiFileResultCompile session' tcm simplified_guts ltype = catchErrs $ do
268275
(guts, details) <- tidyProgram session simplified_guts
269276
(diags, linkable) <- genLinkable session ms guts
270277
pure (linkable, details, diags)
271-
#if MIN_GHC_API_VERSION(8,10,0)
278+
#if MIN_GHC_API_VERSION(9,0,1)
279+
let !partial_iface = force (mkPartialIface session details simplified_guts)
280+
final_iface <- mkFullIface session partial_iface Nothing
281+
#elif MIN_GHC_API_VERSION(8,10,0)
272282
let !partial_iface = force (mkPartialIface session details simplified_guts)
273283
final_iface <- mkFullIface session partial_iface
274284
#else
@@ -336,7 +346,11 @@ generateObjectCode session summary guts = do
336346
target = defaultObjectTarget $ targetPlatform $ hsc_dflags session
337347
#endif
338348
session' = session { hsc_dflags = updOptLevel 0 $ (ms_hspp_opts summary') { outputFile = Just dot_o , hscTarget = target}}
349+
#if MIN_GHC_API_VERSION(9,0,1)
350+
(outputFilename, _mStub, _foreign_files, _cinfos) <- hscGenHardCode session' guts
351+
#else
339352
(outputFilename, _mStub, _foreign_files) <- hscGenHardCode session' guts
353+
#endif
340354
#if MIN_GHC_API_VERSION(8,10,0)
341355
(ms_location summary')
342356
#else
@@ -464,7 +478,15 @@ generateHieAsts hscEnv tcm =
464478
-- don't export an interface which allows for additional information to be added to hie files.
465479
let fake_splice_binds = listToBag (map (mkVarBind unitDataConId) (spliceExpresions $ tmrTopLevelSplices tcm))
466480
real_binds = tcg_binds $ tmrTypechecked tcm
481+
#if MIN_GHC_API_VERSION(9,0,1)
482+
-- TODO: Use some proper values here!
483+
evBinds = emptyBag @EvBind :: Bag EvBind
484+
clsInsts = [] :: [ClsInst]
485+
tyCons = [] :: [TyCon]
486+
Just <$> GHC.enrichHie (fake_splice_binds `unionBags` real_binds) (tmrRenamed tcm) evBinds clsInsts tyCons
487+
#else
467488
Just <$> GHC.enrichHie (fake_splice_binds `unionBags` real_binds) (tmrRenamed tcm)
489+
#endif
468490
where
469491
dflags = hsc_dflags hscEnv
470492

@@ -638,7 +660,7 @@ setupFinderCache mss session = do
638660

639661
-- Make modules available for others that import them,
640662
-- by putting them in the finder cache.
641-
let ims = map (InstalledModule (thisInstalledUnitId $ hsc_dflags session) . moduleName . ms_mod) mss
663+
let ims = map (installedModule (thisInstalledUnitId $ hsc_dflags session) . moduleName . ms_mod) mss
642664
ifrs = zipWith (\ms -> InstalledFound (ms_location ms)) mss ims
643665
-- set the target and module graph in the session
644666
graph = mkModuleGraph mss
@@ -696,7 +718,7 @@ getModSummaryFromImports env fp modTime contents = do
696718

697719
mod = fmap unLoc mb_mod `orElse` mAIN_NAME
698720

699-
(src_idecls, ord_idecls) = partition (ideclSource.unLoc) imps
721+
(src_idecls, ord_idecls) = partition ((== IsBoot) . ideclSource.unLoc) imps
700722

701723
-- GHC.Prim doesn't exist physically, so don't go looking for it.
702724
ordinary_imps = filter ((/= moduleName gHC_PRIM) . unLoc
@@ -765,7 +787,11 @@ parseHeader
765787
=> DynFlags -- ^ flags to use
766788
-> FilePath -- ^ the filename (for source locations)
767789
-> SB.StringBuffer -- ^ Haskell module source text (full Unicode is supported)
790+
#if MIN_GHC_API_VERSION(9,0,1)
791+
-> ExceptT [FileDiagnostic] m ([FileDiagnostic], Located(HsModule))
792+
#else
768793
-> ExceptT [FileDiagnostic] m ([FileDiagnostic], Located(HsModule GhcPs))
794+
#endif
769795
parseHeader dflags filename contents = do
770796
let loc = mkRealSrcLoc (mkFastString filename) 1 1
771797
case unP Parser.parseHeader (mkPState dflags contents loc) of
@@ -814,10 +840,21 @@ parseFileContents env customPreprocessor filename ms = do
814840
throwE $ diagFromErrMsg "parser" dflags $ mkPlainErrMsg dflags locErr msgErr
815841
#endif
816842
POk pst rdr_module ->
817-
let hpm_annotations =
843+
let hpm_annotations :: ApiAnns
844+
hpm_annotations =
845+
#if MIN_GHC_API_VERSION(9,0,1)
846+
-- Copied from GHC.Driver.Main
847+
ApiAnns {
848+
apiAnnItems = Map.fromListWith (++) $ annotations pst,
849+
apiAnnEofPos = eof_pos pst,
850+
apiAnnComments = Map.fromList (annotations_comments pst),
851+
apiAnnRogueComments = comment_q pst
852+
}
853+
#else
818854
(Map.fromListWith (++) $ annotations pst,
819855
Map.fromList ((noSrcSpan,comment_q pst)
820856
:annotations_comments pst))
857+
#endif
821858
(warns, errs) = getMessages pst dflags
822859
in
823860
do
@@ -840,7 +877,7 @@ parseFileContents env customPreprocessor filename ms = do
840877
throwE $ diagFromStrings "parser" DsError errs
841878

842879
let preproc_warnings = diagFromStrings "parser" DsWarning preproc_warns
843-
parsed' <- liftIO $ applyPluginsParsedResultAction env dflags ms hpm_annotations parsed
880+
parsed' <- liftIO $ applyPluginsParsedResultAction env dflags ms (hpm_annotations) parsed
844881

845882
-- To get the list of extra source files, we take the list
846883
-- that the parser gave us,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
22
-- SPDX-License-Identifier: Apache-2.0
3+
{-# LANGUAGE CPP #-}
34

45
module Development.IDE.Core.Preprocessor
56
( preprocessor
@@ -79,7 +80,11 @@ preprocessor env filename mbContents = do
7980
return (contents, opts, dflags)
8081
where
8182
logAction :: IORef [CPPLog] -> LogAction
83+
#if __GLASGOW_HASKELL__ >= 900
84+
logAction cppLogs dflags _reason severity srcSpan msg = do
85+
#else
8286
logAction cppLogs dflags _reason severity srcSpan _style msg = do
87+
#endif
8388
let log = CPPLog severity srcSpan $ T.pack $ showSDoc dflags msg
8489
modifyIORef cppLogs (log :)
8590

@@ -107,7 +112,7 @@ diagsFromCPPLogs filename logs =
107112
-- informational log messages and attaches them to the initial log message.
108113
go :: [CPPDiag] -> [CPPLog] -> [CPPDiag]
109114
go acc [] = reverse $ map (\d -> d {cdMessage = reverse $ cdMessage d}) acc
110-
go acc (CPPLog sev (RealSrcSpan span) msg : logs) =
115+
go acc (CPPLog sev (OldRealSrcSpan span) msg : logs) =
111116
let diag = CPPDiag (realSrcSpanToRange span) (toDSeverity sev) [msg]
112117
in go (diag : acc) logs
113118
go (diag : diags) (CPPLog _sev (UnhelpfulSpan _) msg : logs) =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ lookupMod
261261
:: HieDbWriter -- ^ access the database
262262
-> FilePath -- ^ The `.hie` file we got from the database
263263
-> ModuleName
264-
-> UnitId
264+
-> Unit
265265
-> Bool -- ^ Is this file a boot file?
266266
-> MaybeT IdeAction Uri
267267
lookupMod _dbchan _hie_f _mod _uid _boot = MaybeT $ pure Nothing

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ newSession extras@ShakeExtras{..} shakeDb acts = do
709709
logPriority logger (actionPriority d) msg
710710
notifyTestingLogMessage extras msg
711711

712+
-- The inferred type signature doesn't work in ghc >= 9.0.1
713+
workRun :: (forall b. IO b -> IO b) -> IO (IO ())
712714
workRun restore = withSpan "Shake session" $ \otSpan -> do
713715
let acts' = pumpActionThread otSpan : map (run otSpan) (reenqueued ++ acts)
714716
res <- try @SomeException (restore $ shakeRunDatabase shakeDb acts')

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
module Development.IDE.GHC.CPP(doCpp, addOptP)
2626
where
2727

28-
import Development.IDE.GHC.Compat
28+
import Development.IDE.GHC.Compat as Compat
2929
import FileCleanup
30-
import Module
3130
import Packages
3231
import Panic
3332
import SysTools
@@ -188,7 +187,7 @@ addOptP opt = onSettings (onOptP (opt:))
188187
-- ---------------------------------------------------------------------------
189188
-- Macros (cribbed from Cabal)
190189

191-
generatePackageVersionMacros :: [PackageConfig] -> String
190+
generatePackageVersionMacros :: [Compat.PackageConfig] -> String
192191
generatePackageVersionMacros pkgs = concat
193192
-- Do not add any C-style comments. See #3389.
194193
[ generateMacros "" pkgname version
@@ -221,7 +220,7 @@ getGhcVersionPathName dflags = do
221220
candidates <- case ghcVersionFile dflags of
222221
Just path -> return [path]
223222
Nothing -> (map (</> "ghcversion.h")) <$>
224-
(getPackageIncludePath dflags [toInstalledUnitId rtsUnitId])
223+
(getPackageIncludePath dflags [Compat.toInstalledUnitId Compat.rtsUnit])
225224

226225
found <- filterM doesFileExist candidates
227226
case found of

0 commit comments

Comments
 (0)