Visit subtrees of the AST in evaluation order
For instance, it makes more sense to visit the RHS of an assignment
first because this is evaluated first.
This also fixes a bug in the validator, which allowed definitions
like "a = a"
RELNOTES: None
PiperOrigin-RevId: 166709589
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java
index 79afdb9..39f46d0 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SyntaxTreeVisitorTest.java
@@ -62,7 +62,7 @@
" return h + i.j()");
IdentVisitor visitor = new IdentVisitor();
ast.accept(visitor);
- assertThat(idents).containsExactly("a", "b", "c", "d", "e", "f", "g", "h", "i", "j").inOrder();
+ assertThat(idents).containsExactly("b", "a", "c", "e", "d", "f", "g", "h", "i", "j").inOrder();
assertThat(params).containsExactly("p1", "p2=4", "**p3").inOrder();
}
}
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java
index 604c53f..49f5dbb 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/ValidationTest.java
@@ -105,6 +105,14 @@
}
@Test
+ public void testDefinitionByItself() throws Exception {
+ checkError("name 'a' is not defined", "a = a");
+ checkError("name 'a' is not defined", "a += a");
+ checkError("name 'a' is not defined", "[[] for a in a]");
+ checkError("name 'a' is not defined", "def f():", " for a in a: pass");
+ }
+
+ @Test
public void testLocalValidationEnvironmentsAreSeparated() throws Exception {
parse("def func1():", " a = 1", "def func2():", " a = 'abc'\n");
}