bazel syntax: Implement comprension scopes
Fix this program taken from Starlark spec:
```
x = 1
_ = [x for x in [2]] # new variable x is local to the comprehension
print(x) # 1
```
Before this commit it resulted in
```
Traceback (most recent call last):
File "", line 3
print(x)
File "", line 3, in print
x
global variable 'x' is referenced before assignment.
```
Fixes issue #9438
Closes #9440.
PiperOrigin-RevId: 272401703
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/StarlarkThread.java b/src/main/java/com/google/devtools/build/lib/syntax/StarlarkThread.java
index b9d3a1d..4049865 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/StarlarkThread.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/StarlarkThread.java
@@ -1076,6 +1076,19 @@
return this;
}
+ // Used only for Eval.evalComprehension..
+ void updateInternal(String name, @Nullable Object value) {
+ try {
+ if (value != null) {
+ lexicalFrame.put(this, name, value);
+ } else {
+ lexicalFrame.remove(this, name);
+ }
+ } catch (MutabilityException ex) {
+ throw new IllegalStateException(ex);
+ }
+ }
+
/**
* Initializes a binding in this StarlarkThread. It is an error if the variable is already bound.
* This is not for end-users, and will throw an AssertionError in case of conflict.