Migrate depset.union() and depset.to_list() to SkylarkCallable

RELNOTES: None.
PiperOrigin-RevId: 191641410
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/BazelLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/BazelLibrary.java
index 3a3a507..4e5845f 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/BazelLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/BazelLibrary.java
@@ -19,7 +19,6 @@
 import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.skylarkinterface.Param;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature;
-import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
 import java.util.List;
 
 /**
@@ -171,64 +170,6 @@
     return o instanceof SkylarkList && ((SkylarkList) o).isEmpty();
   }
 
-  @SkylarkSignature(
-    name = "union",
-    objectType = SkylarkNestedSet.class,
-    returnType = SkylarkNestedSet.class,
-    doc =
-        "<i>(Deprecated)</i> Returns a new <a href=\"depset.html\">depset</a> that is the merge "
-            + "of the given depset and <code>new_elements</code>. Use the <code>transitive</code> "
-            + "constructor argument instead.",
-    parameters = {
-      @Param(name = "input", type = SkylarkNestedSet.class, doc = "The input depset."),
-      @Param(name = "new_elements", type = Object.class, doc = "The elements to be added.")
-    },
-    useLocation = true,
-    useEnvironment = true
-  )
-  private static final BuiltinFunction union =
-      new BuiltinFunction("union") {
-        @SuppressWarnings("unused")
-        public SkylarkNestedSet invoke(
-            SkylarkNestedSet input, Object newElements, Location loc, Environment env)
-            throws EvalException {
-          if (env.getSemantics().incompatibleDepsetUnion()) {
-            throw new EvalException(
-                location,
-                "depset method `.union` has been removed. See "
-                    + "https://docs.bazel.build/versions/master/skylark/depsets.html for "
-                    + "recommendations. Use --incompatible_depset_union=false "
-                    + "to temporarily disable this check.");
-          }
-          // newElements' type is Object because of the polymorphism on unioning two
-          // SkylarkNestedSets versus a set and another kind of iterable.
-          // Can't use EvalUtils#toIterable since that would discard this information.
-          return SkylarkNestedSet.of(input, newElements, loc);
-        }
-      };
-
-  @SkylarkSignature(
-    name = "to_list",
-    objectType = SkylarkNestedSet.class,
-    returnType = MutableList.class,
-    doc =
-        "Returns a list of the elements, without duplicates, in the depset's traversal order. "
-            + "Note that order is unspecified (but deterministic) for elements that were added "
-            + "more than once to the depset. Order is also unspecified for <code>\"default\""
-            + "</code>-ordered depsets, and for elements of child depsets whose order differs "
-            + "from that of the parent depset. The list is a copy; modifying it has no effect "
-            + "on the depset and vice versa.",
-    parameters = {@Param(name = "input", type = SkylarkNestedSet.class, doc = "The input depset.")},
-    useEnvironment = true
-  )
-  private static final BuiltinFunction toList =
-      new BuiltinFunction("to_list") {
-        @SuppressWarnings("unused")
-        public MutableList<Object> invoke(SkylarkNestedSet input, Environment env) {
-          return MutableList.copyOf(env, input.toCollection());
-        }
-      };
-
   /**
    * Returns a function-value implementing "select" (i.e. configurable attributes) in the specified
    * package context.
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkNestedSet.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkNestedSet.java
index 1ab3d13..d5c60f8 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkNestedSet.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkNestedSet.java
@@ -21,10 +21,13 @@
 import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
 import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
+import com.google.devtools.build.lib.skylarkinterface.Param;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkPrinter;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
+import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
 import java.util.Collection;
 import javax.annotation.Nullable;
 
@@ -310,6 +313,49 @@
     return (set.toList().contains(key));
   }
 
+  @SkylarkCallable(
+      name = "union",
+      doc =
+          "<i>(Deprecated)</i> Returns a new <a href=\"depset.html\">depset</a> that is the merge "
+              + "of the given depset and <code>new_elements</code>. Use the "
+              + "<code>transitive</code> constructor argument instead.",
+      parameters = {
+          @Param(name = "new_elements", type = Object.class, doc = "The elements to be added.")
+      },
+      useLocation = true,
+      useEnvironment = true
+  )
+  public SkylarkNestedSet union(Object newElements, Location loc, Environment env)
+      throws EvalException {
+    if (env.getSemantics().incompatibleDepsetUnion()) {
+      throw new EvalException(
+          loc,
+          "depset method `.union` has been removed. See "
+              + "https://docs.bazel.build/versions/master/skylark/depsets.html for "
+              + "recommendations. Use --incompatible_depset_union=false "
+              + "to temporarily disable this check.");
+    }
+    // newElements' type is Object because of the polymorphism on unioning two
+    // SkylarkNestedSets versus a set and another kind of iterable.
+    // Can't use EvalUtils#toIterable since that would discard this information.
+    return SkylarkNestedSet.of(this, newElements, loc);
+  }
+
+  @SkylarkCallable(
+      name = "to_list",
+      doc =
+          "Returns a list of the elements, without duplicates, in the depset's traversal order. "
+              + "Note that order is unspecified (but deterministic) for elements that were added "
+              + "more than once to the depset. Order is also unspecified for <code>\"default\""
+              + "</code>-ordered depsets, and for elements of child depsets whose order differs "
+              + "from that of the parent depset. The list is a copy; modifying it has no effect "
+              + "on the depset and vice versa.",
+      useEnvironment = true
+  )
+  public MutableList<Object> toList(Environment env) {
+    return MutableList.copyOf(env, this.toCollection());
+  }
+
   /**
    * Create a {@link Builder} with specified order.
    *
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java
index 0836d0e..17a79d2 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkNestedSetTest.java
@@ -351,8 +351,9 @@
 
   @Test
   public void testUnionWrongNumArgs() throws Exception {
-    new BothModesTest()
-        .testIfErrorContains("insufficient arguments received by union", "depset(['a']).union()");
+    new BothModesTest().testIfErrorContains(
+        "parameter 'new_elements' has no default value, in method call union() of 'depset'",
+        "depset(['a']).union()");
   }
 
   @Test