Skip to content

Commit 44de0dc

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 bcc2c8d commit 44de0dc

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
@@ -986,12 +988,20 @@ mkDetailsFromIface session iface linkable = do
986988
initIfaceLoad hsc' (typecheckIface iface)
987989
return (HomeModInfo iface details linkable)
988990

991+
fakeSpan :: RealSrcSpan
992+
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
989993

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

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

1027-
fakeSpan :: RealSrcSpan
1028-
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
1029-
10301046
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
10311047
-- The interactive paths create problems in ghc-lib builds
10321048
--- and leads to fun errors like "Cannot continue after interface file error".
@@ -1035,7 +1051,7 @@ lookupName :: HscEnv
10351051
-> Name
10361052
-> IO (Maybe TyThing)
10371053
lookupName hsc_env mod name = do
1038-
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
1054+
(_messages, res) <- initTypecheckEnv hsc_env mod $ do
10391055
tcthing <- tcLookup name
10401056
case tcthing of
10411057
AGlobal thing -> return thing

0 commit comments

Comments
 (0)