~jojo/Carth

945c7349292858c6cc7714ba7fdf4e6a0fe3806e — JoJo 5 months ago 8f78cbc
Fix bug when getting free vars of non-rec Let in Monomorphic

Was using fvLet for both recursive and nonrecursive bindings, but for
nonrecursive let:s, the LHS should not be subtracted from the RHS FV set.
2 files changed, 6 insertions(+), 5 deletions(-)

M src/Monomorphic.hs
M src/Parsed.hs
M src/Monomorphic.hs => src/Monomorphic.hs +3 -2
@@ 102,8 102,9 @@ fvExpr' = \case
    App f a _ -> fvApp f a
    If p c a -> fvIf p c a
    Fun (p, (b, _)) -> fvFun p b
    Let d (Expr _ e) ->
        let bs = defToVarDefs d in fvLet (unzip (map (second (snd . unpos)) bs)) e
    Let (VarDef (lhs, WithPos _ (_, rhs))) (Expr _ e) ->
        Set.union (freeVars rhs) (Set.delete lhs (freeVars e))
    Let (RecDefs ds) (Expr _ e) -> fvLet (unzip (map (second (Fun . snd . unpos)) ds)) e
    Match e dt _ -> Set.union (freeVars e) (fvDecisionTree dt)
    Ction (_, _, _, as) -> Set.unions (map freeVars as)
    Sizeof _t -> Set.empty

M src/Parsed.hs => src/Parsed.hs +3 -3
@@ 97,9 97,9 @@ instance Eq Pat where

instance FreeVars Def (Id 'Small) where
    freeVars = \case
        VarDef _ _ _ body -> freeVars body
        FunDef _ _ _ pats body ->
            Set.difference (freeVars body) (Set.unions (map bvPat pats))
        VarDef _ _ _ rhs -> freeVars rhs
        FunDef _ _ _ pats rhs ->
            Set.difference (freeVars rhs) (Set.unions (map bvPat pats))

instance FreeVars DefLike (Id 'Small) where
    freeVars = \case