From 02e9f89e5e55d68eea6ff84c9c30aef8707c5dfa Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Fri, 2 Jun 2023 12:12:02 +0200 Subject: [PATCH] Properly scan imports inside local modules (#2165) - Closes #2163 Nested imports where not scanned properly when building the Internal InfoTable. That is no fixed --- .../Internal/Translation/FromAbstract.hs | 26 +++++++++---------- test/Typecheck/Positive.hs | 4 +++ tests/positive/issue2163/Main.juvix | 8 ++++++ tests/positive/issue2163/juvix.yaml | 4 +++ 4 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 tests/positive/issue2163/Main.juvix create mode 100644 tests/positive/issue2163/juvix.yaml diff --git a/src/Juvix/Compiler/Internal/Translation/FromAbstract.hs b/src/Juvix/Compiler/Internal/Translation/FromAbstract.hs index 57646a3658..c11080b102 100644 --- a/src/Juvix/Compiler/Internal/Translation/FromAbstract.hs +++ b/src/Juvix/Compiler/Internal/Translation/FromAbstract.hs @@ -182,15 +182,26 @@ goDefinition = \case Abstract.StatementAxiom a -> pure . PreAxiomDef <$> goAxiomDef a Abstract.StatementImport {} -> return [] +scanImports :: Abstract.ModuleBody -> [Abstract.TopModule] +scanImports (Abstract.ModuleBody stmts) = mconcatMap go stmts + where + go :: Abstract.Statement -> [Abstract.TopModule] + go = \case + Abstract.StatementLocalModule m -> scanImports (m ^. Abstract.moduleBody) + Abstract.StatementImport t -> [t] + Abstract.StatementInductive {} -> [] + Abstract.StatementFunction {} -> [] + Abstract.StatementAxiom {} -> [] + goModuleBody :: forall r. Members '[Reader ExportsTable, Reader NameDependencyInfo, State TranslationState, NameIdGen] r => Abstract.ModuleBody -> Sem r ModuleBody -goModuleBody (Abstract.ModuleBody stmts) = do +goModuleBody b@(Abstract.ModuleBody stmts) = do preDefs <- concatMapM goDefinition stmts sccs <- buildMutualBlocks preDefs - let imports :: [Abstract.TopModule] = [t | Abstract.StatementImport t <- stmts] + let imports :: [Abstract.TopModule] = scanImports b statements' = map goSCC sccs imports' <- map StatementInclude <$> mapMaybeM goImport imports return @@ -236,17 +247,6 @@ goImport m = do } ) --- goStatement :: --- (Members '[Reader ExportsTable, State TranslationState, NameIdGen, Reader NameDependencyInfo] r) => --- Abstract.Statement -> --- Sem r (Maybe Statement) --- goStatement = \case --- Abstract.StatementAxiom d -> Just . StatementAxiom <$> goAxiomDef d --- Abstract.StatementFunction {} -> return Nothing --- Abstract.StatementImport i -> fmap StatementInclude <$> goImport i --- Abstract.StatementLocalModule m -> Just . StatementModule <$> goModule m --- Abstract.StatementInductive i -> Just . StatementInductive <$> goInductiveDef i - goTypeIden :: Abstract.Iden -> Iden goTypeIden = \case Abstract.IdenFunction f -> IdenFunction (f ^. Abstract.functionRefName) diff --git a/test/Typecheck/Positive.hs b/test/Typecheck/Positive.hs index b55cb00a6a..e0769cf888 100644 --- a/test/Typecheck/Positive.hs +++ b/test/Typecheck/Positive.hs @@ -225,6 +225,10 @@ tests = "Mutual inference inside let" $(mkRelDir ".") $(mkRelFile "MutualLet.juvix"), + posTest + "import inside local module" + $(mkRelDir "issue2163") + $(mkRelFile "Main.juvix"), posTest "id application in type" $(mkRelDir ".") diff --git a/tests/positive/issue2163/Main.juvix b/tests/positive/issue2163/Main.juvix new file mode 100644 index 0000000000..3e910a2dbe --- /dev/null +++ b/tests/positive/issue2163/Main.juvix @@ -0,0 +1,8 @@ +module Main; + +module B; + import Stdlib.Prelude open; + + idNat : Nat -> Nat; + idNat x := x; +end; diff --git a/tests/positive/issue2163/juvix.yaml b/tests/positive/issue2163/juvix.yaml new file mode 100644 index 0000000000..c38b275a4e --- /dev/null +++ b/tests/positive/issue2163/juvix.yaml @@ -0,0 +1,4 @@ +dependencies: +- .juvix-build/stdlib/ +name: issue2163 +version: 0.0.0