diff --git a/src/Juvix/Compiler/Abstract/Extra/DependencyBuilder.hs b/src/Juvix/Compiler/Abstract/Extra/DependencyBuilder.hs index e04059169..84fe31a02 100644 --- a/src/Juvix/Compiler/Abstract/Extra/DependencyBuilder.hs +++ b/src/Juvix/Compiler/Abstract/Extra/DependencyBuilder.hs @@ -10,9 +10,13 @@ import Juvix.Prelude -- adjacency set representation type DependencyGraph = HashMap Name (HashSet Name) -type StartNodes = HashSet Name +newtype VisitedModules = VisitedModules + { _visitedModulesSet :: HashSet Name + } -type VisitedModules = HashSet Name +makeLenses ''VisitedModules + +type StartNodes = HashSet Name type ExportsTable = HashSet NameId @@ -40,7 +44,7 @@ buildDependencyInfoHelper tbl m = createDependencyInfo graph startNodes graph :: DependencyGraph (startNodes, graph) = run $ - evalState (HashSet.empty :: VisitedModules) $ + evalState (VisitedModules mempty) $ runState HashSet.empty $ execState HashMap.empty $ runReader tbl m @@ -86,8 +90,8 @@ checkBuiltinInductiveStartNode i = whenJust (i ^. inductiveBuiltin) go guardNotVisited :: (Member (State VisitedModules) r) => Name -> Sem r () -> Sem r () guardNotVisited n cont = unlessM - (HashSet.member n <$> get) - (modify (HashSet.insert n) >> cont) + (HashSet.member n . (^. visitedModulesSet) <$> get) + (modify (over visitedModulesSet (HashSet.insert n)) >> cont) goModule :: (Members '[Reader ExportsTable, State DependencyGraph, State StartNodes, State VisitedModules] r) => Module -> Sem r () goModule m = do diff --git a/src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/Reachability.hs b/src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/Reachability.hs index 2a701b4d8..35b3cada1 100644 --- a/src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/Reachability.hs +++ b/src/Juvix/Compiler/Internal/Translation/FromInternal/Analysis/Reachability.hs @@ -3,16 +3,15 @@ module Juvix.Compiler.Internal.Translation.FromInternal.Analysis.Reachability wh import Juvix.Compiler.Abstract.Data.NameDependencyInfo import Juvix.Compiler.Internal.Language import Juvix.Compiler.Internal.Translation.FromAbstract.Data.Context --- import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Data.Context qualified as MicroTyped -import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking qualified as MicroArity -import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Data.Context qualified as MicroTyped +import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.ArityChecking qualified as Arity +import Juvix.Compiler.Internal.Translation.FromInternal.Analysis.TypeChecking.Data.Context qualified as Typed import Juvix.Prelude -filterUnreachable :: MicroTyped.InternalTypedResult -> MicroTyped.InternalTypedResult -filterUnreachable r = r {MicroTyped._resultModules = modules'} +filterUnreachable :: Typed.InternalTypedResult -> Typed.InternalTypedResult +filterUnreachable r = r {Typed._resultModules = modules'} where - depInfo = r ^. (MicroTyped.resultInternalArityResult . MicroArity.resultInternalResult . resultDepInfo) - modules = r ^. MicroTyped.resultModules + depInfo = r ^. (Typed.resultInternalArityResult . Arity.resultInternalResult . resultDepInfo) + modules = r ^. Typed.resultModules modules' = run $ runReader depInfo (mapM goModule modules) askIsReachable :: Member (Reader NameDependencyInfo) r => Name -> Sem r Bool @@ -43,6 +42,4 @@ goStatement s = case s of StatementInclude i -> do m <- goModule (i ^. includeModule) return (Just (StatementInclude i {_includeModule = m})) - StatementModule m -> do - m' <- StatementModule <$> goModule m - returnIfReachable (m ^. moduleName) m' + StatementModule m -> Just . StatementModule <$> goModule m diff --git a/test/Internal/Eval/Positive.hs b/test/Internal/Eval/Positive.hs index bbe41f1b2..01b7da42b 100644 --- a/test/Internal/Eval/Positive.hs +++ b/test/Internal/Eval/Positive.hs @@ -188,5 +188,10 @@ tests = "Case expression" $(mkRelDir ".") $(mkRelFile "Case.juvix") - $(mkRelFile "out/Case.out") + $(mkRelFile "out/Case.out"), + PosTest + "Import a module containing a nested module" + $(mkRelDir "NestedModuleScope") + $(mkRelFile "Import.juvix") + $(mkRelFile "out/NestedModuleScope.out") ] diff --git a/tests/Internal/positive/NestedModuleScope/Base.juvix b/tests/Internal/positive/NestedModuleScope/Base.juvix new file mode 100644 index 000000000..3b09f0dad --- /dev/null +++ b/tests/Internal/positive/NestedModuleScope/Base.juvix @@ -0,0 +1,5 @@ +module Base; + +module NestedBase; + open import Base2 public; +end; diff --git a/tests/Internal/positive/NestedModuleScope/Base2.juvix b/tests/Internal/positive/NestedModuleScope/Base2.juvix new file mode 100644 index 000000000..7e7e5c035 --- /dev/null +++ b/tests/Internal/positive/NestedModuleScope/Base2.juvix @@ -0,0 +1,3 @@ +module Base2; + +type Foo := foo : Foo; diff --git a/tests/Internal/positive/NestedModuleScope/Import.juvix b/tests/Internal/positive/NestedModuleScope/Import.juvix new file mode 100644 index 000000000..3380568e9 --- /dev/null +++ b/tests/Internal/positive/NestedModuleScope/Import.juvix @@ -0,0 +1,7 @@ +module Import; + +open import Base; +open import Base2; + +main : Foo; +main := foo; diff --git a/tests/Internal/positive/NestedModuleScope/juvix.yaml b/tests/Internal/positive/NestedModuleScope/juvix.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Internal/positive/NestedModuleScope/out/NestedModuleScope.out b/tests/Internal/positive/NestedModuleScope/out/NestedModuleScope.out new file mode 100644 index 000000000..257cc5642 --- /dev/null +++ b/tests/Internal/positive/NestedModuleScope/out/NestedModuleScope.out @@ -0,0 +1 @@ +foo