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

Generics checks for parameter passing #746

Merged
merged 261 commits into from Mar 20, 2023

Conversation

NikitaAware
Copy link
Contributor

@NikitaAware NikitaAware commented Mar 10, 2023

For method calls, the nullability annotations of the type parameters for formal parameter types and actual parameter types should be exactly the same.
This pull request adds code to compare nullability annotations of actual and formal parameters of generic type and reports an error if the annotations don't match. example:

  static class A<T extends @Nullable Object> { }
  static A<String> sampleMethod(A<A<String>> a1, A<String> a2) {
     return a2;
  }
  static void methodCall(A<A<@Nullable String>> a1, A<String> a2) {

 // **_here the code will report an error as the annotations of formal and actual parameters don't match_**

       A<String> a = sampleMethod(a1, a2);

  }

@@ -44,10 +44,6 @@ public void constructorTypeParamInstantiation() {
" NonNullTypeParam<String> t2 = new NonNullTypeParam<@Nullable String>();",
" // BUG: Diagnostic contains: Generic type parameter",
" testBadNonNull(new NonNullTypeParam<@Nullable String>());",
" testBadNonNull(",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this deleted?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question from me

@msridhar msridhar added the jspecify Related to support for jspecify standard (see jspecify.dev) label Mar 13, 2023
@msridhar
Copy link
Collaborator

@lazaroclapp I think this is ready for you to take a look

Copy link
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

Some minor questions:

@@ -44,10 +44,6 @@ public void constructorTypeParamInstantiation() {
" NonNullTypeParam<String> t2 = new NonNullTypeParam<@Nullable String>();",
" // BUG: Diagnostic contains: Generic type parameter",
" testBadNonNull(new NonNullTypeParam<@Nullable String>());",
" testBadNonNull(",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question from me

" }",
" static void testPositive(A<A<@Nullable String>> a1, A<String> a2) {",
" // BUG: Diagnostic contains: Cannot pass parameter of type",
" A<String> a = sampleMethod(a1, a2);",
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about the other way around? This is invariant, right? So we should also test that we can't pass A<String> to something expecting A<@Nullable String>, correct?

@NikitaAware NikitaAware requested review from lazaroclapp and msridhar and removed request for lazaroclapp March 17, 2023 09:01
@NikitaAware NikitaAware requested review from lazaroclapp and msridhar and removed request for msridhar and lazaroclapp March 17, 2023 09:16
Copy link
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

Very minor comment nit, otherwise looks good to go!

for (int i = 0; i < formalParams.size(); i++) {
int n = formalParams.size();
if (isVarArgs) {
n = n - 1; // don't check for the last argument if
Copy link
Collaborator

Choose a reason for hiding this comment

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

If what? I mean, it's fairly clear, but lets complete the comment. Maybe it should be a comment above like:

// If the last argument is var args, don't check it now, it will be checked against
// all remaining actual arguments in the next loop.

Copy link
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

LGTM! (@msridhar?)

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.

LGTM too! Landing

@msridhar msridhar merged commit a1d1eed into uber:master Mar 20, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 18, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 18, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 19, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 19, 2023
msridhar added a commit to msridhar/NullAway that referenced this pull request Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jspecify Related to support for jspecify standard (see jspecify.dev)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants