Skip to content

Add Null Support#143

Draft
rcosta358 wants to merge 11 commits intomainfrom
null-support
Draft

Add Null Support#143
rcosta358 wants to merge 11 commits intomainfrom
null-support

Conversation

@rcosta358
Copy link
Collaborator

@rcosta358 rcosta358 commented Feb 7, 2026

This PR adds support for null checking in LiquidJava.

Changes

Refinements Language

  • Added the null literal to the grammar
  • Added the LiteralNull AST node

Typing

  • The literal null can be assigned to any non-primitive type
  • String literals, literals of boxed types and new object instances implicitly carry the refinement _ != null, while the literal null carries the refinement _ == null
  • When a class field is declared without a value, it is implicitly refined with _ == null

Z3 Translation

  • Nulls are converted to uninterpreted constants of the uninterpreted sort java.lang.Object
  • In equality comparisons, null literals are converted to the sort of the other operand to match both sorts

Simplification

  • The simplifier removes redundant null checks when an equality to a non-null literal already implies a non-null value. This way, s == -1 && s != null simplifies to s == -1 instead of -1 != null

Testing

  • Added various tests to ensure the correctness of these changes

@rcosta358 rcosta358 self-assigned this Feb 7, 2026
@rcosta358 rcosta358 added the enhancement New feature or request label Feb 7, 2026
Copy link
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

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

For the nullness check, I think we need more cases, like what if we declare the var and it doesn't have a value?
There are full tools just for the nullness checking (e.g., NullAway https://github.com/uber/NullAway)

PS: this would be so much easier if it were 2 prs, one for null and one for boxed types -- the boxed types would be an easy squash and merge

@rcosta358

This comment was marked as resolved.

rcosta358

This comment was marked as resolved.

@rcosta358 rcosta358 changed the title Add Null & Boxed Types Support Add Null Support Feb 13, 2026
Copy link
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

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

This is nice to have, but I have some questions (we already discussed some offline):

  • obj != null and state. It would be great if we didnt have to pollute the refinements with always having to write this != null. Maybe we can assume that all object states imply != null?
  • null of fields are trickier, check the comment below

Copy link
Collaborator

@CatarinaGamboa CatarinaGamboa left a comment

Choose a reason for hiding this comment

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

this comment

Integer x; // implicit null

void test() {
mustBeNull(x);
Copy link
Collaborator

Choose a reason for hiding this comment

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

But we don't know if this is null right? how do we know? what if there was a constructor, what if another method is called first and sets this fiels? what are the assumptions here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah you're right. I'll try to handle those cases.

@rcosta358 rcosta358 marked this pull request as draft February 13, 2026 15:57
@rcosta358 rcosta358 marked this pull request as draft February 13, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants