forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 29
Add @IfNullThrows parameter annotation and unify with AssertMethod in CFG #1566
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
Draft
HenryXi1
wants to merge
3
commits into
eisop:master
Choose a base branch
from
HenryXi1:add-if-null-throws-contract
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
35 changes: 35 additions & 0 deletions
35
checker-qual/src/main/java/org/checkerframework/checker/nullness/qual/IfNullThrows.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,35 @@ | ||
| package org.checkerframework.checker.nullness.qual; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * A parameter annotation indicating that the method throws an exception if this parameter is null. | ||
| * | ||
| * <p>When the CFG is built, calls to methods with {@code @IfNullThrows} on a parameter are | ||
| * translated into an explicit branch: if the argument is null, the method throws; otherwise | ||
| * execution continues. This enables flow-sensitive refinement in type checkers (e.g., the Nullness | ||
| * Checker refines the argument to non-null on the continue path). | ||
| * | ||
| * <p><b>Semantic meaning:</b> {@code @IfNullThrows} means: <i>if this parameter is null, then the | ||
| * method throws</i>. Equivalently: when the method returns normally, the parameter was non-null. | ||
| * | ||
| * <p><b>Example:</b> | ||
| * | ||
| * <pre><code> | ||
| * public static <T> T requireNonNull(@IfNullThrows @Nullable T obj) { | ||
| * if (obj == null) throw new NullPointerException(); | ||
| * return obj; | ||
| * } | ||
| * </code></pre> | ||
| * | ||
| * @checker_framework.manual #type-refinement Automatic type refinement (flow-sensitive type | ||
| * qualifier inference) | ||
| */ | ||
| @Documented | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.PARAMETER) | ||
| public @interface IfNullThrows {} | ||
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
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
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,60 @@ | ||
| // Test that @IfNullThrows (parameter-level postcondition: if null then throw) is respected by the | ||
| // Nullness Checker. | ||
|
|
||
| import org.checkerframework.checker.nullness.qual.IfNullThrows; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| public class IfNullThrowsTest { | ||
|
|
||
| // requireNonNull-style: if param is null, throws; so when returns, param is non-null | ||
| public static <T> T requireNonNull(@IfNullThrows @Nullable T obj) { | ||
| if (obj == null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also in these tests, ensure that the qualifier is actually enforced - add an incorrect implementation and ensure we raise an error. |
||
| throw new NullPointerException(); | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
| void useRequireNonNull(@Nullable String s) { | ||
| requireNonNull(s); | ||
| s.toString(); // OK: s refined to non-null after requireNonNull returns | ||
| } | ||
|
|
||
| // With message parameter | ||
| public static <T> T requireNonNull(@IfNullThrows @Nullable T obj, String msg) { | ||
| if (obj == null) { | ||
| throw new NullPointerException(msg); | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
| void useRequireNonNullWithMsg(@Nullable String s) { | ||
| requireNonNull(s, "s must not be null"); | ||
| s.toString(); // OK | ||
| } | ||
|
|
||
| // Multiple parameters with @IfNullThrows - validates both must be non-null to return | ||
| public static void requireBothNonNull( | ||
| @IfNullThrows @Nullable Object a, @IfNullThrows @Nullable Object b) { | ||
| if (a == null || b == null) { | ||
| throw new NullPointerException(); | ||
| } | ||
| } | ||
|
|
||
| void useRequireBothNonNull(@Nullable Object x, @Nullable Object y) { | ||
| requireBothNonNull(x, y); | ||
| x.toString(); // OK | ||
| y.toString(); // OK | ||
| } | ||
|
|
||
| // Incorrect: has @IfNullThrows but never throws | ||
| // :: error: (if.null.throws.must.throw) | ||
| public static <T> T badNoThrow(@IfNullThrows @Nullable T obj) { | ||
| return obj; | ||
| } | ||
|
|
||
| // Incorrect: has @IfNullThrows but only returns, no throw | ||
| // :: error: (if.null.throws.must.throw) | ||
| public static String badJustReturn(@IfNullThrows @Nullable String s) { | ||
| return s == null ? "" : s; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
This qualifier should be in
checker/.../nullness, not in the framework.