Skip to content

Commit e418ac4

Browse files
committed
Make it compile with all supported GHC versions
1 parent 475737f commit e418ac4

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ module Development.IDE.GHC.Compat.Core (
253253
SrcLoc.noSrcSpan,
254254
SrcLoc.noSrcLoc,
255255
SrcLoc.noLoc,
256+
SrcLoc.mapLoc,
256257
-- * Finder
257258
FindResult(..),
258259
mkHomeModLocation,
@@ -461,6 +462,18 @@ module Development.IDE.GHC.Compat.Core (
461462
module GHC.Unit.Finder.Types,
462463
module GHC.Unit.Env,
463464
module GHC.Driver.Phases,
465+
#endif
466+
# if !MIN_VERSION_ghc(9,4,0)
467+
pattern HsFieldBind,
468+
hfbAnn,
469+
hfbLHS,
470+
hfbRHS,
471+
hfbPun,
472+
#endif
473+
#if !MIN_VERSION_ghc_boot_th(9,4,1)
474+
Extension(.., NamedFieldPuns),
475+
#else
476+
Extension(..)
464477
#endif
465478
) where
466479

@@ -710,12 +723,12 @@ import TcRnMonad hiding (Applicative (..), IORef,
710723
allM, anyM, concatMapM, foldrM,
711724
mapMaybeM, (<$>))
712725
import TcRnTypes
713-
import TcType
726+
import TcType
714727
import qualified TcType
715728
import TidyPgm as GHC
716729
import qualified TyCoRep
717730
import TyCon
718-
import Type
731+
import Type
719732
import TysPrim
720733
import TysWiredIn
721734
import Unify
@@ -755,6 +768,11 @@ import qualified GHC.Driver.Finder as GHC
755768
import qualified Finder as GHC
756769
#endif
757770

771+
-- NOTE(ozkutuk): Cpp clashes Phase.Cpp, so we hide it.
772+
-- Not the greatest solution, but gets the job done
773+
-- (until the CPP extension is actually needed).
774+
import GHC.LanguageExtensions.Type hiding (Cpp)
775+
758776

759777
mkHomeModLocation :: DynFlags -> ModuleName -> FilePath -> IO Module.ModLocation
760778
#if MIN_VERSION_ghc(9,3,0)
@@ -1101,3 +1119,21 @@ driverNoStop =
11011119
hscUpdateHPT :: (HomePackageTable -> HomePackageTable) -> HscEnv -> HscEnv
11021120
hscUpdateHPT k session = session { hsc_HPT = k (hsc_HPT session) }
11031121
#endif
1122+
1123+
#if !MIN_VERSION_ghc(9,2,0)
1124+
match :: HsRecField' id arg -> ((), id, arg, Bool)
1125+
match (HsRecField lhs rhs pun) = ((), SrcLoc.unLoc lhs, rhs, pun)
1126+
1127+
pattern HsFieldBind :: () -> id -> arg -> Bool -> HsRecField' id arg
1128+
pattern HsFieldBind {hfbAnn, hfbLHS, hfbRHS, hfbPun} <- (match -> (hfbAnn, hfbLHS, hfbRHS, hfbPun)) where
1129+
HsFieldBind _ lhs rhs pun = HsRecField (SrcLoc.noLoc lhs) rhs pun
1130+
#elif !MIN_VERSION_ghc(9,4,0)
1131+
pattern HsFieldBind :: XHsRecField id -> id -> arg -> Bool -> HsRecField' id arg
1132+
pattern HsFieldBind {hfbAnn, hfbLHS, hfbRHS, hfbPun} <- HsRecField hfbAnn (SrcLoc.unLoc -> hfbLHS) hfbRHS hfbPun where
1133+
HsFieldBind ann lhs rhs pun = HsRecField ann (SrcLoc.noLoc lhs) rhs pun
1134+
#endif
1135+
1136+
#if !MIN_VERSION_ghc_boot_th(9,4,1)
1137+
pattern NamedFieldPuns :: Extension
1138+
pattern NamedFieldPuns = RecordPuns
1139+
#endif

plugins/hls-explicit-record-fields-plugin/src/Ide/Plugin/ExplicitFields.hs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ import Development.IDE.GHC.Compat (HasSrcSpan (..),
3838
HsRecFields (..), HscEnv (..),
3939
LPat, Outputable, SrcSpan,
4040
pm_mod_summary, unLoc)
41-
import Development.IDE.GHC.Compat.Core (GenLocated (..), GhcPass (..),
41+
import Development.IDE.GHC.Compat.Core (Extension (NamedFieldPuns),
42+
GhcPass (..),
4243
HsExpr (RecordCon, rcon_flds),
43-
HsRecField' (..), LHsExpr,
44-
ModSummary (..), Pass (..),
45-
Pat (..), extensionFlags,
46-
hs_valds)
44+
LHsExpr, ModSummary (..),
45+
Pass (..), Pat (..),
46+
extensionFlags, hfbPun,
47+
hs_valds, mapLoc)
4748
import Development.IDE.GHC.Compat.Util (toList)
4849
import Development.IDE.GHC.Util (printOutputable)
4950
import Development.IDE.Graph (RuleResult)
@@ -53,7 +54,6 @@ import Development.IDE.Spans.Pragmas (NextPragmaInfo (..),
5354
insertNewPragma)
5455
import Development.IDE.Types.Logger (cmapWithPrio)
5556
import GHC.Generics (Generic)
56-
import GHC.LanguageExtensions.Type (Extension (..))
5757
import Ide.PluginUtils (getNormalizedFilePath,
5858
handleMaybeM, pluginResponse,
5959
subRange)
@@ -79,31 +79,26 @@ import qualified Language.LSP.Types.Lens as L
7979
-- the records that originally had wildcards with dots, even after they
8080
-- are removed by the renamer pass. Here `rec_dotdot` is set to
8181
-- `Nothing` so that fields are printed without such post-processing.
82-
preprocessRecord :: HsRecFields p arg -> HsRecFields p arg
82+
preprocessRecord :: HsRecFields (GhcPass c) arg -> HsRecFields (GhcPass c) arg
8383
preprocessRecord flds = flds { rec_dotdot = Nothing , rec_flds = rec_flds' }
8484
where
85-
-- TODO(ozkutuk): HsRecField' is renamed to HsFieldBind in GHC 9.4
86-
-- Add it as a pattern synonym to the ghcide compat module, so it would
87-
-- work on all HLS builds.
88-
-- https://gitlab.haskell.org/ghc/ghc/-/merge_requests/5757
89-
9085
no_pun_count = maybe (length (rec_flds flds)) unLoc (rec_dotdot flds)
9186
-- Field binds of the explicit form (e.g. `{ a = a' }`) should be
9287
-- left as is, hence the split.
9388
(no_puns, puns) = splitAt no_pun_count (rec_flds flds)
9489
-- `hsRecPun` is set to `True` in order to pretty-print the fields as field
9590
-- puns (since there is similar mechanism in the `Outputable` instance as
9691
-- explained above).
97-
puns' = map (\(L ss fld) -> L ss (fld { hsRecPun = True })) puns
92+
puns' = map (mapLoc (\fld -> fld { hfbPun = True })) puns
9893
rec_flds' = no_puns <> puns'
9994

100-
showRecordPat :: Outputable (Pat p) => Pat p -> Maybe Text
95+
showRecordPat :: Outputable (Pat (GhcPass c)) => Pat (GhcPass c) -> Maybe Text
10196
showRecordPat pat@(ConPat _ _ (RecCon flds)) =
10297
Just $ printOutputable $
10398
pat { pat_args = RecCon (preprocessRecord flds) }
10499
showRecordPat _ = Nothing
105100

106-
showRecordCon :: Outputable (HsExpr p) => HsExpr p -> Maybe Text
101+
showRecordCon :: Outputable (HsExpr (GhcPass c)) => HsExpr (GhcPass c) -> Maybe Text
107102
showRecordCon expr@(RecordCon _ _ flds) =
108103
Just $ printOutputable $
109104
expr { rcon_flds = preprocessRecord flds }
@@ -239,14 +234,14 @@ codeActionProvider ideState pId (CodeActionParams _ _ docId range _) = pluginRes
239234
mkTextEdit :: RenderedRecordInfo -> Maybe TextEdit
240235
mkTextEdit (RenderedRecordInfo ss r) = TextEdit <$> srcSpanToRange ss <*> pure r
241236

242-
-- TODO(ozkutuk): `RecordPuns` extension is renamed to `NamedFieldPuns`
243-
-- in GHC 9.4, so I probably need to add this to the compat module as
244-
-- well.
237+
-- NOTE(ozkutuk): `RecordPuns` extension is renamed to `NamedFieldPuns`
238+
-- in GHC 9.4, but we still want to insert `NamedFieldPuns` in pre-9.4
239+
-- GHC as well, hence the replacement.
245240
-- https://gitlab.haskell.org/ghc/ghc/-/merge_requests/6156
246241
pragmaEdit :: Maybe TextEdit
247-
pragmaEdit = if RecordPuns `elem` exts
242+
pragmaEdit = if NamedFieldPuns `elem` exts
248243
then Nothing
249-
else Just $ patchExtName $ insertNewPragma pragma RecordPuns
244+
else Just $ patchExtName $ insertNewPragma pragma NamedFieldPuns
250245
where
251246
patchExtName = L.newText %~ T.replace "Record" "NamedField"
252247

@@ -257,7 +252,7 @@ codeActionProvider ideState pId (CodeActionParams _ _ docId range _) = pluginRes
257252

258253
mkCodeActionTitle :: [Extension] -> Text
259254
mkCodeActionTitle exts =
260-
if RecordPuns `elem` exts
255+
if NamedFieldPuns `elem` exts
261256
then title
262257
else title <> " (needs extension: NamedFieldPuns)"
263258
where

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import Development.IDE.Core.Rules
4242
import Development.IDE.Core.RuleTypes
4343
import Development.IDE.Core.Service
4444
import Development.IDE.Core.Shake hiding (Log)
45-
import Development.IDE.GHC.Compat
45+
import Development.IDE.GHC.Compat hiding
46+
(ImplicitPrelude)
4647
import Development.IDE.GHC.Compat.ExactPrint
4748
import Development.IDE.GHC.Compat.Util
4849
import Development.IDE.GHC.Error

plugins/hls-tactics-plugin/src/Wingman/EmptyCase.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Development.IDE (hscEnv, realSrcSpanToRange)
2121
import Development.IDE.Core.RuleTypes
2222
import Development.IDE.Core.Shake (IdeState (..))
2323
import Development.IDE.Core.UseStale
24-
import Development.IDE.GHC.Compat hiding (empty)
24+
import Development.IDE.GHC.Compat hiding (empty, EmptyCase)
2525
import Development.IDE.GHC.ExactPrint
2626
import Development.IDE.Spans.LocalBindings (getLocalScope)
2727
import Ide.Types

0 commit comments

Comments
 (0)