Skip to content

Commit 6fa9d7b

Browse files
committed
Use closed world assumption for core and .hie files
1 parent 214971e commit 6fa9d7b

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTi
6666
import Data.Tuple.Extra (dupe)
6767
import Data.Unique as Unique
6868
import Debug.Trace
69+
import Development.IDE.Core.FileStore (resetInterfaceStore)
6970
import Development.IDE.Core.Preprocessor
7071
import Development.IDE.Core.RuleTypes
7172
import Development.IDE.Core.Shake
@@ -396,7 +397,7 @@ mkHiFileResultCompile se session' tcm simplified_guts ltype = catchErrs $ do
396397

397398
let genLinkable = case ltype of
398399
ObjectLinkable -> generateObjectCode
399-
BCOLinkable -> generateByteCode WriteCoreFile
400+
BCOLinkable -> generateByteCode se WriteCoreFile
400401

401402
(linkable, details, mguts, diags) <-
402403
if mg_hsc_src simplified_guts == HsBootFile
@@ -545,8 +546,8 @@ generateObjectCode session summary guts = do
545546

546547
data WriteCoreFile = WriteCoreFile | CoreFileExists !UTCTime
547548

548-
generateByteCode :: WriteCoreFile -> HscEnv -> ModSummary -> CgGuts -> IO (IdeResult Linkable)
549-
generateByteCode write_core hscEnv summary guts = do
549+
generateByteCode :: ShakeExtras -> WriteCoreFile -> HscEnv -> ModSummary -> CgGuts -> IO (IdeResult Linkable)
550+
generateByteCode se write_core hscEnv summary guts = do
550551
fmap (either (, Nothing) (second Just)) $
551552
catchSrcErrors (hsc_dflags hscEnv) "bytecode" $ do
552553
(warnings, (_, bytecode, sptEntries)) <-
@@ -566,7 +567,7 @@ generateByteCode write_core hscEnv summary guts = do
566567
WriteCoreFile -> liftIO $ do
567568
let core_fp = ml_core_file $ ms_location summary
568569
core_file = codeGutsToCoreFile guts
569-
atomicFileWrite core_fp $ \fp ->
570+
atomicFileWrite se core_fp $ \fp ->
570571
writeBinCoreFile fp core_file
571572
getModificationTime core_fp
572573
let linkable = LM time (ms_mod summary) [unlinked]
@@ -651,12 +652,14 @@ addRelativeImport :: NormalizedFilePath -> ModuleName -> DynFlags -> DynFlags
651652
addRelativeImport fp modu dflags = dflags
652653
{importPaths = nubOrd $ maybeToList (moduleImportPath fp modu) ++ importPaths dflags}
653654

