Add `depset` as an alias to `set` in Skylark

Renamed all occurrences of `set` to `depset`, added a `set` object constructor for (temporary) backward compatibility. `type(depset())` still temporarily returns "set", that will be changed in the future.

RELNOTES: The `set` constructor is deprecated in favor of `depset`

--
PiperOrigin-RevId: 142851587
MOS_MIGRATED_REVID=142851587
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkAspectsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkAspectsTest.java
index c4d0f88..dbe7efa 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkAspectsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkAspectsTest.java
@@ -180,8 +180,8 @@
     scratch.file(
         "test/aspect.bzl",
         "def _impl(target, ctx):",
-        "   s = set([target.label])",
-        "   c = set([ctx.rule.kind])",
+        "   s = depset([target.label])",
+        "   c = depset([ctx.rule.kind])",
         "   for i in ctx.rule.attr.deps:",
         "       s += i.target_labels",
         "       c += i.rule_kinds",
@@ -233,8 +233,8 @@
     scratch.file(
         "test/aspect.bzl",
         "def _impl(target, ctx):",
-        "   s = set([target.label])",
-        "   c = set([ctx.rule.kind])",
+        "   s = depset([target.label])",
+        "   c = depset([ctx.rule.kind])",
         "   a = ctx.rule.attr",
         "   if hasattr(a, '_stl') and a._stl:",
         "       s += a._stl.target_labels",
@@ -368,13 +368,13 @@
     scratch.file(
         "test/aspect.bzl",
         "def _aspect_impl(target, ctx):",
-        "   s = set([target.label])",
+        "   s = depset([target.label])",
         "   for i in ctx.rule.attr.deps:",
         "       s += i.target_labels",
         "   return struct(target_labels = s)",
         "",
         "def _rule_impl(ctx):",
-        "   s = set([])",
+        "   s = depset([])",
         "   for i in ctx.attr.attr:",
         "       s += i.target_labels",
         "   return struct(rule_deps = s)",
@@ -629,18 +629,17 @@
         "def _impl(target, ctx):",
         "  f = ctx.new_file('f.txt')",
         "  ctx.file_action(f, 'f')",
-        "  return struct(output_groups = { 'duplicate' : set([f]) })",
+        "  return struct(output_groups = { 'duplicate' : depset([f]) })",
         "",
         "MyAspect = aspect(implementation=_impl)",
         "def _rule_impl(ctx):",
         "  g = ctx.new_file('g.txt')",
         "  ctx.file_action(g, 'g')",
-        "  return struct(output_groups = { 'duplicate' : set([g]) })",
+        "  return struct(output_groups = { 'duplicate' : depset([g]) })",
         "my_rule = rule(_rule_impl)",
         "def _noop(ctx):",
         "  pass",
-        "rbase = rule(_noop, attrs = { 'dep' : attr.label(aspects = [MyAspect]) })"
-    );
+        "rbase = rule(_noop, attrs = { 'dep' : attr.label(aspects = [MyAspect]) })");
     scratch.file(
         "test/BUILD",
         "load(':aspect.bzl', 'my_rule', 'rbase')",
@@ -666,7 +665,7 @@
         "def _a1_impl(target, ctx):",
         "  f = ctx.new_file(target.label.name + '_a1.txt')",
         "  ctx.file_action(f, 'f')",
-        "  return struct(output_groups = { 'a1_group' : set([f]) })",
+        "  return struct(output_groups = { 'a1_group' : depset([f]) })",
         "",
         "a1 = aspect(implementation=_a1_impl, attr_aspects = ['dep'])",
         "def _rule_impl(ctx):",
@@ -678,11 +677,10 @@
         "def _a2_impl(target, ctx):",
         "  g = ctx.new_file(target.label.name + '_a2.txt')",
         "  ctx.file_action(g, 'f')",
-        "  return struct(output_groups = { 'a2_group' : set([g]) })",
+        "  return struct(output_groups = { 'a2_group' : depset([g]) })",
         "",
         "a2 = aspect(implementation=_a2_impl, attr_aspects = ['dep'])",
-        "my_rule2 = rule(_rule_impl, attrs = { 'dep' : attr.label(aspects = [a2]) })"
-        );
+        "my_rule2 = rule(_rule_impl, attrs = { 'dep' : attr.label(aspects = [a2]) })");
     scratch.file(
         "test/BUILD",
         "load(':aspect.bzl', 'my_rule1', 'my_rule2')",
@@ -710,7 +708,7 @@
         "def _a1_impl(target, ctx):",
         "  f = ctx.new_file(target.label.name + '_a1.txt')",
         "  ctx.file_action(f, 'f')",
-        "  return struct(output_groups = { 'a1_group' : set([f]) })",
+        "  return struct(output_groups = { 'a1_group' : depset([f]) })",
         "",
         "a1 = aspect(implementation=_a1_impl, attr_aspects = ['dep'])",
         "def _rule_impl(ctx):",
@@ -722,11 +720,10 @@
         "def _a2_impl(target, ctx):",
         "  g = ctx.new_file(target.label.name + '_a2.txt')",
         "  ctx.file_action(g, 'f')",
-        "  return struct(output_groups = { 'a1_group' : set([g]) })",
+        "  return struct(output_groups = { 'a1_group' : depset([g]) })",
         "",
         "a2 = aspect(implementation=_a2_impl, attr_aspects = ['dep'])",
-        "my_rule2 = rule(_rule_impl, attrs = { 'dep' : attr.label(aspects = [a2]) })"
-    );
+        "my_rule2 = rule(_rule_impl, attrs = { 'dep' : attr.label(aspects = [a2]) })");
     scratch.file(
         "test/BUILD",
         "load(':aspect.bzl', 'my_rule1', 'my_rule2')",
@@ -1213,51 +1210,52 @@
 
   private String[] aspectBzlFile(String attrAspects) {
     return new String[] {
-        "def _repro_aspect_impl(target, ctx):",
-        "    s = set([str(target.label)])",
-        "    for d in ctx.rule.attr.deps:",
-        "       if hasattr(d, 'aspect_info'):",
-        "         s = s | d.aspect_info",
-        "    return struct(aspect_info = s)",
-        "",
-        "_repro_aspect = aspect(",
-        "    _repro_aspect_impl,",
-        "    attr_aspects = [" + attrAspects + "],",
-        ")",
-        "",
-        "def repro_impl(ctx):",
-        "    s = set()",
-        "    for d in ctx.attr.deps:",
-        "       if hasattr(d, 'aspect_info'):",
-        "         s = s | d.aspect_info",
-        "    return struct(rule_info = s)",
-        "",
-        "def repro_no_aspect_impl(ctx):",
-        "    pass",
-        "",
-        "repro_no_aspect = rule(implementation = repro_no_aspect_impl,",
-        "             attrs = {",
-        "                       'deps': attr.label_list(",
-        "                             allow_files = True,",
-        "                       )",
-        "                      },",
-        ")",
-        "",
-        "repro = rule(implementation = repro_impl,",
-        "             attrs = {",
-        "                       'deps': attr.label_list(",
-        "                             allow_files = True,",
-        "                             aspects = [_repro_aspect],",
-        "                       )",
-        "                      },",
-        ")"
+      "def _repro_aspect_impl(target, ctx):",
+      "    s = depset([str(target.label)])",
+      "    for d in ctx.rule.attr.deps:",
+      "       if hasattr(d, 'aspect_info'):",
+      "         s = s | d.aspect_info",
+      "    return struct(aspect_info = s)",
+      "",
+      "_repro_aspect = aspect(",
+      "    _repro_aspect_impl,",
+      "    attr_aspects = [" + attrAspects + "],",
+      ")",
+      "",
+      "def repro_impl(ctx):",
+      "    s = depset()",
+      "    for d in ctx.attr.deps:",
+      "       if hasattr(d, 'aspect_info'):",
+      "         s = s | d.aspect_info",
+      "    return struct(rule_info = s)",
+      "",
+      "def repro_no_aspect_impl(ctx):",
+      "    pass",
+      "",
+      "repro_no_aspect = rule(implementation = repro_no_aspect_impl,",
+      "             attrs = {",
+      "                       'deps': attr.label_list(",
+      "                             allow_files = True,",
+      "                       )",
+      "                      },",
+      ")",
+      "",
+      "repro = rule(implementation = repro_impl,",
+      "             attrs = {",
+      "                       'deps': attr.label_list(",
+      "                             allow_files = True,",
+      "                             aspects = [_repro_aspect],",
+      "                       )",
+      "                      },",
+      ")"
     };
   }
 
 
   @Test
   public void aspectOutputsToBinDirectory() throws Exception {
-    scratch.file("foo/extension.bzl",
+    scratch.file(
+        "foo/extension.bzl",
         "def _aspect_impl(target, ctx):",
         "   file = ctx.new_file('aspect-output-' + target.label.name)",
         "   ctx.file_action(file, 'data')",
@@ -1268,14 +1266,13 @@
         "rule_bin_out = rule(_rule_impl, output_to_genfiles=False)",
         "rule_gen_out = rule(_rule_impl, output_to_genfiles=True)",
         "def _main_rule_impl(ctx):",
-        "   s = set()",
+        "   s = depset()",
         "   for d in ctx.attr.deps:",
-        "       s = s | set([d.aspect_file])",
+        "       s = s | depset([d.aspect_file])",
         "   return struct(aspect_files = s)",
         "main_rule = rule(_main_rule_impl,",
         "   attrs = { 'deps' : attr.label_list(aspects = [my_aspect]) },",
-        ")"
-    );
+        ")");
 
     scratch.file("foo/BUILD",
         "load('extension', 'rule_bin_out', 'rule_gen_out', 'main_rule')",
@@ -1413,7 +1410,7 @@
     scratch.file(
         "test/aspect.bzl",
         "def _impl(target, ctx):",
-        "   s = set([target.label])",
+        "   s = depset([target.label])",
         "   if hasattr(ctx.rule.attr, 'runtime_deps'):",
         "     for i in ctx.rule.attr.runtime_deps:",
         "       s += i.target_labels",
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkCommandLineTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkCommandLineTest.java
index cd7a716..7d68bcd 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkCommandLineTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkCommandLineTest.java
@@ -18,7 +18,6 @@
 
 import com.google.devtools.build.lib.skylark.util.SkylarkTestCase;
 import com.google.devtools.build.lib.syntax.SkylarkList;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -46,7 +45,7 @@
     Object result =
         evalRuleContextCode(
             createRuleContext("//foo:foo"),
-            "cmd_helper.template(set(ruleContext.files.srcs), '--%{short_path}=%{path}')");
+            "cmd_helper.template(depset(ruleContext.files.srcs), '--%{short_path}=%{path}')");
     SkylarkList list = (SkylarkList) result;
     assertThat(list).containsExactly("--foo/a.txt=foo/a.txt", "--foo/b.img=foo/b.img").inOrder();
   }
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 9a5dff2..cb94367 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
@@ -236,7 +236,7 @@
         "test/skylark/extension.bzl",
         "def _impl(ctx):",
         "  f = ctx.attr.dep.output_groups['_hidden_top_level" + INTERNAL_SUFFIX + "']",
-        "  g = ctx.attr.dep.output_groups['_hidden_top_level" + INTERNAL_SUFFIX + "'] | set([])",
+        "  g = ctx.attr.dep.output_groups['_hidden_top_level" + INTERNAL_SUFFIX + "'] | depset([])",
         "  return struct(result = g, ",
         "                output_groups = { 'my_group' : g })",
         "my_rule = rule(implementation = _impl,",
@@ -331,7 +331,7 @@
         "load('/test/skylark/functions', 'first')",
         "def custom_rule_impl(ctx):",
         "  attr1 = ctx.files.attr1",
-        "  ftb = set(attr1)",
+        "  ftb = depset(attr1)",
         "  foo()",
         "  return struct(provider_key = ftb)",
         "def foo():",
@@ -365,7 +365,7 @@
         "test/skylark/extension.bzl",
         "def custom_rule_impl(ctx):",
         "  attr1 = ctx.files.attr1",
-        "  ftb = set(attr1)",
+        "  ftb = depset(attr1)",
         "  return struct(runfiles = ctx.runfiles(), files = ftb)",
         "",
         "custom_rule = rule(implementation = custom_rule_impl,",
@@ -602,7 +602,7 @@
         "test/skylark/extension.bzl",
         "def custom_rule_impl(ctx):",
         "  attr1 = ctx.files.attr1",
-        "  ftb = set(attr1)",
+        "  ftb = depset(attr1)",
         "  return struct(provider_key = ftb)",
         "",
         "custom_rule = rule(implementation = custom_rule_impl,",
@@ -688,7 +688,7 @@
         "  ctx.action(",
         "    outputs = files,",
         "    command = 'echo')",
-        "  ftb = set(files)",
+        "  ftb = depset(files)",
         "  return struct(runfiles = ctx.runfiles(), files = ftb)",
         "",
         "def output_func(attr1, attr2):",
@@ -725,7 +725,7 @@
         "  ctx.action(",
         "    outputs = files,",
         "    command = 'echo')",
-        "  return struct(files = set(files))",
+        "  return struct(files = depset(files))",
         "",
         "custom_rule = rule(implementation = custom_rule_impl,",
         "  attrs = {",
@@ -844,7 +844,7 @@
         "  ctx.action(",
         "    outputs = files,",
         "    command = 'echo')",
-        "  ftb = set(files)",
+        "  ftb = depset(files)",
         "  for i in ctx.outputs.out:",
         "    ctx.file_action(output=i, content='hi there')",
         "",
@@ -936,7 +936,7 @@
     scratch.file(
         "test/skylark/extension.bzl",
         "def _impl(ctx):",
-        "  return struct(bad=set([set([])]))",
+        "  return struct(bad=depset([depset([])]))",
         "my_rule = rule(implementation = _impl)");
     scratch.file(
         "test/skylark/BUILD",
@@ -944,7 +944,7 @@
         "my_rule(name = 'r')");
 
     getConfiguredTarget("//test/skylark:r");
-    assertContainsEvent("Value of provider 'bad' is of an illegal type: set\n");
+    assertContainsEvent("Value of provider 'bad' is of an illegal type: depset\n");
   }
 
   @Test
@@ -1006,7 +1006,7 @@
     scratch.file(
         "test/skylark/extension.bzl",
         "def helper_func(attr1):",
-        "  return set(attr1)",
+        "  return depset(attr1)",
         "",
         "def custom_rule_impl(ctx):",
         "  attr1 = ctx.files.attr1",
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
index 066c544..69783f2 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
@@ -943,7 +943,7 @@
 
   @Test
   public void testStructsInSets() throws Exception {
-    eval("set([struct(a='a')])");
+    eval("depset([struct(a='a')])");
   }
 
   @Test
@@ -977,9 +977,7 @@
 
   @Test
   public void testNsetGoodCompositeItem() throws Exception {
-    eval("def func():",
-        "  return set([struct(a='a')])",
-        "s = func()");
+    eval("def func():", "  return depset([struct(a='a')])", "s = func()");
     Collection<Object> result = ((SkylarkNestedSet) lookup("s")).toCollection();
     assertThat(result).hasSize(1);
     assertThat(result.iterator().next()).isInstanceOf(SkylarkClassObject.class);
@@ -987,8 +985,8 @@
 
   @Test
   public void testNsetBadMutableItem() throws Exception {
-    checkEvalError("sets cannot contain mutable items", "set([([],)])");
-    checkEvalError("sets cannot contain mutable items", "set([struct(a=[])])");
+    checkEvalError("depsets cannot contain mutable items", "depset([([],)])");
+    checkEvalError("depsets cannot contain mutable items", "depset([struct(a=[])])");
   }
 
   private static SkylarkClassObject makeStruct(String field, Object value) {
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 802142f..e8a3675 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
@@ -229,7 +229,7 @@
   @SuppressWarnings("unchecked")
   @Test
   public void testListComprehensionsWithNestedSet() throws Exception {
-    Object result = eval("[x + x for x in set([1, 2, 3])]");
+    Object result = eval("[x + x for x in depset([1, 2, 3])]");
     assertThat((Iterable<Object>) result).containsExactly(2, 4, 6).inOrder();
   }
 
@@ -675,9 +675,9 @@
   @Test
   public void testRunfilesBadSetGenericType() throws Exception {
     checkErrorContains(
-        "expected set of Files or NoneType for 'transitive_files' while calling runfiles "
-            + "but got set of ints instead: set([1, 2, 3])",
-        "ruleContext.runfiles(transitive_files=set([1, 2, 3]))");
+        "expected depset of Files or NoneType for 'transitive_files' while calling runfiles "
+            + "but got depset of ints instead: depset([1, 2, 3])",
+        "ruleContext.runfiles(transitive_files=depset([1, 2, 3]))");
   }
 
   @Test
@@ -728,7 +728,7 @@
     Object result =
         evalRuleContextCode(
             ruleContext,
-            "ftb = set() + ruleContext.files.srcs",
+            "ftb = depset() + ruleContext.files.srcs",
             "ruleContext.runfiles(transitive_files = ftb)");
     assertEquals(
         ActionsTestUtil.baseArtifactNames(getRunfileArtifacts(result)),
@@ -803,7 +803,7 @@
   @Test
   public void testNsetContainsList() throws Exception {
     checkErrorContains(
-        "sets cannot contain items of type 'list'", "set() + [ruleContext.files.srcs]");
+        "depsets cannot contain items of type 'list'", "depset() + [ruleContext.files.srcs]");
   }
 
   @Test
@@ -811,7 +811,7 @@
     SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
     Object result =
         evalRuleContextCode(
-            ruleContext, "f = set(ruleContext.files.srcs)", "cmd_helper.join_paths(':', f)");
+            ruleContext, "f = depset(ruleContext.files.srcs)", "cmd_helper.join_paths(':', f)");
     assertEquals("foo/a.txt:foo/b.img", result);
   }