aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar laurentlb <laurentlb@google.com>2017-06-13 23:08:06 +0200
committerGravatar Yun Peng <pcloudy@google.com>2017-06-14 13:17:16 +0200
commit9d5c0a0eb26ab17d152f0cb8281cbc94e9377776 (patch)
tree3225658de8732609fb4aa4f6ed3b2e8344987a55 /src/test
parente0e0071b6b34594f3d283dfff446d572c24539dd (diff)
Introduce --incompatible_comprehension_variables_do_not_leak
When the flag is activated, variables in list comprehensions do not leak anymore. Even if a variable was defined before the loop, it's not accessible after the loop. This change allows us to detect any breakage and ensures that no user is accessing loop variables after the loop. This will make possible for us to change the behavior and follow Python 3 semantics in the future. RELNOTES: None. PiperOrigin-RevId: 158895514
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java30
1 files changed, 30 insertions, 0 deletions
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 d00c1bd50a..81fa580ecb 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
@@ -1490,4 +1490,34 @@ public class SkylarkEvaluationTest extends EvaluationTest {
// TODO(bazel-team): This should probably match the error above better.
"struct has no method 'nonexistent_method'", "v = val.nonexistent_method()");
}
+
+ @Test
+ public void testListComprehensionsDoNotLeakVariables() throws Exception {
+ env =
+ newEnvironmentWithSkylarkOptions("--incompatible_comprehension_variables_do_not_leak=true");
+ checkEvalErrorContains(
+ "name 'a' is not defined",
+ "def foo():",
+ " a = 10",
+ " b = [a for a in range(3)]",
+ " return a",
+ "x = foo()");
+ }
+
+ @Test
+ public void testListComprehensionsShadowGlobalVariable() throws Exception {
+ env =
+ newEnvironmentWithSkylarkOptions("--incompatible_comprehension_variables_do_not_leak=true");
+ eval("a = 18", "def foo():", " b = [a for a in range(3)]", " return a", "x = foo()");
+ assertThat(lookup("x")).isEqualTo(18);
+ }
+
+ @Test
+ public void testListComprehensionsLeakVariables() throws Exception {
+ env =
+ newEnvironmentWithSkylarkOptions(
+ "--incompatible_comprehension_variables_do_not_leak=false");
+ eval("def foo():", " a = 10", " b = [a for a in range(3)]", " return a", "x = foo()");
+ assertThat(lookup("x")).isEqualTo(2);
+ }
}