654-
atomicFileWrite :: FilePath -> (FilePath -> IO a) -> IO ()
655-
atomicFileWrite targetPath write = do
655+
-- | Also resets the interface store
656+
atomicFileWrite :: ShakeExtras -> FilePath -> (FilePath -> IO a) -> IO ()
657+
atomicFileWrite se targetPath write = do
656658
let dir = takeDirectory targetPath
657659
createDirectoryIfMissing True dir
658660
(tempFilePath, cleanUp) <- newTempFileWithin dir
659-
(write tempFilePath >> renameFile tempFilePath targetPath) `onException` cleanUp
661+
(write tempFilePath >> renameFile tempFilePath targetPath >> atomically (resetInterfaceStore se (toNormalizedFilePath' targetPath)))
662+
`onException` cleanUp
660663

661664
generateHieAsts :: HscEnv -> TcModuleResult -> IO ([FileDiagnostic], Maybe (HieASTs Type))
662665
generateHieAsts hscEnv tcm =
@@ -834,18 +837,18 @@ writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =
834837
handleGenerationErrors dflags "extended interface write/compression" $ do
835838
hf <- runHsc hscEnv $
836839
GHC.mkHieFile' mod_summary exports ast source
837-
atomicFileWrite targetPath $ flip GHC.writeHieFile hf
840+
atomicFileWrite se targetPath $ flip GHC.writeHieFile hf
838841
hash <- Util.getFileHash targetPath
839842
indexHieFile se mod_summary srcPath hash hf
840843
where
841844
dflags = hsc_dflags hscEnv
842845
mod_location = ms_location mod_summary
843846
targetPath = Compat.ml_hie_file mod_location
844847

845-
writeHiFile :: HscEnv -> HiFileResult -> IO [FileDiagnostic]
846-
writeHiFile hscEnv tc =
848+
writeHiFile :: ShakeExtras -> HscEnv -> HiFileResult -> IO [FileDiagnostic]
849+
writeHiFile se hscEnv tc =
847850
handleGenerationErrors dflags "interface write" $ do
848-
atomicFileWrite targetPath $ \fp ->
851+
atomicFileWrite se targetPath $ \fp ->
849852
writeIfaceFile hscEnv fp modIface
850853
where
851854
modIface = hm_iface $ hirHomeMod tc
@@ -1210,12 +1213,13 @@ ml_core_file ml = ml_hi_file ml <.> "core"
12101213
-- See Note [Recompilation avoidance in the presence of TH]
12111214
loadInterface
12121215
:: (MonadIO m, MonadMask m)
1213-
=> HscEnv
1216+
=> ShakeExtras
1217+
-> HscEnv
12141218
-> ModSummary
12151219
-> Maybe LinkableType
12161220
-> RecompilationInfo m
12171221
-> m ([FileDiagnostic], Maybe HiFileResult)
1218-
loadInterface session ms linkableNeeded RecompilationInfo{..} = do
1222+
loadInterface se session ms linkableNeeded RecompilationInfo{..} = do
12191223
let sessionWithMsDynFlags = hscSetFlags (ms_hspp_opts ms) session
12201224
mb_old_iface = hm_iface . hirHomeMod . fst <$> old_value
12211225
mb_old_version = snd <$> old_value
@@ -1309,7 +1313,7 @@ loadInterface session ms linkableNeeded RecompilationInfo{..} = do
13091313
-> do_regenerate msg
13101314
| otherwise -> return ([], Just old_hir)
13111315
Nothing -> do
1312-
(warns, hmi) <- liftIO $ mkDetailsFromIface sessionWithMsDynFlags ms iface lb
1316+
(warns, hmi) <- liftIO $ mkDetailsFromIface se sessionWithMsDynFlags ms iface lb
13131317
-- parse the runtime dependencies from the annotations
13141318
let runtime_deps
13151319
| not (mi_used_th iface) = emptyModuleEnv
@@ -1361,8 +1365,8 @@ showReason UpToDate = "UpToDate"
13611365
showReason MustCompile = "MustCompile"
13621366
showReason (RecompBecause s) = s
13631367

1364-
mkDetailsFromIface :: HscEnv -> ModSummary -> ModIface -> Maybe IdeLinkable -> IO ([FileDiagnostic], HomeModInfo)
1365-
mkDetailsFromIface session ms iface ide_linkable = do
1368+
mkDetailsFromIface :: ShakeExtras -> HscEnv -> ModSummary -> ModIface -> Maybe IdeLinkable -> IO ([FileDiagnostic], HomeModInfo)
1369+
mkDetailsFromIface se session ms iface ide_linkable = do
13661370
details <- liftIO $ fixIO $ \details -> do
13671371
let hsc' = session { hsc_HPT = addToHpt (hsc_HPT session) (moduleName $ mi_module iface) (HomeModInfo iface details Nothing) }
13681372
initIfaceLoad hsc' (typecheckIface iface)
@@ -1371,7 +1375,7 @@ mkDetailsFromIface session ms iface ide_linkable = do
13711375
Just (GhcLinkable lb) -> pure ([], Just lb)
13721376
Just (CoreLinkable t core_file) -> do
13731377
cgi_guts <- coreFileToCgGuts session iface details core_file
1374-
generateByteCode (CoreFileExists t) session ms cgi_guts
1378+
generateByteCode se (CoreFileExists t) session ms cgi_guts
13751379

13761380
return (warns, HomeModInfo iface details linkable)
13771381

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ getModificationTimeImpl missingFileDiags file = do
151151
-- But interface files are private, in that only HLS writes them.
152152
-- So we implement watching ourselves, and bypass the need for alwaysRerun.
153153
isInterface :: NormalizedFilePath -> Bool
154-
isInterface f = takeExtension (fromNormalizedFilePath f) `elem` [".hi", ".hi-boot"]
154+
isInterface f = takeExtension (fromNormalizedFilePath f) `elem` [".hi", ".hi-boot", ".hie", ".hie-boot", ".core"]
155155

156156
-- | Reset the GetModificationTime state of interface files
157157
resetInterfaceStore :: ShakeExtras -> NormalizedFilePath -> STM ()

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ getModIfaceFromDiskRule recorder = defineEarlyCutoff (cmapWithPrio LogShake reco
767767
Just session -> do
768768
linkableType <- getLinkableType f
769769
ver <- use_ GetModificationTime f
770-
ShakeExtras{ideNc} <- getShakeExtras
770+
se@ShakeExtras{ideNc} <- getShakeExtras
771771
let m_old = case old of
772772
Shake.Succeeded (Just old_version) v -> Just (v, old_version)
773773
Shake.Stale _ (Just old_version) v -> Just (v, old_version)
@@ -778,7 +778,7 @@ getModIfaceFromDiskRule recorder = defineEarlyCutoff (cmapWithPrio LogShake reco
778778
, get_file_version = use GetModificationTime_{missingFileDiagnostics = False}
779779
, regenerate = regenerateHiFile session f ms
780780
}
781-
r <- loadInterface (hscEnv session) ms linkableType recompInfo
781+
r <- loadInterface se (hscEnv session) ms linkableType recompInfo
782782
case r of
783783
(diags, Nothing) -> return (Nothing, (diags, Nothing))
784784
(diags, Just x) -> do
@@ -901,7 +901,7 @@ getModIfaceRule recorder = defineEarlyCutoff (cmapWithPrio LogShake recorder) $
901901
hiDiags <- case hiFile of
902902
Just hiFile
903903
| OnDisk <- status
904-
, not (tmrDeferedError tmr) -> writeHiFileAction hsc hiFile
904+
, not (tmrDeferedError tmr) -> liftIO $ writeHiFile se hsc hiFile
905905
_ -> pure []
906906
return (fp, (diags++hiDiags, hiFile))
907907
NotFOI -> do
@@ -947,6 +947,7 @@ regenerateHiFile sess f ms compNeeded = do
947947
let compile = compileModule (RunSimplifier True) hsc (pm_mod_summary pm) $ tmrTypechecked tmr
948948

949949
se <- getShakeExtras
950+
950951
-- Bang pattern is important to avoid leaking 'tmr'
951952
(diags'', !res) <- liftIO $ compileToObjCodeIfNeeded se hsc compNeeded compile tmr
952953

@@ -966,7 +967,7 @@ regenerateHiFile sess f ms compNeeded = do
966967
-- We don't write the `.hi` file if there are defered errors, since we won't get
967968
-- accurate diagnostics next time if we do
968969
hiDiags <- if not $ tmrDeferedError tmr
969-
then writeHiFileAction hsc hiFile
970+
then liftIO $ writeHiFile se hsc hiFile
970971
else pure []
971972

972973
pure (hiDiags <> gDiags <> concat wDiags)
@@ -1087,14 +1088,6 @@ computeLinkableTypeForDynFlags d
10871088
newtype CompiledLinkables = CompiledLinkables { getCompiledLinkables :: Var (ModuleEnv UTCTime) }
10881089
instance IsIdeGlobal CompiledLinkables
10891090

1090-
writeHiFileAction :: HscEnv -> HiFileResult -> Action [FileDiagnostic]
1091-
writeHiFileAction hsc hiFile = do
1092-
extras <- getShakeExtras
1093-
let targetPath = Compat.ml_hi_file $ ms_location $ hirModSummary hiFile
1094-
liftIO $ do
1095-
atomically $ resetInterfaceStore extras $ toNormalizedFilePath' targetPath
1096-
writeHiFile hsc hiFile
1097-
10981091
data RulesConfig = RulesConfig
10991092
{ -- | Disable import cycle checking for improved performance in large codebases
11001093
checkForImportCycles :: Bool

0 commit comments

Comments
 (0)