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

Task2 #1053

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft

Task2 #1053

wants to merge 15 commits into from

Conversation

haewiful
Copy link
Contributor

@haewiful haewiful commented Oct 8, 2024

Thank you for contributing to NullAway!

Please note that once you click "Create Pull Request" you will be asked to sign our Uber Contributor License Agreement via CLA assistant.

Before pressing the "Create Pull Request" button, please provide the following:

  • Added support for generic identity functions with explicitly-typed nullness.

  • Related to the issue JSpecify: support generic methods #1035.

  • void genericNonNullIdentityFunction() and void genericNullAllowingIdentityFunction() unit tests are added to GenericMethodTests.java file.

@msridhar msridhar marked this pull request as draft October 9, 2024 16:19
Copy link

codecov bot commented Oct 14, 2024

Codecov Report

Attention: Patch coverage is 88.09524% with 10 lines in your changes missing coverage. Please review.

Project coverage is 87.60%. Comparing base (91cf25d) to head (f5896df).

Files with missing lines Patch % Lines
...ava/com/uber/nullaway/generics/GenericsChecks.java 87.80% 2 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1053   +/-   ##
=========================================
  Coverage     87.60%   87.60%           
- Complexity     2203     2217   +14     
=========================================
  Files            85       85           
  Lines          7187     7271   +84     
  Branches       1415     1435   +20     
=========================================
+ Hits           6296     6370   +74     
- Misses          456      458    +2     
- Partials        435      443    +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

This is a great start! I have a few comments. Also, can we add some tests for a method like this:

    public static <T, U> void twoTypeVars(T first, U second) {
    }

Maybe write some variants where only one of the type variables has a @Nullable upper bound, and some calls where the wrong type argument is made @Nullable. This is just to check that our indexing logic is matching type arguments to type variables correctly.

@@ -145,6 +148,73 @@ private static boolean[] getTypeParamsWithNullableUpperBound(
return result;
}

public static void checkInstantiationForGenericMethodCalls(
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should add Javadoc on this method


// base type that is being instantiated
Type baseType = methodSymbol.asType();
List<Type> baseTypeArgs = baseType.getTypeArguments();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's rename:

Suggested change
List<Type> baseTypeArgs = baseType.getTypeArguments();
List<Type> baseTypeVariables = baseType.getTypeArguments();

@@ -715,6 +785,40 @@ public static Nullness getGenericReturnNullnessAtInvocation(
MethodInvocationTree tree,
VisitorState state,
Config config) {

Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to update the Javadoc of getGenericReturnNullnessAtInvocation to indicate it also handles generic methods, in addition to instance methods of generic classes (what it handled before)

@@ -715,6 +785,40 @@ public static Nullness getGenericReturnNullnessAtInvocation(
MethodInvocationTree tree,
VisitorState state,
Config config) {

List<? extends Tree> typeArgumentTrees = tree.getTypeArguments();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this new logic only apply to generic methods? If so, can we only run it for that case, by checking if the invoked method has any type variables, !invokedMethodSymbol.getTypeParameters().isEmpty()?

Comment on lines +807 to +815
// If it's not a ForAll type, handle it as a normal MethodType
Type.MethodType methodTypeElse = (Type.MethodType) invokedMethodSymbol.type;
substitutedReturnType =
state
.getTypes()
.subst(
methodTypeElse.restype,
invokedMethodSymbol.type.getTypeArguments(),
explicitTypeArgs);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like if we only apply the new logic to generic methods, we will always get a ForAll type and won't need this else case

@@ -768,6 +884,51 @@ public static Nullness getGenericParameterNullnessAtInvocation(
MethodInvocationTree tree,
VisitorState state,
Config config) {
List<? extends Tree> typeArgumentTrees = tree.getTypeArguments();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as above regarding only running the new logic when a generic method is being invoked

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.

2 participants