Remove callerLabel from Environment.

It is a Bazel-specific information.

--
MOS_MIGRATED_REVID=140719791
diff --git a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java
index 9aa6b26..80cbbb9 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleClassFunctions.java
@@ -755,7 +755,7 @@
           throws EvalException {
         Label parentLabel = null;
         if (relativeToCallerRepository) {
-          parentLabel = env.getCallerLabel();
+          parentLabel = SkylarkUtils.getCallerLabel(env);
         } else {
           parentLabel = env.getGlobals().label();
         }
diff --git a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java
index f78e96d..b3c03aa 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/SkylarkRuleConfiguredTargetBuilder.java
@@ -47,6 +47,7 @@
 import com.google.devtools.build.lib.syntax.SkylarkList;
 import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
 import com.google.devtools.build.lib.syntax.SkylarkType;
+import com.google.devtools.build.lib.syntax.SkylarkUtils;
 import com.google.devtools.build.lib.syntax.Type;
 import com.google.devtools.build.lib.util.FileType;
 import com.google.devtools.build.lib.util.FileTypeSet;
@@ -75,12 +76,13 @@
       SkylarkRuleContext skylarkRuleContext = new SkylarkRuleContext(ruleContext,
           null);
       Environment env = Environment.builder(mutability)
-          .setCallerLabel(ruleContext.getLabel())
           .setGlobals(
               ruleContext.getRule().getRuleClassObject().getRuleDefinitionEnvironment().getGlobals())
           .setEventHandler(ruleContext.getAnalysisEnvironment().getEventHandler())
           .build(); // NB: loading phase functions are not available: this is analysis already,
                     // so we do *not* setLoadingPhase().
+      SkylarkUtils.setCallerLabel(env, ruleContext.getLabel());
+
       Object target = ruleImplementation.call(
           ImmutableList.<Object>of(skylarkRuleContext),
           ImmutableMap.<String, Object>of(),
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 11515ef..7029a59 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
@@ -336,13 +336,6 @@
   @Nullable private Continuation continuation;
 
   /**
-   * Gets the label of the BUILD file that is using this environment. For example, if a target
-   * //foo has a dependency on //bar which is a Skylark rule defined in //rules:my_rule.bzl being
-   * evaluated in this environment, then this would return //foo.
-   */
-  @Nullable private final Label callerLabel;
-
-  /**
    * Enters a scope by saving state to a new Continuation
    * @param function the function whose scope to enter
    * @param caller the source AST node for the caller
@@ -463,7 +456,6 @@
    * @param importedExtensions Extension-s from which to import bindings with load()
    * @param fileContentHashCode a hash for the source file being evaluated, if any
    * @param phase the current phase
-   * @param callerLabel the label this environment came from
    */
   private Environment(
       Frame globalFrame,
@@ -471,8 +463,7 @@
       EventHandler eventHandler,
       Map<String, Extension> importedExtensions,
       @Nullable String fileContentHashCode,
-      Phase phase,
-      @Nullable Label callerLabel) {
+      Phase phase) {
     this.globalFrame = Preconditions.checkNotNull(globalFrame);
     this.dynamicFrame = Preconditions.checkNotNull(dynamicFrame);
     Preconditions.checkArgument(globalFrame.mutability().isMutable());
@@ -480,7 +471,6 @@
     this.eventHandler = eventHandler;
     this.importedExtensions = importedExtensions;
     this.phase = phase;
-    this.callerLabel = callerLabel;
     this.transitiveHashCode =
         computeTransitiveContentHashCode(fileContentHashCode, importedExtensions);
   }
@@ -495,7 +485,6 @@
     @Nullable private EventHandler eventHandler;
     @Nullable private Map<String, Extension> importedExtensions;
     @Nullable private String fileContentHashCode;
-    private Label label;
 
     Builder(Mutability mutability) {
       this.mutability = mutability;
@@ -560,13 +549,7 @@
           eventHandler,
           importedExtensions,
           fileContentHashCode,
-          phase,
-          label);
-    }
-
-    public Builder setCallerLabel(Label label) {
-      this.label = label;
-      return this;
+          phase);
     }
   }
 
@@ -575,13 +558,6 @@
   }
 
   /**
-   * Returns the caller's label.
-   */
-  public Label getCallerLabel() {
-    return callerLabel;
-  }
-
-  /**
    * Sets a binding for a special dynamic variable in this Environment.
    * This is not for end-users, and will throw an AssertionError in case of conflict.
    * @param varname the name of the dynamic variable to be bound
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java
index 970543f..3a17fe5 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkUtils.java
@@ -14,6 +14,8 @@
 
 package com.google.devtools.build.lib.syntax;
 
+import com.google.devtools.build.lib.cmdline.Label;
+
 /** This class contains Bazel-specific functions to extend or interoperate with Skylark. */
 public final class SkylarkUtils {
 
@@ -23,6 +25,7 @@
   }
 
   private static final String BAZEL_INFO_KEY = "$bazel";
+  private static final String CALLER_LABEL_KEY = "$caller_label";
 
   private static BazelInfo getInfo(Environment env) {
     Object info = env.lookup(BAZEL_INFO_KEY);
@@ -46,4 +49,23 @@
   public static String getToolsRepository(Environment env) {
     return getInfo(env).toolsRepository;
   }
+
+  public static void setCallerLabel(Environment env, Label label) {
+    // We cannot store this information in BazelInfo, because we need to
+    // have it in the local environment (not the global environment).
+    try {
+      env.update(CALLER_LABEL_KEY, label);
+    } catch (EvalException e) {
+      throw new AssertionError(e);
+    }
+  }
+
+  /**
+   * Gets the label of the BUILD file that is using this environment. For example, if a target
+   * //foo has a dependency on //bar which is a Skylark rule defined in //rules:my_rule.bzl being
+   * evaluated in this environment, then this would return //foo.
+   */
+  public static Label getCallerLabel(Environment env) {
+    return (Label) env.lookup(CALLER_LABEL_KEY);
+  }
 }