Skip to content
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

Array creation annotation checks #928

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

thisisalexandercook
Copy link
Collaborator

@thisisalexandercook thisisalexandercook commented Sep 30, 2024

fixes #927 , added a warning if the first dimension (element type) of array creation is annotated with nullable. Warning will inform user that this annotation has been ignored. Also added a new error message to support the warning.

@Ao-senXiong
Copy link
Member

@thisisalexandercook Please also add test case to show the the check is implemented correctly.

TreeUtils.annotationsFromArrayCreation(tree, 0);
if (AnnotationUtils.containsSame(annotations, NULLABLE)) {
checker.reportWarning(tree, "new.array.nullable.ignored");
}
Copy link
Member

Choose a reason for hiding this comment

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

can we use this error message?
new.array.type.invalid=annotations %s may not be applied as component type for array "%s"

Copy link
Member

Choose a reason for hiding this comment

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

No, because it is not about the component type.

@wmdietl wmdietl changed the title Array element nullable annotation warning Array creation annotation checks Oct 2, 2024
Copy link
Member

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

Can you also look at checker/src/main/java/org/checkerframework/checker/nullness/NullnessNoInitAnnotatedTypeFactory.java and see why visitNewArray and visitNewClass are slightly different? Is there a reason one doesn't call super?

In a separate PR, can you also look at why we backed out fb6cb61 12 years ago? Why doesn't the simpler check work?

Comment on lines 351 to 353
List<? extends AnnotationMirror> annotations =
TreeUtils.annotationsFromArrayCreation(tree, 0);
if (AnnotationUtils.containsSame(annotations, NULLABLE)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
List<? extends AnnotationMirror> annotations =
TreeUtils.annotationsFromArrayCreation(tree, 0);
if (AnnotationUtils.containsSame(annotations, NULLABLE)) {
if (type.hasAnnotation(NULLABLE)) {

type is the type of the NewArrayTree, so wouldn't this perform the check you want?

Copy link
Member

Choose a reason for hiding this comment

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

What about @PolyNull? Is that allowed? MonotonicNonNull?
Are some of these checks maybe in isValid?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

type is holding what the annotation defaulted to (i.e. @NonNull) and I couldn't find a utility to retrieve the original @Nullable annotation (i.e. getExplicitonAnnotation() does not hold onto it), so I pull the annotations directly from the 0th dim of the tree.

Thanks for bringing up @PolyNull and @MonotonicNonNull, both suggest possible nullness so I think they should be disallowed.

public class ArrayMainModifierNullableAnnotation {
void foo() {
// :: warning: (new.array.nullable.ignored)
int[] o = new int @Nullable [10];
Copy link
Member

Choose a reason for hiding this comment

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

It's usually a good idea to include a test where you expect no error. Here, how about adding something with new int [10] @Nullable [] to make sure that is allowed?

@@ -22,6 +22,7 @@ nulltest.redundant=redundant test against null; "%s" is non-null
instanceof.nullable=instanceof is only true for a non-null expression
instanceof.nonnull.redundant=redundant @NonNull annotation on instanceof
Copy link
Member

Choose a reason for hiding this comment

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

Should we have a similar warning for object and array creations?
On the other hand, there is the -AwarnRedundantAnnotations option... so should we remove the special case for instanceof? If needed, please file a new issue for this.

TreeUtils.annotationsFromArrayCreation(tree, 0);
if (AnnotationUtils.containsSame(annotations, NULLABLE)) {
checker.reportWarning(tree, "new.array.nullable.ignored");
}
Copy link
Member

Choose a reason for hiding this comment

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

No, because it is not about the component type.

@@ -22,6 +22,7 @@ nulltest.redundant=redundant test against null; "%s" is non-null
instanceof.nullable=instanceof is only true for a non-null expression
instanceof.nonnull.redundant=redundant @NonNull annotation on instanceof
new.array.type.invalid=annotations %s may not be applied as component type for array "%s"
new.array.nullable.ignored=nullable annotation on new array main modifier ignored, must be nonnull
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any other message, here or in the basetype, refer to the main modifier... which is odd... somewhere we called it primary, but I also don't see that in any of the error messages... Can you look what would be a consistent way to word this?

Other messages in this file also don't use nullable and use non-null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I also can't see any reference to main or primary anywhere else. Where does this wording originate from, what other contexts is it used in?

We could use the JLS wording:

Suggested change
new.array.nullable.ignored=nullable annotation on new array main modifier ignored, must be nonnull
new.array.nullable.ignored=array type annotation must be nonnull

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.

Array creation is intrinsically non-null
3 participants