Skip to content

Commit 374484e

Browse files
RadvendiiAnton-Latukha
authored andcommitted
fix getFreeVars and add test
the treatment of variable bindings was of so `{x}: x` was said to have `x` as a free variable
1 parent 49acc33 commit 374484e

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/Nix/Expr/Types.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -765,18 +765,15 @@ getFreeVars e =
765765
(NHasAttr expr path) -> getFreeVars expr <> pathFree path
766766
(NAbs (Param varname) expr) -> Set.delete varname (getFreeVars expr)
767767
(NAbs (ParamSet varname _ pset) expr) ->
768-
-- Include all free variables from the expression and the default arguments
769-
getFreeVars expr <>
770-
-- But remove the argument name if existing, and all arguments in the parameter set
771768
Set.difference
772-
(Set.unions $ getFreeVars <$> mapMaybe snd pset)
773-
(Set.difference
774-
(one `whenJust` varname)
775-
(Set.fromList $ fst <$> pset)
776-
)
769+
-- Include all free variables from the expression and the default arguments
770+
(getFreeVars expr <> (Set.unions $ getFreeVars <$> mapMaybe snd pset))
771+
-- But remove the argument name if existing, and all arguments in the parameter set
772+
((one `whenJust` varname) <> (Set.fromList $ fst <$> pset))
777773
(NLet bindings expr ) ->
778-
getFreeVars expr <>
779-
diffBetween bindFreeVars bindDefs bindings
774+
Set.difference
775+
(getFreeVars expr <> bindFreeVars bindings)
776+
(bindDefs bindings)
780777
(NIf cond th el ) -> Set.unions $ getFreeVars <$> [cond, th, el]
781778
-- Evaluation is needed to find out whether x is a "real" free variable in `with y; x`, we just include it
782779
-- This also makes sense because its value can be overridden by `x: with y; x`

tests/EvalTests.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,18 @@ case_regression_373 =
455455
)
456456
]
457457

458+
case_bound_vars :: Assertion
459+
case_bound_vars =
460+
traverse_ noFreeVars
461+
[ "a: a"
462+
, "{b}: b"
463+
, "let c = 5; d = c; in d"
464+
, "rec { e = 5; f = e; }"
465+
]
466+
where
467+
noFreeVars = flip sameFreeVars mempty
468+
469+
458470
case_expression_split =
459471
constantEqualText
460472
"[ \"\" [ \"a\" ] \"c\" ]"

0 commit comments

Comments
 (0)