Skip to content

Commit 705d47d

Browse files
committed
ghc9-ghcide: Fix some more issues that caused runtime errors
1 parent 6d10f44 commit 705d47d

File tree

3 files changed

+66
-51
lines changed

3 files changed

+66
-51
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ cradleToOptsAndLibDir cradle file = do
503503
emptyHscEnv :: IORef NameCache -> FilePath -> IO HscEnv
504504
emptyHscEnv nc libDir = do
505505
env <- runGhc (Just libDir) getSession
506-
initDynLinker env
506+
-- initDynLinker env -- This causes ghc9 to crash
507507
pure $ setNameCache nc env{ hsc_dflags = (hsc_dflags env){useUnicode = True } }
508508

509509
data TargetDetails = TargetDetails

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

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
22
-- SPDX-License-Identifier: Apache-2.0
33

4-
{-# LANGUAGE CPP #-}
5-
{-# LANGUAGE ConstraintKinds #-}
4+
{-# LANGUAGE CPP #-}
5+
{-# LANGUAGE ConstraintKinds #-}
66
{-# LANGUAGE FlexibleInstances #-}
7-
{-# LANGUAGE PatternSynonyms #-}
7+
{-# LANGUAGE PatternSynonyms #-}
88
{-# OPTIONS -Wno-dodgy-imports -Wno-incomplete-uni-patterns #-}
99
{-# OPTIONS -Wno-missing-signatures #-} -- TODO: Remove!
1010
#include "ghc-api-version.h"
@@ -70,6 +70,8 @@ module Development.IDE.GHC.Compat(
7070
Scaled,
7171
scaledThing,
7272

73+
lookupUnit',
74+
preloadClosureUs,
7375
-- Reexports from Package
7476
InstalledUnitId,
7577
PackageConfig,
@@ -82,7 +84,7 @@ module Development.IDE.GHC.Compat(
8284
packageVersion,
8385
toInstalledUnitId,
8486
lookupPackage,
85-
lookupPackage',
87+
-- lookupPackage',
8688
explicitPackages,
8789
exposedModules,
8890
packageConfigId,
@@ -122,71 +124,67 @@ module Development.IDE.GHC.Compat(
122124
,isQualifiedImport) where
123125

124126
#if MIN_GHC_API_VERSION(8,10,0)
125-
import LinkerTypes
127+
import LinkerTypes
126128
#endif
127129

128-
import StringBuffer
130+
import DynFlags hiding (ExposePackage)
129131
import qualified DynFlags
130-
import DynFlags hiding (ExposePackage)
131-
import Fingerprint (Fingerprint)
132-
import qualified Outputable as Out
133-
import qualified ErrUtils as Err
132+
import qualified ErrUtils as Err
133+
import Fingerprint (Fingerprint)
134134
import qualified Module
135+
import qualified Outputable as Out
136+
import StringBuffer
135137
#if MIN_GHC_API_VERSION(9,0,1)
136-
import qualified SrcLoc
137-
import qualified Data.Set as S
138+
import qualified Data.Set as S
139+
import GHC.Core.TyCo.Rep (Scaled, scaledThing)
138140
import GHC.Iface.Load
139-
import GHC.Core.TyCo.Rep (Scaled, scaledThing)
140141
import GHC.Types.Unique.Set (emptyUniqSet)
142+
import qualified SrcLoc
141143
#else
142-
import Module (InstalledUnitId,toInstalledUnitId)
144+
import Module (InstalledUnitId, toInstalledUnitId)
143145
#endif
144-
import Packages
145-
import Data.IORef
146-
import HscTypes
147-
import NameCache
148-
import qualified Data.ByteString as BS
149-
import MkIface
150-
import TcRnTypes
151-
import Compat.HieAst (mkHieFile,enrichHie)
152-
import Compat.HieBin
153-
import Compat.HieTypes
154-
import Compat.HieUtils
146+
import Compat.HieAst (enrichHie, mkHieFile)
147+
import Compat.HieBin
148+
import Compat.HieTypes
149+
import Compat.HieUtils
150+
import qualified Data.ByteString as BS
151+
import Data.IORef
152+
import HscTypes
153+
import MkIface
154+
import NameCache
155+
import Packages
156+
import TcRnTypes
155157

156158
#if MIN_GHC_API_VERSION(8,10,0)
157-
import GHC.Hs.Extension
159+
import GHC.Hs.Extension
158160
#else
159-
import HsExtension
161+
import HsExtension
160162
#endif
161163

164+
import Avail
165+
import GHC hiding (HasSrcSpan, ModLocation, getLoc,
166+
lookupName)
162167
import qualified GHC
163168
import qualified TyCoRep
164-
import GHC hiding (
165-
ModLocation,
166-
HasSrcSpan,
167-
lookupName,
168-
getLoc
169-
)
170-
import Avail
171169
#if MIN_GHC_API_VERSION(8,8,0)
172-
import Data.List (foldl')
170+
import Data.List (foldl')
173171
#else
174-
import Data.List (foldl', isSuffixOf)
172+
import Data.List (foldl', isSuffixOf)
175173
#endif
176174

177-
import DynamicLoading
178-
import Plugins (Plugin(parsedResultAction), withPlugins)
179-
import qualified Data.Map as M
175+
import qualified Data.Map as M
176+
import DynamicLoading
177+
import Plugins (Plugin (parsedResultAction), withPlugins)
180178

181179
#if !MIN_GHC_API_VERSION(8,8,0)
182-
import System.FilePath ((-<.>))
180+
import System.FilePath ((-<.>))
183181
#endif
184182

185183
#if !MIN_GHC_API_VERSION(8,8,0)
186184
import qualified EnumSet
187185

188-
import System.IO
189-
import Foreign.ForeignPtr
186+
import Foreign.ForeignPtr
187+
import System.IO
190188

191189

192190
hPutStringBuffer :: Handle -> StringBuffer -> IO ()
@@ -250,10 +248,11 @@ addIncludePathsQuote path x = x{includePaths = f $ includePaths x}
250248
where f i = i{includePathsQuote = path : includePathsQuote i}
251249

252250
pattern ModLocation :: Maybe FilePath -> FilePath -> FilePath -> GHC.ModLocation
253-
pattern ModLocation a b c <-
254251
#if MIN_GHC_API_VERSION(8,8,0)
252+
pattern ModLocation a b c <-
255253
GHC.ModLocation a b c _ where ModLocation a b c = GHC.ModLocation a b c ""
256254
#else
255+
pattern ModLocation a b c <-
257256
GHC.ModLocation a b c where ModLocation a b c = GHC.ModLocation a b c
258257
#endif
259258

@@ -356,9 +355,11 @@ packageName = Packages.unitPackageName
356355
lookupPackage = Packages.lookupUnit . unitState
357356
-- lookupPackage' = undefined
358357
-- lookupPackage' b pm u = Packages.lookupUnit' b pm undefined u
359-
lookupPackage' b pm u = Packages.lookupUnit' b pm emptyUniqSet u -- TODO: Is this correct?
358+
-- lookupPackage' b pm u = Packages.lookupUnit' b pm emptyUniqSet u -- TODO: Is this correct?
360359
-- lookupPackage' = fmap Packages.lookupUnit' . unitState
361360
getPackageConfigMap = Packages.unitInfoMap . unitState
361+
preloadClosureUs = Packages.preloadClosure . unitState
362+
-- getPackageConfigMap = unitState
362363
-- getPackageIncludePath = undefined
363364
getPackageIncludePath = Packages.getUnitIncludePath
364365
explicitPackages = Packages.explicitUnits
@@ -394,23 +395,35 @@ oldLookupModuleWithSuggestions = Packages.lookupModuleWithSuggestions . unitStat
394395
oldRenderWithStyle dflags sdoc sty = Out.renderWithStyle (initSDocContext dflags sty) sdoc
395396
oldMkUserStyle _ = Out.mkUserStyle
396397
oldMkErrStyle _ = Out.mkErrStyle
398+
399+
-- TODO: This is still a mess!
397400
oldFormatErrDoc :: DynFlags -> Err.ErrDoc -> Out.SDoc
398-
oldFormatErrDoc = Err.formatErrDoc . undefined
401+
oldFormatErrDoc dflags = Err.formatErrDoc dummySDocContext
402+
where dummySDocContext = initSDocContext dflags Out.defaultUserStyle
399403
-- oldFormatErrDoc = Err.formatErrDoc . undefined
400404
writeIfaceFile = writeIface
401405

402406
#else
403407
type Unit = Module.UnitId
404408
-- type PackageConfig = Packages.PackageConfig
409+
definiteUnitId :: Module.DefUnitId -> UnitId
405410
definiteUnitId = Module.DefiniteUnitId
411+
defUnitId :: InstalledUnitId -> Module.DefUnitId
406412
defUnitId = Module.DefUnitId
413+
installedModule :: InstalledUnitId -> ModuleName -> Module.InstalledModule
407414
installedModule = Module.InstalledModule
415+
oldLookupInstalledPackage :: DynFlags -> InstalledUnitId -> Maybe PackageConfig
408416
oldLookupInstalledPackage = Packages.lookupInstalledPackage
409417
-- packageName = Packages.packageName
410418
-- lookupPackage = Packages.lookupPackage
411419
-- getPackageConfigMap = Packages.getPackageConfigMap
420+
setThisInstalledUnitId :: InstalledUnitId -> DynFlags -> DynFlags
412421
setThisInstalledUnitId uid df = df { thisInstalledUnitId = uid}
413422

423+
lookupUnit' :: Bool -> PackageConfigMap -> p -> UnitId -> Maybe PackageConfig
424+
lookupUnit' b pcm _ = Packages.lookupPackage' b pcm
425+
preloadClosureUs = const ()
426+
414427
oldUnhelpfulSpan = UnhelpfulSpan
415428
pattern OldRealSrcSpan :: RealSrcSpan -> SrcSpan
416429
pattern OldRealSrcSpan x = RealSrcSpan x
@@ -486,15 +499,15 @@ pattern FunTy arg res <- TyCoRep.FunTy arg res
486499
isQualifiedImport :: ImportDecl a -> Bool
487500
#if MIN_GHC_API_VERSION(8,10,0)
488501
isQualifiedImport ImportDecl{ideclQualified = NotQualified} = False
489-
isQualifiedImport ImportDecl{} = True
502+
isQualifiedImport ImportDecl{} = True
490503
#else
491-
isQualifiedImport ImportDecl{ideclQualified} = ideclQualified
504+
isQualifiedImport ImportDecl{ideclQualified} = ideclQualified
492505
#endif
493-
isQualifiedImport _ = False
506+
isQualifiedImport _ = False
494507

495508
getRealSpan :: SrcSpan -> Maybe RealSrcSpan
496509
getRealSpan (OldRealSrcSpan x) = Just x
497-
getRealSpan _ = Nothing
510+
getRealSpan _ = Nothing
498511

499512

500513

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,14 @@ modifyDynFlags f = do
9595
-- | Given a 'Unit' try and find the associated 'PackageConfig' in the environment.
9696
lookupPackageConfig :: Unit -> HscEnv -> Maybe GHC.PackageConfig
9797
lookupPackageConfig unit env =
98-
GHC.lookupPackage' False pkgConfigMap unit
98+
-- GHC.lookupPackage' False pkgConfigMap unit
99+
GHC.lookupUnit' False pkgConfigMap prClsre unit
99100
where
100101
pkgConfigMap =
101102
-- For some weird reason, the GHC API does not provide a way to get the PackageConfigMap
102103
-- from PackageState so we have to wrap it in DynFlags first.
103104
getPackageConfigMap $ hsc_dflags env
105+
prClsre = preloadClosureUs $ hsc_dflags env
104106

105107

106108
-- | Convert from the @text@ package to the @GHC@ 'StringBuffer'.

0 commit comments

Comments
 (0)