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

JSpecify: fix crash with calls to static methods #856

Merged
4 changes: 2 additions & 2 deletions nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ public static Nullness getGenericReturnNullnessAtInvocation(
MethodInvocationTree tree,
VisitorState state,
Config config) {
if (!(tree.getMethodSelect() instanceof MemberSelectTree)) {
if (!(tree.getMethodSelect() instanceof MemberSelectTree) || invokedMethodSymbol.isStatic()) {
return Nullness.NONNULL;
}
Type methodReceiverType =
Expand Down Expand Up @@ -834,7 +834,7 @@ public static Nullness getGenericParameterNullnessAtInvocation(
MethodInvocationTree tree,
VisitorState state,
Config config) {
if (!(tree.getMethodSelect() instanceof MemberSelectTree)) {
if (!(tree.getMethodSelect() instanceof MemberSelectTree) || invokedMethodSymbol.isStatic()) {
return Nullness.NONNULL;
}
Type enclosingType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,34 @@ public void interactionWithContracts() {
.doTest();
}

@Test
public void testForStaticMethodCallAsAParam() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@akulk022 have you confirmed that this test fails with two unexpected errors if you remove the fixes in GenericsChecks?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I verified it for both scenarios by removing the fix and seeing if the test case fails with the same exception as before.

makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static class A<T> {",
" public static <T> A<T> returnA(){",
" return new A<T>();",
" }",
" public static <T> A<T> returnAWithParam(Object o){",
" return new A<T>();",
" }",
" }",
" static void func(A<Object> a){",
" }",
" static void testNegative() {",
" func(A.returnA());",
" }",
" static void testNegative2() {",
" func(A.returnAWithParam(new Object()));",
" }",
"}")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
Arrays.asList(
Expand Down