diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java index efe1504744..2ae38b804d 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -459,7 +459,14 @@ private void reportInvalidOverridingMethodParamTypeError( } } } - result = symbol.type; + result = ASTHelpers.getType(tree); + // type on the tree itself can be missing nested annotations in certain cases, so use the + // type on the symbol instead. for type variables, we've found that the type on the symbol + // can be wrong (which caused https://github.com/uber/NullAway/issues/1377), so we still use + // the tree type for type variables + if (result != null && !(result instanceof Type.TypeVar)) { + result = symbol.type; + } } else if (tree instanceof AssignmentTree) { // type on the tree itself can be missing nested annotations for arrays; get the type from // the symbol for the assigned location instead, if available diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java index ca4c5eb095..66daf8d5f4 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2753,6 +2753,58 @@ public void nonNullInFieldTypeArg() { .doTest(); } + @Test + public void issue1377() { + makeHelper() + .addSourceLines( + "Test.java", + "import org.jspecify.annotations.NullMarked;", + "@NullMarked", + "class Test {", + "interface Marker {}", + "class Generic {", + " public void method() {}", + "}", + "class Base {", + " T instance;", + " Base(T instance) {", + " this.instance = instance;", + " }", + "}", + "class SubClass & Marker> extends Base {", + " SubClass(T instance) {", + " super(instance);", + " }", + " void method() {", + " instance.method();", + " }", + "}", + "}") + .doTest(); + } + + @Test + public void annotationsOnTypeVariableTypeArgs() { + makeHelper() + .addSourceLines( + "Test.java", + "import org.jspecify.annotations.*;", + "import java.util.function.Function;", + "@NullMarked", + "class Test {", + " static final class Foo {", + " final Function<@Nullable T, @Nullable T> func;", + " Foo(Function<@Nullable T, @Nullable T> func) {", + " this.func = func;", + " }", + " @Nullable T test(@Nullable T t) {", + " return func.apply(t);", + " }", + " }", + "}") + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs(