Get Target out of SupportedEnvironmentsProvider. Only a Label and Location are needed, so just use that.

PiperOrigin-RevId: 185738140
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/LabelAndLocation.java b/src/main/java/com/google/devtools/build/lib/analysis/LabelAndLocation.java
new file mode 100644
index 0000000..665feff
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/analysis/LabelAndLocation.java
@@ -0,0 +1,44 @@
+// Copyright 2018 The Bazel Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.devtools.build.lib.analysis;
+
+import com.google.auto.value.AutoValue;
+import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.events.Location;
+import com.google.devtools.build.lib.packages.Target;
+import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
+
+/**
+ * Container for some attributes of a {@link Target} that is significantly less heavyweight than an
+ * actual {@link Target} for purposes of serialization. Should still not be used indiscriminately,
+ * since {@link Location} can be quite heavy on its own and each of these wrapper objects costs 24
+ * bytes over an existing {@link Target}.
+ */
+@AutoCodec
+@AutoValue
+public abstract class LabelAndLocation {
+  @AutoCodec.Instantiator
+  static LabelAndLocation create(Label label, Location location) {
+    return new AutoValue_LabelAndLocation(label, location);
+  }
+
+  public static LabelAndLocation of(Target target) {
+    return create(target.getLabel(), target.getLocation());
+  }
+
+  public abstract Label getLabel();
+
+  public abstract Location getLocation();
+}
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetBuilder.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetBuilder.java
index 93b2df3..954f512 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetBuilder.java
@@ -40,7 +40,6 @@
 import com.google.devtools.build.lib.packages.Info;
 import com.google.devtools.build.lib.packages.NativeProvider;
 import com.google.devtools.build.lib.packages.Provider;
-import com.google.devtools.build.lib.packages.Target;
 import com.google.devtools.build.lib.packages.TargetUtils;
 import com.google.devtools.build.lib.syntax.EvalException;
 import com.google.devtools.build.lib.syntax.Type;
@@ -170,7 +169,7 @@
         ConstraintSemantics.getSupportedEnvironments(ruleContext);
     if (supportedEnvironments != null) {
       EnvironmentCollection.Builder refinedEnvironments = new EnvironmentCollection.Builder();
-      Map<Label, Target> removedEnvironmentCulprits = new LinkedHashMap<>();
+      Map<Label, LabelAndLocation> removedEnvironmentCulprits = new LinkedHashMap<>();
       ConstraintSemantics.checkConstraints(ruleContext, supportedEnvironments, refinedEnvironments,
           removedEnvironmentCulprits);
       add(SupportedEnvironmentsProvider.class,
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
index aaebac4..28af97d 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/ConstraintSemantics.java
@@ -21,6 +21,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
+import com.google.devtools.build.lib.analysis.LabelAndLocation;
 import com.google.devtools.build.lib.analysis.RuleContext;
 import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
 import com.google.devtools.build.lib.analysis.configuredtargets.OutputFileConfiguredTarget;
@@ -499,25 +500,26 @@
    *
    * <ul>
    *   <li>Static environment checking: if this rule supports environment E, all deps outside
-   *     selects must also support E
+   *       selects must also support E
    *   <li>Refined environment computation: this rule's refined environments are its static
-   *     environments intersected with the refined environments of all dependencies (including
-   *     chosen deps in selects)
+   *       environments intersected with the refined environments of all dependencies (including
+   *       chosen deps in selects)
    *   <li>Refined environment checking: no environment groups can be "emptied" due to refinement
    * </ul>
    *
    * @param ruleContext the rule to analyze
-   * @param staticEnvironments the rule's supported environments, as defined by the return
-   *     value of {@link #getSupportedEnvironments}. In particular, for any environment group that's
-   *     not in this collection, the rule is assumed to support the defaults for that group.
+   * @param staticEnvironments the rule's supported environments, as defined by the return value of
+   *     {@link #getSupportedEnvironments}. In particular, for any environment group that's not in
+   *     this collection, the rule is assumed to support the defaults for that group.
    * @param refinedEnvironments a builder for populating this rule's refined environments
    * @param removedEnvironmentCulprits a builder for populating the core dependencies that trigger
    *     pruning away environments through refinement. If multiple dependencies qualify (e.g.
-   *     two direct deps under the current rule), one is arbitrarily chosen.
    */
-  public static void checkConstraints(RuleContext ruleContext,
-      EnvironmentCollection staticEnvironments, EnvironmentCollection.Builder refinedEnvironments,
-      Map<Label, Target> removedEnvironmentCulprits) {
+  public static void checkConstraints(
+      RuleContext ruleContext,
+      EnvironmentCollection staticEnvironments,
+      EnvironmentCollection.Builder refinedEnvironments,
+      Map<Label, LabelAndLocation> removedEnvironmentCulprits) {
     Set<EnvironmentWithGroup> refinedEnvironmentsSoFar = new LinkedHashSet<>();
     // Start with the full set of static environments:
     refinedEnvironmentsSoFar.addAll(staticEnvironments.getGroupedEnvironments());
@@ -582,7 +584,7 @@
       Map<Label, EnvironmentWithGroup> labelsToEnvironments,
       Set<EnvironmentWithGroup> refinedEnvironmentsSoFar,
       Set<EnvironmentGroup> groupsWithEnvironmentsRemoved,
-      Map<Label, Target> removedEnvironmentCulprits) {
+      Map<Label, LabelAndLocation> removedEnvironmentCulprits) {
 
     SupportedEnvironmentsProvider depEnvironments =
         dep.getProvider(SupportedEnvironmentsProvider.class);
@@ -632,8 +634,8 @@
    * Helper method for {@link #checkConstraints}: performs refined environment constraint checking.
    *
    * <p>Refined environment expectations: no environment group should be emptied out due to
-   * refining. This reflects the idea that some of the static declared environments get pruned
-   * out by the build configuration, but <i>all</i> environments shouldn't be pruned out.
+   * refining. This reflects the idea that some of the static declared environments get pruned out
+   * by the build configuration, but <i>all</i> environments shouldn't be pruned out.
    *
    * <p>Violations of this expectation trigger rule analysis errors.
    */
@@ -642,7 +644,7 @@
       Set<EnvironmentGroup> groupsWithEnvironmentsRemoved,
       Set<EnvironmentWithGroup> refinedEnvironmentsSoFar,
       EnvironmentCollection.Builder refinedEnvironments,
-      Map<Label, Target> removedEnvironmentCulprits) {
+      Map<Label, LabelAndLocation> removedEnvironmentCulprits) {
     Set<EnvironmentGroup> refinedGroups = new LinkedHashSet<>();
     for (EnvironmentWithGroup envWithGroup : refinedEnvironmentsSoFar) {
       refinedEnvironments.put(envWithGroup.group(), envWithGroup.environment());
@@ -657,11 +659,12 @@
   }
 
   /**
-   * Constructs an error message for when all environments have been pruned out of one
-   * or more environment groups due to refining.
+   * Constructs an error message for when all environments have been pruned out of one or more
+   * environment groups due to refining.
    */
-  private static String getOverRefinementError(Set<EnvironmentGroup> newlyEmptyGroups,
-      Map<Label, Target> removedEnvironmentCulprits) {
+  private static String getOverRefinementError(
+      Set<EnvironmentGroup> newlyEmptyGroups,
+      Map<Label, LabelAndLocation> removedEnvironmentCulprits) {
     StringBuilder message = new StringBuilder("the current command-line flags disqualify "
         + "all supported environments because of incompatible select() paths:");
     for (EnvironmentGroup group : newlyEmptyGroups) {
@@ -669,7 +672,7 @@
         message.append("\n\nenvironment group: " + group.getLabel() + ":");
       }
       for (Label prunedEnvironment : group.getEnvironments()) {
-        Target culprit = removedEnvironmentCulprits.get(prunedEnvironment);
+        LabelAndLocation culprit = removedEnvironmentCulprits.get(prunedEnvironment);
         if (culprit != null) { // Only environments this rule declared support for have culprits.
           message.append("\n environment: " + prunedEnvironment
               + " removed by: " + culprit.getLabel() + " (" + culprit.getLocation() + ")");
@@ -685,18 +688,19 @@
    *
    * <p>For example, say we have R -> D1 -> D2 and all rules support environment E. If the
    * refinement happens because D2 has
+   *
    * <pre>
    *   deps = select({":foo": ["restricted_to_E"], ":bar": ["restricted_to_F"]}}  # Choose F.
    * </pre>
    *
    * <p>then D2 is the original refiner (even though D1 and R inherit the same pruning).
    */
-  private static Target findOriginalRefiner(RuleContext ruleContext,
-      SupportedEnvironmentsProvider dep, EnvironmentWithGroup envToPrune) {
-    Target depCulprit = dep.getRemovedEnvironmentCulprit(envToPrune.environment());
+  private static LabelAndLocation findOriginalRefiner(
+      RuleContext ruleContext, SupportedEnvironmentsProvider dep, EnvironmentWithGroup envToPrune) {
+    LabelAndLocation depCulprit = dep.getRemovedEnvironmentCulprit(envToPrune.environment());
     // If the dep has no record of this environment being refined, that means the current rule
     // is the culprit.
-    return depCulprit == null ? ruleContext.getTarget() : depCulprit;
+    return depCulprit == null ? LabelAndLocation.of(ruleContext.getTarget()) : depCulprit;
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java
index 4648d17..0114088 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/Environment.java
@@ -24,8 +24,6 @@
 import com.google.devtools.build.lib.analysis.RunfilesProvider;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.packages.EnvironmentGroup;
-import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
-import com.google.devtools.build.lib.packages.Target;
 
 /**
  * Implementation for the environment rule.
@@ -50,7 +48,7 @@
     EnvironmentCollection env = new EnvironmentCollection.Builder().put(group, label).build();
     return new RuleConfiguredTargetBuilder(ruleContext)
         .addProvider(SupportedEnvironmentsProvider.class,
-            new SupportedEnvironments(env, env, ImmutableMap.<Label, Target>of()))
+            new SupportedEnvironments(env, env, ImmutableMap.of()))
         .addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
         .add(FileProvider.class, FileProvider.EMPTY)
         .add(FilesToRunProvider.class, FilesToRunProvider.EMPTY)
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironments.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironments.java
index f1fcae1..1b89bda 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironments.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironments.java
@@ -15,9 +15,8 @@
 package com.google.devtools.build.lib.analysis.constraints;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.analysis.LabelAndLocation;
 import com.google.devtools.build.lib.cmdline.Label;
-import com.google.devtools.build.lib.packages.Target;
-
 import java.util.Map;
 
 /**
@@ -26,10 +25,12 @@
 public class SupportedEnvironments implements SupportedEnvironmentsProvider {
   private final EnvironmentCollection staticEnvironments;
   private final EnvironmentCollection refinedEnvironments;
-  private final ImmutableMap<Label, Target> removedEnvironmentCulprits;
+  private final ImmutableMap<Label, LabelAndLocation> removedEnvironmentCulprits;
 
-  public SupportedEnvironments(EnvironmentCollection staticEnvironments,
-      EnvironmentCollection refinedEnvironments, Map<Label, Target> removedEnvironmentCulprits) {
+  public SupportedEnvironments(
+      EnvironmentCollection staticEnvironments,
+      EnvironmentCollection refinedEnvironments,
+      Map<Label, LabelAndLocation> removedEnvironmentCulprits) {
     this.staticEnvironments = staticEnvironments;
     this.refinedEnvironments = refinedEnvironments;
     this.removedEnvironmentCulprits = ImmutableMap.copyOf(removedEnvironmentCulprits);
@@ -46,7 +47,7 @@
   }
 
   @Override
-  public Target getRemovedEnvironmentCulprit(Label environment) {
+  public LabelAndLocation getRemovedEnvironmentCulprit(Label environment) {
     return removedEnvironmentCulprits.get(environment);
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironmentsProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironmentsProvider.java
index 81909fc..6ef6785 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironmentsProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/SupportedEnvironmentsProvider.java
@@ -14,9 +14,9 @@
 
 package com.google.devtools.build.lib.analysis.constraints;
 
+import com.google.devtools.build.lib.analysis.LabelAndLocation;
 import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
 import com.google.devtools.build.lib.cmdline.Label;
-import com.google.devtools.build.lib.packages.Target;
 
 /**
  * A provider that advertises which environments the associated target is compatible with
@@ -46,15 +46,14 @@
    * If the given environment was refined away from this target's set of supported environments,
    * returns the dependency that originally removed the environment.
    *
-   * <p>For example, if the current rule is restricted_to [E] and depends on D1, D1 is
-   * restricted_to [E] and depends on D2, and D2 is restricted_to [E, F] and has a select()
-   * with one path following an E-restricted dep and the other path following an F-restricted dep,
-   * then when the build chooses the F path the current rule has [E] refined to [] and D2 is the
-   * culprit.
+   * <p>For example, if the current rule is restricted_to [E] and depends on D1, D1 is restricted_to
+   * [E] and depends on D2, and D2 is restricted_to [E, F] and has a select() with one path
+   * following an E-restricted dep and the other path following an F-restricted dep, then when the
+   * build chooses the F path the current rule has [E] refined to [] and D2 is the culprit.
    *
    * <p>If the given environment was not refined away for this rule, returns null.
    *
    * <p>See {@link ConstraintSemantics} class documentation for more details on refinement.
    */
-  Target getRemovedEnvironmentCulprit(Label environment);
+  LabelAndLocation getRemovedEnvironmentCulprit(Label environment);
 }