bazel syntax: delete EvalUtils.toIterableStrict
RELNOTES: None.
PiperOrigin-RevId: 281058705
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java
index 317445f..c6f4a2c 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleContext.java
@@ -1047,7 +1047,7 @@
throws ConversionException, EvalException {
checkMutable("resolve_command");
Label ruleLabel = getLabel();
- Map<Label, Iterable<Artifact>> labelDict = checkLabelDict(labelDictUnchecked, loc, thread);
+ Map<Label, Iterable<Artifact>> labelDict = checkLabelDict(labelDictUnchecked, loc);
// The best way to fix this probably is to convert CommandHelper to Skylark.
CommandHelper helper =
CommandHelper.builder(getRuleContext())
@@ -1115,8 +1115,8 @@
* Returns a corresponding map where any sets are replaced by iterables.
*/
// TODO(bazel-team): find a better way to typecheck this argument.
- private static Map<Label, Iterable<Artifact>> checkLabelDict(
- Map<?, ?> labelDict, Location loc, StarlarkThread thread) throws EvalException {
+ private static Map<Label, Iterable<Artifact>> checkLabelDict(Map<?, ?> labelDict, Location loc)
+ throws EvalException {
Map<Label, Iterable<Artifact>> convertedMap = new HashMap<>();
for (Map.Entry<?, ?> entry : labelDict.entrySet()) {
Object key = entry.getKey();
@@ -1126,12 +1126,14 @@
ImmutableList.Builder<Artifact> files = ImmutableList.builder();
Object val = entry.getValue();
Iterable<?> valIter;
- try {
- valIter = EvalUtils.toIterableStrict(val, loc, thread);
- } catch (EvalException ex) {
- // EvalException is thrown only if the type is wrong.
+ if (val instanceof Iterable) {
+ valIter = (Iterable<?>) val;
+ } else {
throw new EvalException(
- loc, Printer.format("invalid value %r in 'label_dict': " + ex, val));
+ loc,
+ Printer.format(
+ "invalid value %r in 'label_dict': expected iterable, but got '%s'",
+ val, EvalUtils.getDataTypeName(val)));
}
for (Object file : valIter) {
if (!(file instanceof Artifact)) {
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Type.java b/src/main/java/com/google/devtools/build/lib/packages/Type.java
index 04af9bc..92fd12f 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Type.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Type.java
@@ -21,11 +21,13 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.cmdline.Label;
+import com.google.devtools.build.lib.collect.nestedset.NestedSet.NestedSetDepthException;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.EvalUtils;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.Sequence;
+import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import com.google.devtools.build.lib.util.LoggingUtil;
import com.google.devtools.build.lib.util.StringCanonicalizer;
import java.util.ArrayList;
@@ -580,11 +582,25 @@
public List<ElemT> convert(Object x, Object what, Object context)
throws ConversionException {
Iterable<?> iterable;
- try {
- iterable = EvalUtils.toIterableStrict(x, null, null);
- } catch (EvalException ex) {
+
+ if (x instanceof Iterable) {
+ iterable = (Iterable<?>) x;
+ } else if (x instanceof SkylarkNestedSet) {
+ try {
+ iterable = ((SkylarkNestedSet) x).toCollection();
+ } catch (NestedSetDepthException exception) {
+ throw new ConversionException(
+ "depset exceeded maximum depth "
+ + exception.getDepthLimit()
+ + ". This was only discovered when attempting to flatten the depset for"
+ + " iteration, as the size of depsets is unknown until flattening. See"
+ + " https://github.com/bazelbuild/bazel/issues/9180 for details and possible "
+ + "solutions.");
+ }
+ } else {
throw new ConversionException(this, x, what);
}
+
int index = 0;
List<ElemT> result = new ArrayList<>(Iterables.size(iterable));
ListConversionContext conversionContext = new ListConversionContext(what);
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
index a428f0b..b468fd5 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/EvalUtils.java
@@ -20,7 +20,6 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
-import com.google.devtools.build.lib.collect.nestedset.NestedSet.NestedSetDepthException;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.SkylarkInterfaceUtils;
@@ -329,29 +328,6 @@
}
}
- // TODO(laurentlb): Get rid of this function.
- private static Collection<?> nestedSetToCollection(
- SkylarkNestedSet set, Location loc, @Nullable StarlarkThread thread) throws EvalException {
- if (thread != null) {
- throw new EvalException(
- loc,
- "type 'depset' is not iterable. Use the `to_list()` method to get a list. Use "
- + "--incompatible_depset_is_not_iterable=false to temporarily disable this check.");
- }
- try {
- return set.toCollection();
- } catch (NestedSetDepthException exception) {
- throw new EvalException(
- loc,
- "depset exceeded maximum depth "
- + exception.getDepthLimit()
- + ". This was only discovered when attempting to flatten the depset for iteration, "
- + "as the size of depsets is unknown until flattening. "
- + "See https://github.com/bazelbuild/bazel/issues/9180 for details and possible "
- + "solutions.");
- }
- }
-
public static Iterable<?> toIterable(Object o, Location loc) throws EvalException {
if (o instanceof Iterable) {
return (Iterable<?>) o;
@@ -363,34 +339,6 @@
}
}
- /**
- * Given an {@link Iterable}, returns it as-is. Given a {@link SkylarkNestedSet}, returns its
- * contents as an iterable. Throws {@link EvalException} for any other value.
- *
- * <p>This is a kludge for the change that made {@code SkylarkNestedSet} not implement {@code
- * Iterable}. It is different from {@link #toIterable} in its behavior for strings and other types
- * that are not strictly Java-iterable.
- *
- * @throws EvalException if {@code o} is not an iterable or set
- * @deprecated avoid writing APIs that implicitly treat depsets as iterables. It encourages
- * unnecessary flattening of depsets.
- * <p>TODO(bazel-team): Remove this if/when implicit iteration over {@code SkylarkNestedSet}
- * is no longer supported.
- */
- @Deprecated
- public static Iterable<?> toIterableStrict(
- Object o, Location loc, @Nullable StarlarkThread thread) throws EvalException {
- if (o instanceof Iterable) {
- return (Iterable<?>) o;
- } else if (o instanceof SkylarkNestedSet) {
- return nestedSetToCollection((SkylarkNestedSet) o, loc, thread);
- } else {
- throw new EvalException(loc,
- "expected Iterable or depset, but got '" + getDataTypeName(o) + "' (strings and maps "
- + "are not allowed here)");
- }
- }
-
public static void lock(Object object, Location loc) {
if (object instanceof StarlarkMutable) {
StarlarkMutable x = (StarlarkMutable) object;