forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 29
Add Queue.isEmpty/poll refinement #1603
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
nhioe
wants to merge
6
commits into
eisop:master
Choose a base branch
from
nhioe:queue-is-empty-poll-refinement
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.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1ccfb9d
Add Queue.isEmpty/poll refinement
nhioe 29323d6
Add support for queue in store
nhioe 0e630ad
consistent vars in tests
nhioe 0178723
Make updateForMethodCall more conservative for queues
nhioe 2f98f00
Merge branch 'master' into queue-is-empty-poll-refinement
nhioe e6d8cdc
Merge branch 'master' into queue-is-empty-poll-refinement
wmdietl 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
Some comments aren't visible on the classic Files Changed page.
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
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |||||||
|
|
||||||||
| import java.util.List; | ||||||||
| import java.util.Map; | ||||||||
| import java.util.Queue; | ||||||||
|
|
||||||||
| import javax.lang.model.element.AnnotationMirror; | ||||||||
| import javax.lang.model.element.ExecutableElement; | ||||||||
|
|
@@ -81,6 +82,14 @@ public class NullnessNoInitTransfer | |||||||
| */ | ||||||||
| protected final AnnotatedDeclaredType MAP_TYPE; | ||||||||
|
|
||||||||
| /** | ||||||||
| * Java's Queue interface. | ||||||||
| * | ||||||||
| * <p>The qualifiers in this type don't matter -- it is not used as a fully-annotated | ||||||||
| * AnnotatedDeclaredType, but just passed to asSuper(). | ||||||||
| */ | ||||||||
| protected final AnnotatedDeclaredType QUEUE_TYPE; | ||||||||
|
|
||||||||
| /** The type factory for the nullness analysis that was passed to the constructor. */ | ||||||||
| protected final NullnessNoInitAnnotatedTypeFactory nullnessTypeFactory; | ||||||||
|
|
||||||||
|
|
@@ -130,6 +139,14 @@ public NullnessNoInitTransfer(NullnessNoInitAnalysis analysis) { | |||||||
| nullnessTypeFactory, | ||||||||
| false); | ||||||||
|
|
||||||||
| QUEUE_TYPE = | ||||||||
| (AnnotatedDeclaredType) | ||||||||
| AnnotatedTypeMirror.createType( | ||||||||
| TypesUtils.typeFromClass( | ||||||||
| Queue.class, analysis.getTypes(), elements), | ||||||||
| nullnessTypeFactory, | ||||||||
| false); | ||||||||
|
|
||||||||
| nonNullAssumptionAfterInvocation = | ||||||||
| !analysis.getTypeFactory() | ||||||||
| .getChecker() | ||||||||
|
|
@@ -464,6 +481,34 @@ public TransferResult<NullnessNoInitValue, NullnessNoInitStore> visitMethodInvoc | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Handle Collection.isEmpty(), mark receiver as non-empty in the false branch | ||||||||
| if (nullnessTypeFactory.isCollectionIsEmpty(n)) { | ||||||||
| JavaExpression receiverExpr = JavaExpression.fromNode(receiver); | ||||||||
| if (CFAbstractStore.canInsertJavaExpression(receiverExpr)) { | ||||||||
| NullnessNoInitStore thenStore = result.getThenStore(); | ||||||||
| NullnessNoInitStore elseStore = result.getElseStore(); | ||||||||
| elseStore.insertValue(receiverExpr, NONNULL); | ||||||||
| return new ConditionalTransferResult<>( | ||||||||
| result.getResultValue(), thenStore, elseStore); | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // Refine result to @NonNull if n is an invocation of Queue.poll(), the receiver is known to | ||||||||
| // be non-empty | ||||||||
| // and Queue element type is @NonNull | ||||||||
|
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.
Suggested change
I like the Oxford comma. |
||||||||
| if (nullnessTypeFactory.isQueuePoll(n)) { | ||||||||
| NullnessNoInitStore store = result.getRegularStore(); | ||||||||
| JavaExpression receiverExpr = JavaExpression.fromNode(receiver); | ||||||||
| NullnessNoInitValue receiverValue = store.getValue(receiverExpr); | ||||||||
| if (receiverValue != null && receiverValue.getAnnotations().contains(NONNULL)) { | ||||||||
| AnnotatedTypeMirror receiverType = nullnessTypeFactory.getReceiverType(n.getTree()); | ||||||||
| if (!isElementTypeNullable(receiverType)) { | ||||||||
| makeNonNull(result, n); | ||||||||
| refineToNonNull(result); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return result; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -485,6 +530,24 @@ private boolean isValueTypeNullable(AnnotatedTypeMirror mapOrSubtype) { | |||||||
| return valueType.hasAnnotation(NULLABLE); | ||||||||
| } | ||||||||
|
|
||||||||
| /** | ||||||||
| * Returns true if queueType's element type (the E type argument to Queue) is @Nullable. | ||||||||
| * | ||||||||
| * @param queueOrSubtype the Queue type, or a subtype | ||||||||
| * @return true if queueType's element type is @Nullable | ||||||||
| */ | ||||||||
| private boolean isElementTypeNullable(AnnotatedTypeMirror queueOrSubtype) { | ||||||||
| AnnotatedDeclaredType queueType = | ||||||||
| AnnotatedTypes.asSuper(nullnessTypeFactory, queueOrSubtype, QUEUE_TYPE); | ||||||||
| int numTypeArguments = queueType.getTypeArguments().size(); | ||||||||
| if (numTypeArguments != 1) { | ||||||||
| throw new TypeSystemError( | ||||||||
| "Wrong number %d of type arguments: %s", numTypeArguments, queueType); | ||||||||
| } | ||||||||
| AnnotatedTypeMirror elementType = queueType.getTypeArguments().get(0); | ||||||||
| return elementType.hasAnnotation(NULLABLE); | ||||||||
| } | ||||||||
|
|
||||||||
| @Override | ||||||||
| public TransferResult<NullnessNoInitValue, NullnessNoInitStore> visitReturn( | ||||||||
| ReturnNode n, TransferInput<NullnessNoInitValue, NullnessNoInitStore> in) { | ||||||||
|
|
||||||||
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
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.
The sentence read a bit odd. Documentation comments should still end with a period.
You need to explain somewhere how you mark the receiver as non-empty. The body adds NONNULL, which seems a bit odd - the receiver for any method invocation should already be non-null. So how is this conveying any information you can test for below?
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.
Semantically, marking it NONNULL doesn't map perfectly to collection state, but it acts as a signal to
poll()about what we learned fromisEmpty, so we can refine the return type. Is a custom annotation for non-empty worth exploring?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.
Could you add something about
poll()to the Store? Check the conditions here and set the return type ofpoll()to non-null here? Hopefully such a fact would automatically removed on side effects.