Skip to content

JavaToLaurelCompiler: collapse reference comparison against null to a constant - #438

Open
tautschnig wants to merge 1 commit into
strata-org:mainfrom
tautschnig:pr/null-comparison-collapse
Open

JavaToLaurelCompiler: collapse reference comparison against null to a constant#438
tautschnig wants to merge 1 commit into
strata-org:mainfrom
tautschnig:pr/null-comparison-collapse

Conversation

@tautschnig

@tautschnig tautschnig commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

In JVerify's array-as-map model an array-typed value is never null, so arr == null / arr != null (detected via a BOT-typed null-literal operand) is folded to a boolean constant — false / true respectively — before operand conversion. This keeps the BOT-typed null literal off the convertLiteral path.

The fold is restricted to comparisons whose non-null operand is array-typed (map-backed), so it does not silently mask the nullability of ordinary object references — those fall through to the normal conversion path. The degenerate null == null / null != null case folds to true / false respectively.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0.

Copilot AI review requested due to automatic review settings June 9, 2026 13:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds an early rewrite in the Java→Laurel compiler to treat == null / != null as constant booleans (since the Laurel model has no null), avoiding literal conversion for BOT-typed null literals.

Changes:

  • Detects EQ/NE comparisons where either operand is a null literal (TypeTag.BOT)
  • Rewrites those comparisons to boolean constants prior to recursive operand conversion

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

In JVerify's array-as-map model an array-typed value is never null, so
`arr == null` / `arr != null` (detected via a BOT-typed null literal
operand) is folded to a boolean constant (false / true respectively)
before operand conversion. This keeps the BOT-typed literal off the
convertLiteral path.

The fold is restricted to comparisons whose non-null operand is
array-typed, so it does not silently mask the nullability of ordinary
object references — those fall through to the normal conversion path.
The degenerate `null == null` / `null != null` case folds to true /
false respectively.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig
tautschnig force-pushed the pr/null-comparison-collapse branch from 2589d34 to 5270d6a Compare June 9, 2026 14:29

@fabiomadge fabiomadge left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is unreachable until array support lands — array-typed operands error at translateType today (verified), so arr == null never reaches this fold. #441 (array support) is what makes it reachable, and notably #441 doesn't need this — it doesn't touch arr == null handling at all.

Suggestion: punt this until arrays land, and fold it into #441 (or a follow-up on top of it). Reasons: (1) it can't be tested in isolation today; (2) the "arrays are never null" assumption is only meaningful once #441's mapType(int, int) model exists — at which point it's verifiable; (3) the instanceof Type.ArrayType check would also match @Nullable int[], harmless now (@Nullable is a no-op — isNullable has zero call sites) but worth settling alongside the real array+null model rather than ahead of it.

The logic itself looks reasonable; it's just landing ahead of the thing it depends on.

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.

3 participants