Skip to content

Commit 51113a9

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 3de4c48 commit 51113a9

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
@@ -989,12 +990,20 @@ mkDetailsFromIface session iface linkable = do
989990
initIfaceLoad hsc' (typecheckIface iface)
990991
return (HomeModInfo iface details linkable)
991992

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

993-
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
994-
-- The interactive paths create problems in ghc-lib builds
995-
--- and lead to fun errors like "Cannot continue after interface file error".
996-
getDocsNonInteractive :: Name -> IOEnv (Env TcGblEnv TcLclEnv) (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
997-
getDocsNonInteractive name = do
996+
initTypecheckEnv :: HscEnv -> Module -> TcRn r -> IO (Messages, Maybe r)
997+
initTypecheckEnv hsc_env mod = initTc hsc_env HsSrcFile False mod fakeSpan
998+
999+
getDocsNonInteractive'
1000+
:: Name
1001+
-> IOEnv
1002+
(Env TcGblEnv TcLclEnv)
1003+
(Name,
1004+
Either
1005+
GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
1006+
getDocsNonInteractive' name =
9981007
case nameModule_maybe name of
9991008
Nothing -> return (name, Left $ NameHasNoModule name)
10001009
Just mod -> do
@@ -1006,7 +1015,7 @@ getDocsNonInteractive name = do
10061015
<- loadModuleInterface "getModuleInterface" mod
10071016
let
10081017
isNameCompiled =
1009-
-- TODO: Find a more direct indicator.
1018+
-- comment from GHC: Find a more direct indicator.
10101019
case nameSrcLoc name of
10111020
RealSrcLoc {} -> False
10121021
UnhelpfulLoc {} -> True
@@ -1015,20 +1024,26 @@ getDocsNonInteractive name = do
10151024
then Left $ NoDocsInIface mod isNameCompiled
10161025
else Right (Map.lookup name dmap, Map.lookup name amap)
10171026

1027+
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
1028+
-- The interactive paths create problems in ghc-lib builds
1029+
--- and lead to fun errors like "Cannot continue after interface file error".
1030+
getDocsNonInteractive :: HscEnv -> Module -> Name -> IO (Either ErrorMessages (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString))))
1031+
getDocsNonInteractive hsc_env mod name = do
1032+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ getDocsNonInteractive' name
1033+
pure $ maybeToEither errs res
1034+
1035+
10181036
-- | Non-interactive, batch version of 'GHC.Runtime.Eval.getDocs'.
10191037
getDocsBatch
10201038
:: HscEnv
10211039
-> Module -- ^ a moudle where the names are in scope
10221040
-> [Name]
10231041
-> IO (Either ErrorMessages (Map.Map Name (Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))))
10241042
-- ^ Return a 'Map' of 'Name's to 'Either' (no docs messages) (general doc body & arg docs)
1025-
getDocsBatch hsc_env _mod _names = do
1026-
((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ Map.fromList <$> traverse getDocsNonInteractive _names
1043+
getDocsBatch hsc_env mod names = do
1044+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ Map.fromList <$> traverse getDocsNonInteractive' names
10271045
pure $ maybeToEither errs res
10281046

1029-
fakeSpan :: RealSrcSpan
1030-
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
1031-
10321047
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
10331048
-- The interactive paths create problems in ghc-lib builds
10341049
--- and leads to fun errors like "Cannot continue after interface file error".
@@ -1037,7 +1052,7 @@ lookupName :: HscEnv
10371052
-> Name
10381053
-> IO (Maybe TyThing)
10391054
lookupName hsc_env mod name = do
1040-
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
1055+
(_messages, res) <- initTypecheckEnv hsc_env mod $ do
10411056
tcthing <- tcLookup name
10421057
case tcthing of
10431058
AGlobal thing -> return thing

0 commit comments

Comments
 (0)