Skip to content

Commit

Permalink
Enable Starlark sets by default
Browse files Browse the repository at this point in the history
Now that the Starlark language spec has been approved: https://github.com/bazelbuild/starlark/blob/master/spec.md#sets

Require elements of arguments to StarlarkSet methods to be hashable, matching
the final version of the language spec.

Take the opportunity to update our documentation to match the spec whenever
reasonable (modulo minor differences in terminology and formatting).

RELNOTES: Flip --experimental_enable_starlark_set and enable the Starlark set data type by default.
PiperOrigin-RevId: 707659085
Change-Id: Ibcad59838f9709e980d7b69f4957b8f0fede51c6
  • Loading branch information
tetromino authored and copybara-github committed Dec 18, 2024
1 parent b2ec21e commit 8ae2570
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public final class BuildLanguageOptions extends OptionsBase {

@Option(
name = "experimental_enable_starlark_set",
defaultValue = "false",
defaultValue = "true",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/net/starlark/java/eval/MethodLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,24 @@ public StarlarkInt intForStarlark(Object x, Object baseO) throws EvalException {
@StarlarkMethod(
name = "set",
doc =
"<b>Experimental</b>. This API is experimental and may change at any time. Please do not"
+ " depend on it. It may be enabled on an experimental basis by setting"
+ " <code>--experimental_enable_starlark_set</code>.\n" //
+ "<p>Creates a new <a href=\"../core/set.html\">set</a>, optionally initialized to"
+ " contain the elements from a given iterable.",
"""
Creates a new <a href=\"../core/set.html\">set</a> containing the unique elements of a given
iterable, preserving iteration order.
<p>If called with no argument, <code>set()</code> returns a new empty set.
<p>For example,
<pre class=language-python>
set() # an empty set
set([3, 1, 1, 2]) # set([3, 1, 2]), a set of three elements
set({"k1": "v1", "k2": "v2"}) # set(["k1", "k2"]), a set of two elements
</pre>
""",
parameters = {
@Param(name = "elements", defaultValue = "[]", doc = "A set, sequence, or dict."),
@Param(
name = "elements",
defaultValue = "[]",
doc = "A set, a sequence of hashable values, or a dict."),
},
useStarlarkThread = true)
public StarlarkSet<Object> set(Object elements, StarlarkThread thread) throws EvalException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,5 @@ public final String toString() {
public static final String ALLOW_RECURSION = "-allow_recursion";

/** Whether StarlarkSet objects may be constructed by the interpreter. */
public static final String EXPERIMENTAL_ENABLE_STARLARK_SET = "-experimental_enable_starlark_set";
public static final String EXPERIMENTAL_ENABLE_STARLARK_SET = "+experimental_enable_starlark_set";
}
Loading

2 comments on commit 8ae2570

@fmeum
Copy link
Collaborator

@fmeum fmeum commented on 8ae2570 Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tetromino Please consider cherry-picking this into 8.1.0 to allow the community to adopt it much faster (not having to wait until support for Bazel 8 is dropped).

@tetromino
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fmeum - good point, see #25111

Please sign in to comment.