Skip to content

Add cast and isinstance keywords - #1505

Merged
fmeum merged 1 commit into
bazel-contrib:mainfrom
tetromino:cast-isinstance
Sep 16, 2026
Merged

fmeum merged 1 commit into
bazel-contrib:mainfrom
tetromino:cast-isinstance

Conversation

@tetromino

@tetromino tetromino commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Finishing support for the type syntax proposed in bazelbuild/starlark#362

After some reflection, I think we should always parse cast/isinstance as keywords (requiring 2 arguments, one of which is a value expression and the other a type expression) since permitting pre-type Starlark syntax is likely to only lead to confusion, and would be quite difficult to get right.

Note that this is an incompatible change: forms like cast(x, y, z) or cast = foo will now be a parse error. However, in practice, I observe no occurrences of cast or isinstance used as identifiers in any Starlark code in Google's monorepo, so I believe that it's rather unlikely to break anyone.

Store the parsed cast/isinstance exprs as CallExpr-s (to avoid diverging their formatting/walking/etc. support from call expressions) with an enum field to mark that they are of a special kind.

Buildtools PR checklist

  • The code in this PR is covered by unit/integration tests.
  • I have tested these changes and provide testing instructions below.
  • I have either responded to, or resolved all Gemini comments on the PR.
  • I have read Google Eng Practices on Small Changes, this PR either follows these guidelines or the description provides reasoning for why they can not be followed.

@fmeum

fmeum commented Sep 4, 2026

Copy link
Copy Markdown
Member

Needs a rebase after the Buck2 fix

@fmeum

fmeum commented Sep 16, 2026

Copy link
Copy Markdown
Member

I unfortunately missed that this didn't end up merging for v10. I will send a v10.0.2 with this and argue for it to be a bugfix for type support, but call out the incompatible change.

Finishing support for the type syntax proposed in bazelbuild/starlark#362

After some reflection, I think we should always parse cast/isinstance
as keywords (requiring 2 arguments, one of which is a value expression
and the other a type expression) since permitting pre-type Starlark
syntax is likely to only lead to confusion, and would be quite
difficult to get right.

Note that we observe no occurrences of `cast` or `isinstance` as
identifiers in any Starlark code in the Google's monorepo.

Store the parsed cast/isinstance exprs as CallExpr-s (to avoid diverging
their formatting/walking/etc. support from call expressions) with an
enum field to mark that they are of a special kind.
@fmeum
fmeum enabled auto-merge (squash) September 16, 2026 10:49
@fmeum

fmeum commented Sep 16, 2026

Copy link
Copy Markdown
Member

@tetromino I rebased the PR onto main and will merge it now.

@fmeum
fmeum merged commit 660070f into bazel-contrib:main Sep 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants