-
Notifications
You must be signed in to change notification settings - Fork 3
JavaToLaurel: support null / reference-constant comparisons and values
#460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tautschnig
wants to merge
1
commit into
strata-org:main
Choose a base branch
from
tautschnig:le1-null-reference-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...test/java/org/strata/jverify/verifier/tests/javasupport/NullReferenceLimitationsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package org.strata.jverify.verifier.tests.javasupport; | ||
|
|
||
| import org.strata.jverify.testengine.JVerifyTest; | ||
|
|
||
| import static org.strata.jverify.JVerify.check; | ||
|
|
||
| /** | ||
| * Deliberate limitations of the stop-gap per-sort {@code <sort>$null} null | ||
| * model (see {@code JavaToLaurelCompiler.NULL_REF_SUFFIX}). These are the cases | ||
| * the eventual Laurel nullable model is meant to handle; pinning them as | ||
| * should-fail shows the current model is neither unsound nor over-permissive: | ||
| * it treats an unconstrained reference as possibly-{@code null} and | ||
| * possibly-non-{@code null}, and cannot invent non-nullness. | ||
| * | ||
| * <p>Companion to {@code NullReferenceTest}, which covers what <em>does</em> | ||
| * work. | ||
| */ | ||
| @JVerifyTest(exitCode = 4, methodsVerified = 1, errorCount = 3) | ||
| class NullReferenceLimitationsTest { | ||
|
|
||
| /** | ||
| * Not over-permissive: an unconstrained reference cannot be proved | ||
| * non-null — it may be the sort's {@code $null} element. | ||
| */ | ||
| static void cannotProveParamNonNull(Object o) { | ||
| check(o != null); | ||
| // ^^^^^^^^^^^^^^^^ Error: assertion does not hold | ||
| } | ||
|
|
||
| /** | ||
| * ...and, symmetrically, cannot be proved null. The model is agnostic | ||
| * about an unconstrained reference, not biased either way. | ||
| */ | ||
| static void cannotProveParamNull(Object o) { | ||
| check(o == null); | ||
| // ^^^^^^^^^^^^^^^^ Error: assertion does not hold | ||
| } | ||
|
|
||
| /** | ||
| * A freshly allocated object is not provably non-null: {@code new T(...)} | ||
| * lowers to an opaque {@code new_(T)} value carrying no fact that it | ||
| * differs from {@code T$null}. | ||
| */ | ||
| static void cannotProveFreshObjectNonNull() { | ||
| Object o = new Object(); | ||
| check(o != null); | ||
| // ^^^^^^^^^^^^^^^^ Error: assertion does not hold | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
verifier/src/test/java/org/strata/jverify/verifier/tests/javasupport/NullReferenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package org.strata.jverify.verifier.tests.javasupport; | ||
|
|
||
| import org.strata.jverify.Pure; | ||
| import org.strata.jverify.testengine.JVerifyTest; | ||
|
|
||
| import static org.strata.jverify.JVerify.postcondition; | ||
| import static org.strata.jverify.JVerify.precondition; | ||
|
|
||
| /** | ||
| * Reference comparisons against the {@code null} literal, and standalone | ||
| * {@code null} values, now translate to Laurel instead of crashing with | ||
| * "Unsupported constant type tag: BOT". | ||
| * | ||
| * <p>An object reference is compared to / assigned the sort's distinguished | ||
| * {@code <sort>$null} value (an uninterpreted element of the otherwise-opaque | ||
| * reference sort). A standalone null picks up its reference sort from the | ||
| * use-site expected type (return type, declared local type, assignment target, | ||
| * or callee parameter type). | ||
| */ | ||
| @JVerifyTest(methodsVerified = 7, errorCount = 0) | ||
| public class NullReferenceTest { | ||
|
|
||
| /** `o != null` in a precondition. */ | ||
| int withNonNullPrecondition(Object o) { | ||
| precondition(o != null); | ||
| return 5; | ||
| } | ||
|
|
||
| /** `o == null` as a value, mirrored in the postcondition. */ | ||
| boolean isNull(Object o) { | ||
| postcondition((boolean r) -> r == (o == null)); | ||
| return o == null; | ||
| } | ||
|
|
||
| /** `return null` — sort taken from the return type. */ | ||
| Object returnsNull() { | ||
| return null; | ||
| } | ||
|
|
||
| /** Local initialised to null, then compared (folds to true). */ | ||
| boolean localNull() { | ||
| postcondition((boolean r) -> r); | ||
| Object a = null; | ||
| return a == null; | ||
| } | ||
|
|
||
| /** Passing `null` as an argument — sort taken from the parameter type. */ | ||
| boolean callWithNull() { | ||
| postcondition((boolean r) -> r); | ||
| return acceptsRef(null); | ||
| } | ||
|
|
||
| @Pure | ||
| boolean acceptsRef(Object o) { | ||
| return o == null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a FYI: the design of JVerify is to assume that all reference variables are non-null, unless annotated with
@Nullable.