Skip to content

Add java.util.OptionalInt - #41

Merged
tautschnig merged 1 commit into
masterfrom
model-optionalint
Nov 4, 2026
Merged

tautschnig merged 1 commit into
masterfrom
model-optionalint

Conversation

@tautschnig

@tautschnig tautschnig commented Jul 28, 2026 •

Copy link
Copy Markdown
Collaborator

Without a model, JBMC treats OptionalInt as a stub class, so that the
results of OptionalInt.of, isPresent, getAsInt etc. are
nondeterministic.

Like the other models in this library, this is the OpenJDK 8 source
file, with changes marked "DIFFBLUE MODEL LIBRARY". The only change is
the addition of isEmpty from JDK 11. The JDK implementation needs no
other changes for JBMC: it has no loops, no native methods, and its
empty instance is a final static field, which JBMC's --nondet-static
does not make nondeterministic.

Checked with JBMC on a program that asserts the documented behaviour of
empty, of, isPresent, isEmpty, getAsInt, orElse, equals and hashCode:
all assertions hold, and two false assertions are reported as failing,
with and without --symex-driven-lazy-loading and --nondet-static.

Copilot AI review requested due to automatic review settings July 28, 2026 23:05

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

Adds a minimal, executable JBMC model for java.util.OptionalInt to support deterministic construction and basic accessors consistent with the JDK contract.

Changes:

  • Introduces java.util.OptionalInt with empty(), of(int), isPresent(), isEmpty(), and getAsInt() (throwing NoSuchElementException when absent).
  • Adds an orElse(int) convenience method (not mentioned in the PR description).

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

Comment on lines +1 to +7
package java.util;

/**
* JBMC model of java.util.OptionalInt.
* A simple value container: either empty or holds an int.
*/
public final class OptionalInt {
Without a model, JBMC treats OptionalInt as a stub class, so that the
results of OptionalInt.of, isPresent, getAsInt etc. are
nondeterministic.

Like the other models in this library, this is the OpenJDK 8 source
file, with changes marked "DIFFBLUE MODEL LIBRARY". The only change is
the addition of isEmpty from JDK 11. The JDK implementation needs no
other changes for JBMC: it has no loops, no native methods, and its
empty instance is a final static field, which JBMC's --nondet-static
does not make nondeterministic.

Checked with JBMC on a program that asserts the documented behaviour of
empty, of, isPresent, isEmpty, getAsInt, orElse, equals and hashCode:
all assertions hold, and two false assertions are reported as failing,
with and without --symex-driven-lazy-loading and --nondet-static.

Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
@tautschnig tautschnig changed the title OptionalInt model: deterministic of/empty/isPresent/isEmpty/getAsInt Add java.util.OptionalInt Nov 4, 2026
@tautschnig
tautschnig merged commit 47f1fc7 into master Nov 4, 2026
1 check passed
@tautschnig
tautschnig deleted the model-optionalint branch November 4, 2026 10:42
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