Make sure lexicalFrame is never null

This simplifies the code a bit.

https://github.com/bazelbuild/bazel/issues/5637

RELNOTES: None.
PiperOrigin-RevId: 210410389
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
index 888642e..387f6ef 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
@@ -470,7 +470,7 @@
     @Nullable final Continuation continuation;
 
     /** The lexical Frame of the caller. */
-    final LexicalFrame lexicalFrame;
+    final Frame lexicalFrame;
 
     /** The global Frame of the caller. */
     final GlobalFrame globalFrame;
@@ -482,7 +482,7 @@
         @Nullable Continuation continuation,
         BaseFunction function,
         @Nullable FuncallExpression caller,
-        LexicalFrame lexicalFrame,
+        Frame lexicalFrame,
         GlobalFrame globalFrame,
         @Nullable LinkedHashSet<String> knownGlobalVariables) {
       this.continuation = continuation;
@@ -655,7 +655,7 @@
    * Static Frame for lexical variables that are always looked up in the current Environment
    * or for the definition Environment of the function currently being evaluated.
    */
-  private LexicalFrame lexicalFrame;
+  private Frame lexicalFrame;
 
   /**
    * Static Frame for global variables; either the current lexical Frame if evaluation is currently
@@ -728,7 +728,7 @@
    */
   void enterScope(
       BaseFunction function,
-      LexicalFrame lexical,
+      Frame lexical,
       @Nullable FuncallExpression caller,
       GlobalFrame globals) {
     continuation =
@@ -782,7 +782,7 @@
    * as opposed to inside the body of a function.
    */
   boolean isGlobal() {
-    return lexicalFrame == null;
+    return lexicalFrame instanceof GlobalFrame;
   }
 
   @Override
@@ -791,11 +791,6 @@
     return dynamicFrame.mutability();
   }
 
-  /** Returns the current Frame, in which variable side-effects happen. */
-  private Frame currentFrame() {
-    return isGlobal() ? globalFrame : lexicalFrame;
-  }
-
   /** Returns the global variables for the Environment (not including dynamic bindings). */
   public GlobalFrame getGlobals() {
     return globalFrame;
@@ -863,6 +858,7 @@
       @Nullable String fileContentHashCode,
       Phase phase,
       @Nullable Label callerLabel) {
+    this.lexicalFrame = Preconditions.checkNotNull(globalFrame);
     this.globalFrame = Preconditions.checkNotNull(globalFrame);
     this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame);
     Preconditions.checkArgument(!globalFrame.mutability().isFrozen());
@@ -990,21 +986,11 @@
    * @return this Environment, in fluid style
    */
   public Environment setupDynamic(String varname, Object value) {
-    if (dynamicFrame.get(varname) != null) {
+    if (lookup(varname) != null) {
       throw new AssertionError(
           String.format("Trying to bind dynamic variable '%s' but it is already bound",
               varname));
     }
-    if (lexicalFrame != null && lexicalFrame.get(varname) != null) {
-      throw new AssertionError(
-          String.format("Trying to bind dynamic variable '%s' but it is already bound lexically",
-              varname));
-    }
-    if (globalFrame.get(varname) != null) {
-      throw new AssertionError(
-          String.format("Trying to bind dynamic variable '%s' but it is already bound globally",
-              varname));
-    }
     try {
       dynamicFrame.put(this, varname, value);
     } catch (MutabilityException e) {
@@ -1021,7 +1007,7 @@
   /** Remove variable from local bindings. */
   void removeLocalBinding(String varname) {
     try {
-      currentFrame().remove(this, varname);
+      lexicalFrame.remove(this, varname);
     } catch (MutabilityException e) {
       throw new AssertionError(e);
     }
@@ -1048,7 +1034,7 @@
           null, String.format("Trying to update read-only global variable '%s'", varname));
     }
     try {
-      currentFrame().put(this, varname, Preconditions.checkNotNull(value));
+      lexicalFrame.put(this, varname, Preconditions.checkNotNull(value));
     } catch (MutabilityException e) {
       // Note that since at this time we don't accept the global keyword, and don't have closures,
       // end users should never be able to mutate a frozen Environment, and a MutabilityException
@@ -1096,11 +1082,9 @@
    */
   public Object lookup(String varname) {
     // Lexical frame takes precedence, then globals, then dynamics.
-    if (lexicalFrame != null) {
-      Object lexicalValue = lexicalFrame.get(varname);
-      if (lexicalValue != null) {
-        return lexicalValue;
-      }
+    Object lexicalValue = lexicalFrame.get(varname);
+    if (lexicalValue != null) {
+      return lexicalValue;
     }
     Object globalValue = globalFrame.get(varname);
     Object dynamicValue = dynamicFrame.get(varname);
@@ -1138,9 +1122,8 @@
    */
   public Set<String> getVariableNames() {
     LinkedHashSet<String> vars = new LinkedHashSet<>();
-    if (lexicalFrame != null) {
-      vars.addAll(lexicalFrame.getTransitiveBindings().keySet());
-    }
+    vars.addAll(lexicalFrame.getTransitiveBindings().keySet());
+    // No-op when globalFrame = lexicalFrame
     vars.addAll(globalFrame.getTransitiveBindings().keySet());
     vars.addAll(dynamicFrame.getTransitiveBindings().keySet());
     return vars;
@@ -1190,7 +1173,7 @@
     ImmutableList.Builder<DebugFrame> frameListBuilder = ImmutableList.builder();
 
     Continuation currentContinuation = continuation;
-    Frame currentFrame = currentFrame();
+    Frame currentFrame = lexicalFrame;
 
     // if there's a continuation then the current frame is a lexical frame
     while (currentContinuation != null) {