Skip to content

Commit

Permalink
Handle calls to generic constructors in JSpecify mode (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar authored Feb 3, 2025
1 parent 18a0a68 commit f064222
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,21 @@ private static boolean[] getTypeParamsWithNullableUpperBound(
*/
public static void checkGenericMethodCallTypeArguments(
Tree tree, VisitorState state, NullAway analysis, Config config, Handler handler) {
List<? extends Tree> typeArguments = ((MethodInvocationTree) tree).getTypeArguments();
List<? extends Tree> typeArguments;
switch (tree.getKind()) {
case METHOD_INVOCATION:
typeArguments = ((MethodInvocationTree) tree).getTypeArguments();
break;
case NEW_CLASS:
typeArguments = ((NewClassTree) tree).getTypeArguments();
break;
default:
throw new RuntimeException("Unexpected tree kind: " + tree.getKind());
}
if (typeArguments.isEmpty()) {
return;
}
// get Nullable annotated type arguments
MethodInvocationTree methodTree = (MethodInvocationTree) tree;
Map<Integer, Tree> nullableTypeArguments = new HashMap<>();
for (int i = 0; i < typeArguments.size(); i++) {
Tree curTypeArg = typeArguments.get(i);
Expand All @@ -182,7 +191,8 @@ public static void checkGenericMethodCallTypeArguments(
}
}
}
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(methodTree);
Symbol.MethodSymbol methodSymbol =
castToNonNull((Symbol.MethodSymbol) ASTHelpers.getSymbol(tree));

// check if type variables are allowed to be Nullable
Type baseType = methodSymbol.asType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,32 @@ public void issue1035() {
.doTest();
}

@Test
public void issue1138() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.NullMarked;",
"import org.jspecify.annotations.Nullable;",
"@NullMarked",
"class Foo {",
" <T> Foo(T source) {",
" }",
" static <T> Foo createNoTypeArgs(T in) {",
" return new Foo(in);",
" }",
" static Foo createWithTypeArgNegative(String s) {",
" return new <String>Foo(s);",
" }",
" static Foo createWithTypeArgPositive() {",
" // BUG: Diagnostic contains: Type argument cannot be @Nullable, as method <T>Foo(T)'s type variable T is not @Nullable",
" return new <@Nullable String>Foo(null);",
" }",
"}")
.doTest();
}

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

0 comments on commit f064222

Please sign in to comment.