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: avoid crashes when encountering raw types #792

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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