From 48772af91a0fa9d6d1dc0dd825beb5cbcedf0e0c Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Wed, 26 Jul 2023 10:20:06 -0700 Subject: [PATCH] JSpecify: avoid crashes when encountering raw types (#792) 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. --- .../com/uber/nullaway/GenericsChecks.java | 7 +++- .../NullAwayJSpecifyGenericsTests.java | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java b/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java index e66430e114..4ca3d3ebd2 100644 --- a/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java @@ -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; } } diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java index 2cac6729cc..5040606433 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java @@ -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 {}", + " static class NullableTypeParam {}", + " static void rawLocals() {", + " NonNullTypeParam t1 = new NonNullTypeParam();", + " NullableTypeParam<@Nullable String> t2 = new NullableTypeParam();", + " NonNullTypeParam t3 = new NonNullTypeParam();", + " 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(