Skip to content

Commit

Permalink
Remove Depset.legacyOf
Browse files Browse the repository at this point in the history
Uses in the tests are replaced with Depset.of(Class, Nestedset).

PiperOrigin-RevId: 513907058
Change-Id: If1097202d4cc8f13cb3f370ae49c6d85ee947601
  • Loading branch information
comius authored and copybara-github committed Mar 6, 2023
1 parent 4e739d3 commit 05253dc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,40 +102,6 @@ private Depset(@Nullable Class<?> elemClass, NestedSet<?> set) {
this.set = set;
}

// Implementation of deprecated depset(items) constructor, where items is
// supplied positionally. See https://github.com/bazelbuild/bazel/issues/9017.
static Depset legacyOf(Order order, Object items) throws EvalException {
Class<?> elemClass = null; // special value for empty depset
NestedSetBuilder<Object> builder = new NestedSetBuilder<>(order);

if (items instanceof Depset) {
Depset nestedSet = (Depset) items;
if (!nestedSet.isEmpty()) {
elemClass = nestedSet.elemClass;
try {
builder.addTransitive(nestedSet.set);
} catch (IllegalArgumentException e) {
// Order mismatch between items and builder.
throw Starlark.errorf("%s", e.getMessage());
}
}

} else if (items instanceof Sequence) {
for (Object x : (Sequence) items) {
checkElement(x, /* strict= */ true);
Class<?> xt = ElementType.getTypeClass(x.getClass());
elemClass = checkType(elemClass, xt);
builder.add(x);
}

} else {
throw Starlark.errorf(
"depset: got value of type '%s', want depset or sequence", Starlark.type(items));
}

return new Depset(elemClass, builder.build());
}

private static void checkElement(Object x, boolean strict) throws EvalException {
// Historically the requirement for a depset element was isImmutable(x).
// However, this check is neither necessary not sufficient.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ public void testGetSet() throws Exception {
// getSet argument must be a legal Starlark value class, or Object,
// but not some superclass that doesn't implement StarlarkValue.
Depset ints =
Depset.legacyOf(
Order.STABLE_ORDER, Tuple.of(StarlarkInt.of(1), StarlarkInt.of(2), StarlarkInt.of(3)));
Depset.of(
StarlarkInt.class,
NestedSetBuilder.create(
Order.STABLE_ORDER, StarlarkInt.of(1), StarlarkInt.of(2), StarlarkInt.of(3)));
assertThat(ints.getSet(StarlarkInt.class).toString()).isEqualTo("[1, 2, 3]");
IllegalArgumentException ex =
assertThrows(IllegalArgumentException.class, () -> ints.getSet(Number.class));
Expand Down Expand Up @@ -278,10 +280,10 @@ public void testOrderCompatibility() throws Exception {
// (b) at least one order is "default"

for (Order first : Order.values()) {
Depset s1 = Depset.legacyOf(first, Tuple.of("1", "11"));
Depset s1 = Depset.of(String.class, NestedSetBuilder.create(first, "1", "11"));

for (Order second : Order.values()) {
Depset s2 = Depset.legacyOf(second, Tuple.of("2", "22"));
Depset s2 = Depset.of(String.class, NestedSetBuilder.create(second, "2", "22"));

boolean compatible = true;

Expand Down

0 comments on commit 05253dc

Please sign in to comment.