BEGIN_PUBLIC
starlark: infinite precision ints
BEWARE: j.l.Integer is no longer a legal Starlark value.
This change makes the Starlark 'int' type a bigint,
aka infinite-precision integer, one capable of exact arithmetic
on any integer value. The new Int class has three subclasses,
similar to j.l.Integer, j.l.Long, and j.l.BigInteger.
The most compact representation is always used.
This makes Starlark capable of handling all the integer
types that occur in protocol messages---signed and unsigned
64-bit values.
Memory usage should not change much because StarlarkInt.Int32
has the same layout as the j.l.Integer it replaces.
As with j.l.Integer, small values (<100,000) are cached
to avoid unnecessary allocation.
Integer is no longer a legal Starlark value type. However,
for compatibility and convenience, parameters of
StarlarkMethod-annotated Java functions may continue to
use Integer to mean "32-bit signed int". The interpreter
does a "reboxing" operation to convert Int arguments
to Integer parameters as needed. Also, such functions may
return Integer values, and they will be reboxed by fromJava,
similar to List and Map. However, just as one cannot return
List and Map values nested inside Starlark data structures,
nor can one nest Integers in them; an explicit boxing operation
is required: StarlarkInt.of(x).
To limit the scope of this change, it does not yet add support
for parsing bigint literals. That will come in a follow-up.
Bazel: attr.int(..) rule attributes now use Int instead of
Integer. This is necessary because the assumption that Attribute
values are all legal Starlark values seems to be widely relied on.
However, int attributes remain restricted to the signed 32-bit
part of the value range. (Changing this would have much greater
ramifications for Bazel.) Every access of an int-valued rule
attribute must now call toIntUnchecked(), which cannot fail.
Suggested reading order:
- eval.StarlarkInt, the new type.
- the rest of eval, which does reboxing.
- trivial updates to tests of the interpreter.
(Most of these tests belong in testdata/*.star files.)
- lib.packages attribute changes.
- the rest, which is mostly trivial updates.
The most obvious downsides of this change are the loss of implicit
boxing, the potential for latent errors due to the lack of dynamic
checkValid calls in (e.g.) Dict.put, and the need to tell other Java
packages (such as the Gson JSON package) that Int is basically a
version of Integer, which it already knows about.
I have no doubt missed a few spots, and we may encounter a few
unhelpful "want int, got int" errors when an Integer value
sneaks into the Starlark value realm. I will fix them as they
arise.
Credit to Jon Brandvein for b/36358845#comment9. Before it, I
had resigned myself to this feature being infeasible in Starlark/Java.
END_PUBLIC
PiperOrigin-RevId: 334649352
diff --git a/src/test/java/com/google/devtools/build/lib/packages/AttributeTest.java b/src/test/java/com/google/devtools/build/lib/packages/AttributeTest.java
index 998a9fe..75b4d8e 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/AttributeTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/AttributeTest.java
@@ -42,6 +42,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
+import net.starlark.java.eval.StarlarkInt;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@@ -60,9 +61,9 @@
@Test
public void testBasics() throws Exception {
- Attribute attr = attr("foo", Type.INTEGER).mandatory().value(3).build();
+ Attribute attr = attr("foo", Type.INTEGER).mandatory().value(StarlarkInt.of(3)).build();
assertThat(attr.getName()).isEqualTo("foo");
- assertThat(attr.getDefaultValue(null)).isEqualTo(3);
+ assertThat(attr.getDefaultValue(null)).isEqualTo(StarlarkInt.of(3));
assertThat(attr.getType()).isEqualTo(Type.INTEGER);
assertThat(attr.isMandatory()).isTrue();
assertThat(attr.isDocumented()).isTrue();
@@ -75,7 +76,7 @@
NullPointerException e =
assertThrows(
NullPointerException.class,
- () -> attr("foo", Type.INTEGER).nonEmpty().value(3).build());
+ () -> attr("foo", Type.INTEGER).nonEmpty().value(StarlarkInt.of(3)).build());
assertThat(e).hasMessageThat().isEqualTo("attribute 'foo' must be a list");
}
@@ -92,7 +93,7 @@
IllegalStateException e =
assertThrows(
IllegalStateException.class,
- () -> attr("foo", Type.INTEGER).singleArtifact().value(3).build());
+ () -> attr("foo", Type.INTEGER).singleArtifact().value(StarlarkInt.of(3)).build());
assertThat(e).hasMessageThat().isEqualTo("attribute 'foo' must be a label-valued type");
}
@@ -119,10 +120,8 @@
*/
@Test
public void testConvenienceFactoriesDefaultValues() throws Exception {
- assertDefaultValue(0,
- attr("x", INTEGER).build());
- assertDefaultValue(42,
- attr("x", INTEGER).value(42).build());
+ assertDefaultValue(StarlarkInt.of(0), attr("x", INTEGER).build());
+ assertDefaultValue(StarlarkInt.of(42), attr("x", INTEGER).value(StarlarkInt.of(42)).build());
assertDefaultValue("",
attr("x", STRING).build());
@@ -158,8 +157,7 @@
public void testConvenienceFactoriesTypes() throws Exception {
assertType(INTEGER,
attr("x", INTEGER).build());
- assertType(INTEGER,
- attr("x", INTEGER).value(42).build());
+ assertType(INTEGER, attr("x", INTEGER).value(StarlarkInt.of(42)).build());
assertType(STRING,
attr("x", STRING).build());