Skip to content

Commit

Permalink
JSpecify: avoid crashes when encountering raw types (#792)
Browse files Browse the repository at this point in the history
We check for the presence of a raw type and (for now) bail out of any
checking. Adding further support for JSpecify checking of raw types is a
low priority (not even sure anything needs to be done).

This fixes the initial crash from #791, though we may want to leave that
issue open for now until we split off issues for the other crashes as
needed.
  • Loading branch information
msridhar authored Jul 26, 2023
1 parent 9ece8b3 commit 48772af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ private Type getTreeType(Tree tree) {
}
return typeWithPreservedAnnotations(paramTypedTree);
} else {
return ASTHelpers.getType(tree);
Type result = ASTHelpers.getType(tree);
if (result != null && result.isRaw()) {
// bail out of any checking involving raw types for now
return null;
}
return result;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,41 @@ public void varargsParameter() {
.doTest();
}

/**
* Currently this test is solely to ensure NullAway does not crash in the presence of raw types.
* Further study of the JSpecify documents is needed to determine whether any errors should be
* reported for these cases.
*/
@Test
public void rawTypes() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static class NonNullTypeParam<E> {}",
" static class NullableTypeParam<E extends @Nullable Object> {}",
" static void rawLocals() {",
" NonNullTypeParam<String> t1 = new NonNullTypeParam();",
" NullableTypeParam<@Nullable String> t2 = new NullableTypeParam();",
" NonNullTypeParam t3 = new NonNullTypeParam<String>();",
" NullableTypeParam t4 = new NullableTypeParam<@Nullable String>();",
" NonNullTypeParam t5 = new NonNullTypeParam();",
" NullableTypeParam t6 = new NullableTypeParam();",
" }",
" static void rawConditionalExpression(boolean b, NullableTypeParam<@Nullable String> p) {",
" NullableTypeParam<@Nullable String> t = b ? new NullableTypeParam() : p;",
" }",
" static void doNothing(NullableTypeParam<@Nullable String> p) { }",
" static void rawParameterPassing() { doNothing(new NullableTypeParam()); }",
" static NullableTypeParam<@Nullable String> rawReturn() {",
" return new NullableTypeParam();",
"}",
"}")
.doTest();
}

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

0 comments on commit 48772af

Please sign in to comment.