Add static name resolution
This implements a part of the name resolution document: https://github.com/bazelbuild/proposals/blob/master/docs/2018-06-18-name-resolution.md
and it is enabled with the flag --incompatible_static_name_resolution
There are two visible changes:
1. Local variables can be used before their definition point.
2. Local variables will shadow global variables, even if they are not initialiazed yet.
https://github.com/bazelbuild/bazel/issues/5637
RELNOTES: None.
PiperOrigin-RevId: 210562752
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
index 6991363..64ebe3e 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
@@ -1796,6 +1796,32 @@
}
@Test
+ public void testLocalVariableDefinedBelow() throws Exception {
+ new SkylarkTest("--incompatible_static_name_resolution=true")
+ .setUp(
+ "def beforeEven(li):", // returns the value before the first even number
+ " for i in li:",
+ " if i % 2 == 0:",
+ " return a",
+ " else:",
+ " a = i",
+ "res = beforeEven([1, 3, 4, 5])")
+ .testLookup("res", 3);
+ }
+
+ @Test
+ public void testShadowisNotInitialized() throws Exception {
+ new SkylarkTest("--incompatible_static_name_resolution=true")
+ .testIfErrorContains(
+ /* error message */ "name 'gl' is not defined",
+ "gl = 5",
+ "def foo():", // returns the value before the first even number
+ " if False: gl = 2",
+ " return gl",
+ "res = foo()");
+ }
+
+ @Test
public void testFunctionCallRecursion() throws Exception {
new SkylarkTest().testIfErrorContains("Recursion was detected when calling 'f' from 'g'",
"def main():",