Skip to content

Commit b751674

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: #2371
1 parent 0a5900d commit b751674

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
@@ -27,6 +27,7 @@ module Development.IDE.Core.Compile
2727
, loadHieFile
2828
, loadInterface
2929
, loadModulesHome
30+
, getDocsNonInteractive
3031
, getDocsBatch
3132
, lookupName
3233
,mergeEnvs) where
@@ -1016,12 +1017,20 @@ mkDetailsFromIface session iface linkable = do
10161017
initIfaceLoad hsc' (typecheckIface iface)
10171018
return (HomeModInfo iface details linkable)
10181019

1020+
fakeSpan :: RealSrcSpan
1021+
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
10191022

1020-
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
1021-
-- The interactive paths create problems in ghc-lib builds
1022-
--- and lead to fun errors like "Cannot continue after interface file error".
1023-
getDocsNonInteractive :: Name -> IOEnv (Env TcGblEnv TcLclEnv) (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
1024-
getDocsNonInteractive name = do
1023+
initTypecheckEnv :: HscEnv -> Module -> TcRn r -> IO (Messages, Maybe r)
1024+
initTypecheckEnv hsc_env mod = initTc hsc_env HsSrcFile False mod fakeSpan
1025+
1026+
getDocsNonInteractive'
1027+
:: Name
1028+
-> IOEnv
1029+
(Env TcGblEnv TcLclEnv)
1030+
(Name,
1031+
Either
1032+
GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))
1033+
getDocsNonInteractive' name =
10251034
case nameModule_maybe name of
10261035
Nothing -> return (name, Left $ NameHasNoModule name)
10271036
Just mod -> do
@@ -1033,7 +1042,7 @@ getDocsNonInteractive name = do
10331042
<- loadModuleInterface "getModuleInterface" mod
10341043
let
10351044
isNameCompiled =
1036-
-- TODO: Find a more direct indicator.
1045+
-- comment from GHC: Find a more direct indicator.
10371046
case nameSrcLoc name of
10381047
RealSrcLoc {} -> False
10391048
UnhelpfulLoc {} -> True
@@ -1042,20 +1051,26 @@ getDocsNonInteractive name = do
10421051
then Left $ NoDocsInIface mod isNameCompiled
10431052
else Right (Map.lookup name dmap, Map.lookup name amap)
10441053

1054+
-- | Non-interactive modification of 'GHC.Runtime.Eval.getDocs'.
1055+
-- The interactive paths create problems in ghc-lib builds
1056+
--- and lead to fun errors like "Cannot continue after interface file error".
1057+
getDocsNonInteractive :: HscEnv -> Module -> Name -> IO (Either ErrorMessages (Name, Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString))))
1058+
getDocsNonInteractive hsc_env mod name = do
1059+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ getDocsNonInteractive' name
1060+
pure $ maybeToEither errs res
1061+
1062+
10451063
-- | Non-interactive, batch version of 'GHC.Runtime.Eval.getDocs'.
10461064
getDocsBatch
10471065
:: HscEnv
10481066
-> Module -- ^ a moudle where the names are in scope
10491067
-> [Name]
10501068
-> IO (Either ErrorMessages (Map.Map Name (Either GetDocsFailure (Maybe HsDocString, Maybe (Map.Map Int HsDocString)))))
10511069
-- ^ Return a 'Map' of 'Name's to 'Either' (no docs messages) (general doc body & arg docs)
1052-
getDocsBatch hsc_env _mod _names = do
1053-
((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ Map.fromList <$> traverse getDocsNonInteractive _names
1070+
getDocsBatch hsc_env mod names = do
1071+
((_warns,errs), res) <- initTypecheckEnv hsc_env mod $ Map.fromList <$> traverse getDocsNonInteractive' names
10541072
pure $ maybeToEither errs res
10551073

1056-
fakeSpan :: RealSrcSpan
1057-
fakeSpan = realSrcLocSpan $ mkRealSrcLoc (Util.fsLit "<ghcide>") 1 1
1058-
10591074
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
10601075
-- The interactive paths create problems in ghc-lib builds
10611076
--- and leads to fun errors like "Cannot continue after interface file error".
@@ -1064,7 +1079,7 @@ lookupName :: HscEnv
10641079
-> Name
10651080
-> IO (Maybe TyThing)
10661081
lookupName hsc_env mod name = do
1067-
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
1082+
(_messages, res) <- initTypecheckEnv hsc_env mod $ do
10681083
tcthing <- tcLookup name
10691084
case tcthing of
10701085
AGlobal thing -> return thing

0 commit comments

Comments
 (0)