Skip to content

Commit 93088b1

Browse files
committed
ghcide: Core.Compile: add getDocsNonInteractive{',}
`getDocsBatch` cuurently (& before) used only for single name retrieval function. Use of it is in `Documentation` module `getDocumentationTryGhc` where it wraps arg into singleton & gives to `getDocsBatch` & then recieves a Map with 1 entry & unsafely "lookups" doc in it. This work is to supply the proper single name retrieval-optimized version to stop that `getDocsBatch` there. & further ideally `getDocumentationTryGhc` uses single-retrieval & `getDocumentationsTryGhc` uses a batch mode & batch mode gets optimized along the lines of: haskell#2371
1 parent 7aaad14 commit 93088b1

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module Development.IDE.Core.Compile
2828
, loadInterface
2929
, loadModulesHome
3030
, setupFinderCache
31+
, getDocsNonInteractive
32+
, getDocsNonInteractive'
3133
, getDocsBatch
3234
, lookupName
3335
,mergeEnvs) where
@@ -990,12 +992,20 @@ mkDetailsFromIface session iface linkable = do
990992
initIfaceLoad hsc' (typecheckIface iface)
991993
return (HomeModInfo iface details linkable)
992994

995+
fakeSpan :: RealSrcSpan
996+
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
993997

994-
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
995-
-- The interactive paths create problems in ghc-lib builds
996-
--- and lead to fun errors like "Cannot continue after interface file error".
997-
getDocsNonInteractive :: Name -> IOEnv (Env TcGblEnv TcLclEnv) (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
998-
getDocsNonInteractive name = do
998+
initTypecheckEnv :: HscEnv -> Module -> TcRn r -> IO (Messages, Maybe r)
999+
initTypecheckEnv hsc_env mod = initTc hsc_env HsSrcFile False mod fakeSpan
1000+
1001+
getDocsNonInteractive'
1002+
:: Name
1003+
-> IOEnv
1004+
(Env TcGblEnv TcLclEnv)
1005+
(Name,
1006+
Either
1007+
GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
1008+
getDocsNonInteractive' name =
9991009
case nameModule_maybe name of
10001010
Nothing -> return (name, Left $ NameHasNoModule name)
10011011
Just mod -> do
@@ -1007,7 +1017,7 @@ getDocsNonInteractive name = do
10071017
<- loadModuleInterface "getModuleInterface" mod
10081018
let
10091019
isNameCompiled =
1010-
-- TODO: Find a more direct indicator.
1020+
-- comment from GHC: Find a more direct indicator.
10111021
case nameSrcLoc name of
10121022
RealSrcLoc {} -> False
10131023
UnhelpfulLoc {} -> True
@@ -1016,6 +1026,15 @@ getDocsNonInteractive name = do
10161026
then Left $ NoDocsInIface mod isNameCompiled
10171027
else Right (Map.lookup name dmap, Map.lookup name amap)
10181028

1029+
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
1030+
-- The interactive paths create problems in ghc-lib builds
1031+
--- and lead to fun errors like "Cannot continue after interface file error".
1032+
getDocsNonInteractive :: HscEnv -> Module -> Name -> IO (Either ErrorMessages (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString))))
1033+
getDocsNonInteractive hsc_env mod name = do
1034+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ getDocsNonInteractive' name
1035+
pure $ maybeToEither errs res
1036+
1037+
10191038
-- | Non-interactive, batch version of 'GHC.Runtime.Eval.getDocs'.
10201039
getDocsBatch
10211040
:: HscEnv
@@ -1024,13 +1043,10 @@ getDocsBatch
10241043
-- 2021-11-18: NOTE: Map Int would become IntMap if next GHCs.
10251044
-> IO (Either ErrorMessages (Map.Map Name (Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))))
10261045
-- ^ Return a 'Map' of 'Name's to 'Either' (no docs messages) (general doc body & arg docs)
1027-
getDocsBatch hsc_env _mod _names = do
1028-
((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ Map.fromList <$> traverse getDocsNonInteractive _names
1046+
getDocsBatch hsc_env mod names = do
1047+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ Map.fromList <$> traverse getDocsNonInteractive' names
10291048
pure $ maybeToEither errs res
10301049

1031-
fakeSpan :: RealSrcSpan
1032-
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
1033-
10341050
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
10351051
-- The interactive paths create problems in ghc-lib builds
10361052
--- and leads to fun errors like "Cannot continue after interface file error".
@@ -1039,7 +1055,7 @@ lookupName :: HscEnv
10391055
-> Name
10401056
-> IO (Maybe TyThing)
10411057
lookupName hsc_env mod name = do
1042-
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
1058+
(_messages, res) <- initTypecheckEnv hsc_env mod $ do
10431059
tcthing <- tcLookup name
10441060
case tcthing of
10451061
AGlobal thing -> return thing

0 commit comments

Comments
 (0)