Skip to content

Commit b51b190

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 cd3dc93 commit b51b190

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Development.IDE.Core.Compile
2828
, loadInterface
2929
, loadModulesHome
3030
, setupFinderCache
31+
, getDocsNonInteractive
3132
, getDocsBatch
3233
, lookupName
3334
,mergeEnvs) where
@@ -990,12 +991,20 @@ mkDetailsFromIface session iface linkable = do
990991
initIfaceLoad hsc' (typecheckIface iface)
991992
return (HomeModInfo iface details linkable)
992993

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

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
997+
initTypecheckEnv :: HscEnv -> Module -> TcRn r -> IO (Messages, Maybe r)
998+
initTypecheckEnv hsc_env mod = initTc hsc_env HsSrcFile False mod fakeSpan
999+
1000+
getDocsNonInteractive'
1001+
:: Name
1002+
-> IOEnv
1003+
(Env TcGblEnv TcLclEnv)
1004+
(Name,
1005+
Either
1006+
GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
1007+
getDocsNonInteractive' name =
9991008
case nameModule_maybe name of
10001009
Nothing -> return (name, Left $ NameHasNoModule name)
10011010
Just mod -> do
@@ -1007,7 +1016,7 @@ getDocsNonInteractive name = do
10071016
<- loadModuleInterface "getModuleInterface" mod
10081017
let
10091018
isNameCompiled =
1010-
-- TODO: Find a more direct indicator.
1019+
-- comment from GHC: Find a more direct indicator.
10111020
case nameSrcLoc name of
10121021
RealSrcLoc {} -> False
10131022
UnhelpfulLoc {} -> True
@@ -1016,6 +1025,15 @@ getDocsNonInteractive name = do
10161025
then Left $ NoDocsInIface mod isNameCompiled
10171026
else Right (Map.lookup name dmap, Map.lookup name amap)
10181027

1028+
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
1029+
-- The interactive paths create problems in ghc-lib builds
1030+
--- and lead to fun errors like "Cannot continue after interface file error".
1031+
getDocsNonInteractive :: HscEnv -> Module -> Name -> IO (Either ErrorMessages (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString))))
1032+
getDocsNonInteractive hsc_env mod name = do
1033+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ getDocsNonInteractive' name
1034+
pure $ maybeToEither errs res
1035+
1036+
10191037
-- | Non-interactive, batch version of 'GHC.Runtime.Eval.getDocs'.
10201038
getDocsBatch
10211039
:: HscEnv
@@ -1024,13 +1042,10 @@ getDocsBatch
10241042
-- 2021-11-18: NOTE: Map Int would become IntMap if next GHCs.
10251043
-> IO (Either ErrorMessages (Map.Map Name (Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))))
10261044
-- ^ 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
1045+
getDocsBatch hsc_env mod names = do
1046+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ Map.fromList <$> traverse getDocsNonInteractive' names
10291047
pure $ maybeToEither errs res
10301048

1031-
fakeSpan :: RealSrcSpan
1032-
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
1033-
10341049
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
10351050
-- The interactive paths create problems in ghc-lib builds
10361051
--- and leads to fun errors like "Cannot continue after interface file error".
@@ -1039,7 +1054,7 @@ lookupName :: HscEnv
10391054
-> Name
10401055
-> IO (Maybe TyThing)
10411056
lookupName hsc_env mod name = do
1042-
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
1057+
(_messages, res) <- initTypecheckEnv hsc_env mod $ do
10431058
tcthing <- tcLookup name
10441059
case tcthing of
10451060
AGlobal thing -> return thing

0 commit comments

Comments
 (0)