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
72 files changed
tree: 6e8342c77b51c92a9d7eaf1a9ab1354fbdf80c8f
  1. .bazelci/
  2. examples/
  3. scripts/
  4. site/
  5. src/
  6. third_party/
  7. tools/
  8. .bazelrc
  9. .gitattributes
  10. .gitignore
  11. AUTHORS
  12. BUILD
  13. CHANGELOG.md
  14. CODEBASE.md
  15. CODEOWNERS
  16. combine_distfiles.py
  17. combine_distfiles_to_tar.sh
  18. compile.sh
  19. CONTRIBUTING.md
  20. CONTRIBUTORS
  21. distdir.bzl
  22. ISSUE_TEMPLATE.md
  23. LICENSE
  24. README.md
  25. WORKSPACE
README.md

Bazel

{Fast, Correct} - Choose two

Build and test software of any size, quickly and reliably.

  • Speed up your builds and tests: Bazel rebuilds only what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.

  • One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

  • Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.

  • Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.

Getting Started

Documentation

Contributing to Bazel

See CONTRIBUTING.md

Build status