Skip to content

Commit

Permalink
Fix node scanning for ImportTree (#3251)
Browse files Browse the repository at this point in the history
- Fixes #3244 

The crash occurred because the ImportTree was built incorrectly. In
particular, some nodes were scanned with an incorrect resolver root.
  • Loading branch information
janmasrovira authored Dec 19, 2024
1 parent 58d1f43 commit 4dcbf79
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
38 changes: 34 additions & 4 deletions src/Juvix/Compiler/Concrete/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:"
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/Juvix/Compiler/Pipeline/Driver.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::
Expand Down
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Pipeline/Loader/PathResolver/ImportTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4dcbf79

Please sign in to comment.