lang: ast: New test "clear-env-on-var"

This test detects a mistake which is easy to make: when making a
recursive call to the target of an ExprVar, it would be easy to
accidentally pass the environment, like we usually do with every other
recursive call. For variables, this is a mistake, because the lambda
parameters which are in scope where the variable is used must not be in
scope where the variable is defined.

In fact, ExprVar.Graph() currently makes this mistake. The test passes
anyway, because an earlier phase (SetScope) correctly clears the
environment and detects the problem before the Graph phase. Thus, this
test does not guarantee that all the phases correctly clear their
environment, it merely detects the unlikely case in which all the phases
make the same mistake.
This commit is contained in:
Samuel Gélineau
2023-12-01 23:39:21 -05:00
parent b8d391024a
commit 72235b0fd4

View File

@@ -0,0 +1,9 @@
-- main.mcl --
$wat = $x
$f = func($x) {
$wat
}
test $f("foo") {}
-- OUTPUT --
# err: errSetScope: scope-checking the function definition `$f`: failed to set scope on function body: variable x not in scope