Skip to content

Commit

Permalink
Add nodes with no edges to the dependency graph (#2390)
Browse files Browse the repository at this point in the history
- Closes #2373 

Consider this:
```
let 
 x : _ := 0
in ...
```
When translating the let to internal, we build the dependency graph and
then use that to group definitions in mutually recursive blocks. Since
`x` has no edge, it was not being added to the dependency graph, so it
was not being translated to Internal, thus crashing later during
inference.
  • Loading branch information
janmasrovira authored Sep 26, 2023
1 parent 0cf9931 commit 27df394
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Juvix/Compiler/Internal/Extra/DependencyBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ addStartNode n = modify (HashSet.insert n)
addEdgeMay :: (Member (State DependencyGraph) r) => Maybe Name -> Name -> Sem r ()
addEdgeMay mn1 n2 = whenJust mn1 $ \n1 -> addEdge n1 n2

addNode :: (Member (State DependencyGraph) r) => Name -> Sem r ()
addNode n =
modify
( HashMap.alter
( \case
Just x -> Just x
Nothing -> Just (mempty :: HashSet Name)
)
n
)

addEdge :: (Member (State DependencyGraph) r) => Name -> Name -> Sem r ()
addEdge n1 n2 =
modify
Expand Down Expand Up @@ -179,6 +190,7 @@ goFunctionDefHelper ::
FunctionDef ->
Sem r ()
goFunctionDefHelper f = do
addNode (f ^. funDefName)
checkStartNode (f ^. funDefName)
when (f ^. funDefInstance) $
goInstance f
Expand Down
6 changes: 5 additions & 1 deletion src/Juvix/Compiler/Internal/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,13 @@ instance PrettyCode FunctionDef where
<?+> funDefName'
<+> kwColon
<+> funDefType'
<> line
<> hardline
<> vsep (toList clauses')

instance PrettyCode PreLetStatement where
ppCode = \case
PreLetFunctionDef f -> ppCode f

instance PrettyCode FunctionClause where
ppCode c = do
funName <- ppCode (c ^. clauseName)
Expand Down
2 changes: 2 additions & 0 deletions src/Juvix/Compiler/Internal/Translation/FromConcrete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ buildLetMutualBlocks ss = nonEmpty' . mapMaybe nameToPreStatement $ scomponents
where
-- TODO buildDependencyInfoLet is repeating too much work when there are big nested lets
depInfo = buildDependencyInfoLet ss

scomponents :: [SCC Internal.Name] = buildSCCs depInfo

statementsByName :: HashMap Internal.Name Internal.PreLetStatement
statementsByName = HashMap.fromList (map mkAssoc (toList ss))
where
Expand Down
4 changes: 4 additions & 0 deletions test/Typecheck/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ tests =
posTest
"Instance import"
$(mkRelDir "InstanceImport")
$(mkRelFile "Main.juvix"),
posTest
"Hole as numeric type"
$(mkRelDir "issue2373")
$(mkRelFile "Main.juvix")
]
<> [ compilationTest t | t <- Compilation.tests
Expand Down
10 changes: 10 additions & 0 deletions tests/positive/issue2373/Main.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Main;

import Stdlib.Data.Nat open;
import Stdlib.Data.Bool open;

main : Nat :=
let
y : Nat := 0;
x : _ := 0;
in x;
4 changes: 4 additions & 0 deletions tests/positive/issue2373/juvix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
- .juvix-build/stdlib/
name: issue2373
version: 0.0.0

0 comments on commit 27df394

Please sign in to comment.