From 4dcbf7911d86c1e27372708cade537625b5c1fff Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Thu, 19 Dec 2024 12:05:39 +0100 Subject: [PATCH] Fix node scanning for ImportTree (#3251) - Fixes #3244 The crash occurred because the ImportTree was built incorrectly. In particular, some nodes were scanned with an incorrect resolver root. --- src/Juvix/Compiler/Concrete/Print/Base.hs | 38 +++++++++++++++++-- src/Juvix/Compiler/Pipeline/Driver.hs | 4 +- .../Loader/PathResolver/ImportTree.hs | 4 +- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Juvix/Compiler/Concrete/Print/Base.hs b/src/Juvix/Compiler/Concrete/Print/Base.hs index 7ea873494c..957e852d3f 100644 --- a/src/Juvix/Compiler/Concrete/Print/Base.hs +++ b/src/Juvix/Compiler/Concrete/Print/Base.hs @@ -20,10 +20,11 @@ import Juvix.Compiler.Concrete.Language.Base import Juvix.Compiler.Concrete.Pretty.Options import Juvix.Compiler.Concrete.Translation.ImportScanner.Base import Juvix.Compiler.Pipeline.Loader.PathResolver.Data +import Juvix.Compiler.Pipeline.Loader.PathResolver.PackageInfo import Juvix.Compiler.Store.Scoped.Language (Alias, ModuleSymbolEntry, PreSymbolEntry (..), ScopedModule, SymbolEntry, aliasName, moduleEntry, scopedModuleName, symbolEntry) import Juvix.Data.Ape.Base import Juvix.Data.Ape.Print -import Juvix.Data.CodeAnn (Ann, CodeAnn (..), CodeAnnReference (..), ppStringLit) +import Juvix.Data.CodeAnn (Ann, CodeAnn (..), CodeAnnReference (..), ppCodeAnn, ppStringLit) import Juvix.Data.CodeAnn qualified as C import Juvix.Data.Effect.ExactPrint import Juvix.Data.Keyword.All qualified as Kw @@ -1314,6 +1315,19 @@ instance (SingI s) => PrettyPrint (UsingItem s) where kwmodule = ppCode <$> (ui ^. usingModuleKw) kwmodule (sym' <+?> kwAs' <+?> alias') +instance PrettyPrint PackageInfo where + ppCode PackageInfo {..} = do + header ("Package name: " <> (_packagePackage ^. packageLikeName)) + noLoc ("root:" P.<+> pretty _packageRoot) + let roots = case nonEmpty _packageAvailableRoots of + Nothing -> return () + Just roots1 -> hardline <> indent (itemize (fmap (noLoc . pretty) roots1)) + hardline + noLoc ("available roots:") <> roots + hardline + noLoc ("package id:" P.<+> ppCodeAnn _packageInfoPackageId) + hardline + instance PrettyPrint ImportTreeStats where ppCode ImportTreeStats {..} = do header "Import Tree Statistics:" @@ -1331,19 +1345,35 @@ instance PrettyPrint ImportTree where header "============" hardline + header ("Packages (" <> show (length importsTable) <> "):") + header "=========" + itemize . map (noLoc . pretty) $ Map.keys importsTable + hardline + + hardline forM_ (Map.toList importsTable) $ \(pkgRoot, tbl :: Map (Path Rel File) (Set ImportNode)) -> do annotated AnnImportant (noLoc ("* Package at " <> pretty pkgRoot)) hardline let pkgNodes :: HashSet ImportNode = fromJust (nodesByRoot ^. at pkgRoot) - header ("Nodes (" <> show (length pkgNodes) <> ")") + header ("Nodes Relative paths (" <> show (length pkgNodes) <> ")") forM_ pkgNodes $ \node -> do noLoc (pMod (node ^. importNodeFile)) hardline hardline + header ("Nodes Absolute paths (" <> show (length pkgNodes) <> ")") + forM_ pkgNodes $ \node -> do + noLoc (pMod (node ^. importNodeAbsFile)) + hardline + hardline let numEdges = sum (map length (toList tbl)) header ("Edges (" <> show numEdges <> ")") - forM_ (Map.toList tbl) $ \(fromFile, toFiles) -> do - noLoc (pMod fromFile P.<+> annotate AnnKeyword "imports" P.<+> "(" <> pretty (length toFiles) <> "):") + forM_ (Map.toList tbl) $ \(fromFile, toFiles :: Set ImportNode) -> do + let fromNode :: ImportNode = + ImportNode + { _importNodePackageRoot = pkgRoot, + _importNodeFile = fromFile + } + noLoc (pMod fromFile P.<+> "at" P.<+> pMod (fromNode ^. importNodeAbsFile) P.<+> annotate AnnKeyword "imports" P.<+> "(" <> pretty (length toFiles) <> "):") hardline indent . itemize . (`map` (toList toFiles)) $ \toFile -> do let toMod diff --git a/src/Juvix/Compiler/Pipeline/Driver.hs b/src/Juvix/Compiler/Pipeline/Driver.hs index 7bdb65907a..c0818f3c7c 100644 --- a/src/Juvix/Compiler/Pipeline/Driver.hs +++ b/src/Juvix/Compiler/Pipeline/Driver.hs @@ -335,9 +335,7 @@ processImport p = withPathFile p getCachedImport hasParallelSupport <- supportsParallel eix <- mkEntryIndex node if - | hasParallelSupport -> do - res <- cacheGetResult eix - return (res ^. cacheResult) + | hasParallelSupport -> cacheGet eix | otherwise -> processModule eix processFileUpToParsing :: diff --git a/src/Juvix/Compiler/Pipeline/Loader/PathResolver/ImportTree.hs b/src/Juvix/Compiler/Pipeline/Loader/PathResolver/ImportTree.hs index ea78b53b8c..1a9cfb5450 100644 --- a/src/Juvix/Compiler/Pipeline/Loader/PathResolver/ImportTree.hs +++ b/src/Juvix/Compiler/Pipeline/Loader/PathResolver/ImportTree.hs @@ -78,12 +78,12 @@ mkImportTree mentrypointModulePath = ) => ImportNode -> Sem r' () - scanNode fromNode = withImportNode fromNode $ do + scanNode fromNode = withResolverRoot (fromNode ^. importNodePackageRoot) . withImportNode fromNode $ do scans <- toList <$> getNodeImports fromNode imports :: [ImportNode] <- mapM resolveImportScan scans forM_ (zipExact scans imports) $ \(importscan, toNode) -> do importTreeAddEdge importscan toNode - withResolverRoot (toNode ^. importNodePackageRoot) (visit toNode) + visit toNode resolveImportScan :: forall r'. (Members '[PathResolver] r') => ImportScan -> Sem r' ImportNode resolveImportScan s = do