Skip to content

Commit

Permalink
Make list EMPTY instance work like tuples
Browse files Browse the repository at this point in the history
I.e., use an accessor for type inference. The EMPTY field will be made private in a future CL.

RELNOTES: None
PiperOrigin-RevId: 163843569
  • Loading branch information
brandjon authored and dslomov committed Aug 2, 2017
1 parent 262946b commit a2d101a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
42 changes: 30 additions & 12 deletions src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
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,
Expand Down Expand Up @@ -332,6 +332,26 @@ public MutableList(Mutability mutability) {
this(Collections.EMPTY_LIST, mutability);
}

/**
* 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
Expand Down Expand Up @@ -493,11 +513,6 @@ public Mutability mutability() {
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) */
Expand Down Expand Up @@ -528,16 +543,14 @@ private Tuple(ImmutableList<E> contents) {
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;
Expand All @@ -553,6 +566,11 @@ public static<E> Tuple<E> create(ImmutableList<E> contents) {
return new Tuple<>(contents);
}

@Override
public Mutability mutability() {
return Mutability.IMMUTABLE;
}

/**
* Creates a Tuple from an Iterable.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ public void testRuleClassNonMandatoryEmptyOutputs() throws Exception {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ public void testBinDirPath() throws Exception {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,13 @@ public void testListMultiply() throws Exception {
.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")
Expand Down

0 comments on commit a2d101a

Please sign in to comment.