aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar brandjon <brandjon@google.com>2017-08-01 18:54:28 +0200
committerGravatar Dmitry Lomov <dslomov@google.com>2017-08-02 10:39:58 +0200
commita2d101a755a99d4e44a259e98179993a81300912 (patch)
treec635552cb8a460a977845f62f344cc34834c845f
parent262946b8897d4ed85e2cb3e164893f43e60703b5 (diff)
Make list EMPTY instance work like tuples
I.e., use an accessor for type inference. The EMPTY field will be made private in a future CL. RELNOTES: None PiperOrigin-RevId: 163843569
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java42
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java2
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java2
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java12
4 files changed, 38 insertions, 20 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
index 3b70903449..eb642d2200 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
@@ -29,7 +29,7 @@ import java.util.ListIterator;
import java.util.RandomAccess;
import javax.annotation.Nullable;
-/** A class to handle lists and tuples in Skylark. */
+/** A Skylark list or tuple. */
@SkylarkModule(
name = "sequence",
documented = false,
@@ -333,6 +333,26 @@ public abstract class SkylarkList<E> extends MutableCollection<E>
}
/**
+ * A shared instance for the empty list with immutable mutability.
+ *
+ * <p>Other immutable empty list objects can exist, e.g. lists that were once mutable but whose
+ * environments were then frozen. This instance is for empty lists that were always frozen from
+ * the beginning.
+ *
+ * @deprecated Prefer {@link #empty()} instead, since that includes a cast for the element type.
+ * This field will be made private in the near future.
+ */
+ @Deprecated
+ public static final MutableList<?> EMPTY =
+ new MutableList<>(ImmutableList.of(), Mutability.IMMUTABLE);
+
+ /** Returns an empty frozen list, cast to have an arbitrary content type. */
+ @SuppressWarnings("unchecked")
+ public static <E> MutableList<E> empty() {
+ return (MutableList<E>) EMPTY;
+ }
+
+ /**
* Builds a Skylark list from a variable number of arguments.
* @param env an Environment from which to inherit Mutability, or null for immutable
* @param contents the contents of the list
@@ -493,11 +513,6 @@ public abstract class SkylarkList<E> extends MutableCollection<E>
public boolean isTuple() {
return false;
}
-
- /**
- * An empty IMMUTABLE MutableList.
- */
- public static final MutableList<?> EMPTY = new MutableList<>(Tuple.EMPTY, Mutability.IMMUTABLE);
}
/** An immutable tuple, e.g. in (1, 2, 3) */
@@ -528,16 +543,14 @@ public abstract class SkylarkList<E> extends MutableCollection<E>
this.contents = contents;
}
- @Override
- public Mutability mutability() {
- return Mutability.IMMUTABLE;
- }
-
/**
- * THE empty Skylark tuple.
+ * A shared instance for the empty tuple.
+ *
+ * <p>This instance should be the only empty tuple.
*/
private static final Tuple<?> EMPTY = new Tuple<>(ImmutableList.of());
+ /** Returns the empty tuple, cast to have an arbitrary content type. */
@SuppressWarnings("unchecked")
public static <E> Tuple<E> empty() {
return (Tuple<E>) EMPTY;
@@ -553,6 +566,11 @@ public abstract class SkylarkList<E> extends MutableCollection<E>
return new Tuple<>(contents);
}
+ @Override
+ public Mutability mutability() {
+ return Mutability.IMMUTABLE;
+ }
+
/**
* Creates a Tuple from an Iterable.
*/
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java
index 38792865a2..71e084da10 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkIntegrationTest.java
@@ -932,7 +932,7 @@ public class SkylarkIntegrationTest extends BuildViewTestCase {
ConfiguredTarget target = getConfiguredTarget("//test/skylark:cr");
assertThat(target.get("o1")).isEqualTo(Runtime.NONE);
- assertThat(target.get("o2")).isEqualTo(MutableList.EMPTY);
+ assertThat(target.get("o2")).isEqualTo(MutableList.empty());
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
index e23df8bd6c..075809f497 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleImplementationFunctionsTest.java
@@ -1538,7 +1538,7 @@ public class SkylarkRuleImplementationFunctionsTest extends SkylarkTestCase {
public void testEmptyLabelListTypeAttrInCtx() throws Exception {
SkylarkRuleContext ctx = createRuleContext("//foo:baz");
Object result = evalRuleContextCode(ctx, "ruleContext.attr.srcs");
- assertThat(result).isEqualTo(MutableList.EMPTY);
+ assertThat(result).isEqualTo(MutableList.empty());
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
index c722ab25da..d5cf1d2953 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/EvaluationTest.java
@@ -460,13 +460,13 @@ public class EvaluationTest extends EvaluationTestCase {
.testStatement("[1, 2] * 3", MutableList.of(env, 1, 2, 1, 2, 1, 2))
.testStatement("[1, 2] * 4", MutableList.of(env, 1, 2, 1, 2, 1, 2, 1, 2))
.testStatement("[8] * 5", MutableList.of(env, 8, 8, 8, 8, 8))
- .testStatement("[ ] * 10", MutableList.EMPTY)
- .testStatement("[1, 2] * 0", MutableList.EMPTY)
- .testStatement("[1, 2] * -4", MutableList.EMPTY)
+ .testStatement("[ ] * 10", MutableList.empty())
+ .testStatement("[1, 2] * 0", MutableList.empty())
+ .testStatement("[1, 2] * -4", MutableList.empty())
.testStatement(" 2 * [1, 2]", MutableList.of(env, 1, 2, 1, 2))
- .testStatement("10 * []", MutableList.EMPTY)
- .testStatement(" 0 * [1, 2]", MutableList.EMPTY)
- .testStatement("-4 * [1, 2]", MutableList.EMPTY);
+ .testStatement("10 * []", MutableList.empty())
+ .testStatement(" 0 * [1, 2]", MutableList.empty())
+ .testStatement("-4 * [1, 2]", MutableList.empty());
}
@SuppressWarnings("unchecked")