From 2d6122e0edc9d9da563b9d73886acb98526b3e5c Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 08:06:33 -0800 Subject: [PATCH 1/8] add test case --- .../uber/nullaway/jspecify/GenericsTests.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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..5bcd2ec57f 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,30 @@ 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;", + "}", + "class SubClass & Marker> extends Base {", + " void method() {", + " instance.method();", + " }", + "}", + "}") + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs( From 5f0fbbefa875d87a6afb286aefe2b5751d96564b Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 08:17:22 -0800 Subject: [PATCH 2/8] enhance test --- .../test/java/com/uber/nullaway/jspecify/GenericsTests.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 5bcd2ec57f..b95c9ff2ab 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2767,8 +2767,14 @@ public void issue1377() { "}", "class Base {", " T instance;", + " Base(T instance) {", + " this.instance = instance;", + " }", "}", "class SubClass & Marker> extends Base {", + " SubClass(T instance) {", + " super(instance);", + " }", " void method() {", " instance.method();", " }", From 85a9af5846fa4fc5f6c33743106c51e45883097e Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 09:07:34 -0800 Subject: [PATCH 3/8] more tests --- .../uber/nullaway/jspecify/GenericsTests.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) 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 b95c9ff2ab..b0cb1d13cd 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2783,6 +2783,76 @@ public void issue1377() { .doTest(); } + @Test + public void typeVarReceiverAsSuper() { + makeHelper() + .addSourceLines( + "Test.java", + "import org.jspecify.annotations.NullMarked;", + "import org.jspecify.annotations.Nullable;", + "@NullMarked", + "class Test {", + " interface Marker {}", + " interface Generic {", + " T get();", + " }", + " static final class Impl implements Generic<@Nullable String>, Marker {", + " @Override", + " public @Nullable String get() {", + " return null;", + " }", + " }", + " static final class Holder> {", + " final T instance;", + " Holder(T instance) {", + " this.instance = instance;", + " }", + " void test() {", + " // BUG: Diagnostic contains: dereferenced expression instance.get() is @Nullable", + " instance.get().length();", + " }", + " }", + " static void run(Holder holder) {", + " holder.test();", + " }", + "}") + .doTest(); + } + + @Test + public void typeVarReceiverArgumentNullabilityMismatch() { + makeHelper() + .addSourceLines( + "Test.java", + "import org.jspecify.annotations.NullMarked;", + "import org.jspecify.annotations.Nullable;", + "@NullMarked", + "class Test {", + " static final class Foo {}", + " interface Generic {", + " void take(Foo arg);", + " }", + " static final class Impl implements Generic {", + " @Override", + " public void take(Foo arg) {}", + " }", + " static final class Holder> {", + " final T instance;", + " Holder(T instance) {", + " this.instance = instance;", + " }", + " void test(Foo<@Nullable String> badArg) {", + " // BUG: Diagnostic contains: incompatible types: Foo<@Nullable String> cannot be converted to Foo", + " instance.take(badArg);", + " }", + " }", + " static void run(Holder holder, Foo<@Nullable String> badArg) {", + " holder.test(badArg);", + " }", + "}") + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs( From 5b2b660ae425318a38c8827fba8890e8bc6878c8 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 09:33:26 -0800 Subject: [PATCH 4/8] WIP --- .../generics/TypeSubstitutionUtils.java | 14 ++++-- .../uber/nullaway/jspecify/GenericsTests.java | 45 ++++--------------- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java index 04f6b0d4c2..b3c0fddf7e 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java @@ -63,11 +63,19 @@ public class TypeSubstitutionUtils { */ public static Type memberType(Types types, Type t, Symbol sym, Config config) { Type origType = sym.type; - Type memberType = types.memberType(t, sym); + Type receiverType = t; + if (t instanceof Type.TypeVar) { + Type superType = asSuper(types, t, (Symbol.ClassSymbol) sym.owner, config); + if (superType == null) { + return origType; + } + receiverType = superType; + } + Type memberType = types.memberType(receiverType, sym); Map annotsOnTypeVarsFromSubtypes = - t instanceof DeclaredType + receiverType instanceof DeclaredType ? getAnnotsOnTypeVarsFromSubtypes( - (DeclaredType) t, (Symbol.MethodSymbol) sym, types, config) + (DeclaredType) receiverType, (Symbol.MethodSymbol) sym, types, config) : Map.of(); return restoreExplicitNullabilityAnnotations( origType, memberType, config, annotsOnTypeVarsFromSubtypes); 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 b0cb1d13cd..fc263675d9 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2802,10 +2802,15 @@ public void typeVarReceiverAsSuper() { " return null;", " }", " }", - " static final class Holder> {", - " final T instance;", + "static class Base {", + " T instance;", + " Base(T instance) {", + " this.instance = instance;", + " }", + "}", + " static final class Holder> extends Base {", " Holder(T instance) {", - " this.instance = instance;", + " super(instance);", " }", " void test() {", " // BUG: Diagnostic contains: dereferenced expression instance.get() is @Nullable", @@ -2819,40 +2824,6 @@ public void typeVarReceiverAsSuper() { .doTest(); } - @Test - public void typeVarReceiverArgumentNullabilityMismatch() { - makeHelper() - .addSourceLines( - "Test.java", - "import org.jspecify.annotations.NullMarked;", - "import org.jspecify.annotations.Nullable;", - "@NullMarked", - "class Test {", - " static final class Foo {}", - " interface Generic {", - " void take(Foo arg);", - " }", - " static final class Impl implements Generic {", - " @Override", - " public void take(Foo arg) {}", - " }", - " static final class Holder> {", - " final T instance;", - " Holder(T instance) {", - " this.instance = instance;", - " }", - " void test(Foo<@Nullable String> badArg) {", - " // BUG: Diagnostic contains: incompatible types: Foo<@Nullable String> cannot be converted to Foo", - " instance.take(badArg);", - " }", - " }", - " static void run(Holder holder, Foo<@Nullable String> badArg) {", - " holder.test(badArg);", - " }", - "}") - .doTest(); - } - private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs( From b4ccc4c5d94aae6a14b8db06e1b8684759185490 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 09:48:53 -0800 Subject: [PATCH 5/8] a better fix --- .../nullaway/generics/GenericsChecks.java | 6 ++- .../generics/TypeSubstitutionUtils.java | 14 +++---- .../uber/nullaway/jspecify/GenericsTests.java | 41 ------------------- 3 files changed, 12 insertions(+), 49 deletions(-) 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..76480c97a8 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,11 @@ private void reportInvalidOverridingMethodParamTypeError( } } } - result = symbol.type; + result = ASTHelpers.getType(tree); + // if result is an array type, use the type on the symbol instead + if (result != null && result.getKind() == TypeKind.ARRAY) { + 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/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java index b3c0fddf7e..bec25832dd 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java @@ -64,13 +64,13 @@ public class TypeSubstitutionUtils { public static Type memberType(Types types, Type t, Symbol sym, Config config) { Type origType = sym.type; Type receiverType = t; - if (t instanceof Type.TypeVar) { - Type superType = asSuper(types, t, (Symbol.ClassSymbol) sym.owner, config); - if (superType == null) { - return origType; - } - receiverType = superType; - } + // if (t instanceof Type.TypeVar) { + // Type superType = asSuper(types, t, (Symbol.ClassSymbol) sym.owner, config); + // if (superType == null) { + // return origType; + // } + // receiverType = superType; + // } Type memberType = types.memberType(receiverType, sym); Map annotsOnTypeVarsFromSubtypes = receiverType instanceof DeclaredType 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 fc263675d9..b95c9ff2ab 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2783,47 +2783,6 @@ public void issue1377() { .doTest(); } - @Test - public void typeVarReceiverAsSuper() { - makeHelper() - .addSourceLines( - "Test.java", - "import org.jspecify.annotations.NullMarked;", - "import org.jspecify.annotations.Nullable;", - "@NullMarked", - "class Test {", - " interface Marker {}", - " interface Generic {", - " T get();", - " }", - " static final class Impl implements Generic<@Nullable String>, Marker {", - " @Override", - " public @Nullable String get() {", - " return null;", - " }", - " }", - "static class Base {", - " T instance;", - " Base(T instance) {", - " this.instance = instance;", - " }", - "}", - " static final class Holder> extends Base {", - " Holder(T instance) {", - " super(instance);", - " }", - " void test() {", - " // BUG: Diagnostic contains: dereferenced expression instance.get() is @Nullable", - " instance.get().length();", - " }", - " }", - " static void run(Holder holder) {", - " holder.test();", - " }", - "}") - .doTest(); - } - private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( JSpecifyJavacConfig.withJSpecifyModeArgs( From 133a34a368a4aa4eaea408a7f292ca7d4f2fba79 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 09:50:50 -0800 Subject: [PATCH 6/8] comment --- .../com/uber/nullaway/generics/GenericsChecks.java | 3 ++- .../nullaway/generics/TypeSubstitutionUtils.java | 14 +++----------- 2 files changed, 5 insertions(+), 12 deletions(-) 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 76480c97a8..228f06c697 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -460,8 +460,9 @@ private void reportInvalidOverridingMethodParamTypeError( } } result = ASTHelpers.getType(tree); - // if result is an array type, use the type on the symbol instead if (result != null && result.getKind() == TypeKind.ARRAY) { + // type on the tree itself can be missing nested annotations for arrays; get the type from + // the symbol for the variable instead result = symbol.type; } } else if (tree instanceof AssignmentTree) { diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java index bec25832dd..04f6b0d4c2 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java @@ -63,19 +63,11 @@ public class TypeSubstitutionUtils { */ public static Type memberType(Types types, Type t, Symbol sym, Config config) { Type origType = sym.type; - Type receiverType = t; - // if (t instanceof Type.TypeVar) { - // Type superType = asSuper(types, t, (Symbol.ClassSymbol) sym.owner, config); - // if (superType == null) { - // return origType; - // } - // receiverType = superType; - // } - Type memberType = types.memberType(receiverType, sym); + Type memberType = types.memberType(t, sym); Map annotsOnTypeVarsFromSubtypes = - receiverType instanceof DeclaredType + t instanceof DeclaredType ? getAnnotsOnTypeVarsFromSubtypes( - (DeclaredType) receiverType, (Symbol.MethodSymbol) sym, types, config) + (DeclaredType) t, (Symbol.MethodSymbol) sym, types, config) : Map.of(); return restoreExplicitNullabilityAnnotations( origType, memberType, config, annotsOnTypeVarsFromSubtypes); From 17f215db7e6023d464e0e60f6d66a48ba1389ea7 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 10:34:27 -0800 Subject: [PATCH 7/8] improve --- .../nullaway/generics/GenericsChecks.java | 2 +- .../uber/nullaway/jspecify/GenericsTests.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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 228f06c697..5bac8fa7d5 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -460,7 +460,7 @@ private void reportInvalidOverridingMethodParamTypeError( } } result = ASTHelpers.getType(tree); - if (result != null && result.getKind() == TypeKind.ARRAY) { + if (result != null && !(result instanceof Type.TypeVar)) { // type on the tree itself can be missing nested annotations for arrays; get the type from // the symbol for the variable instead result = symbol.type; 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 b95c9ff2ab..66daf8d5f4 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2783,6 +2783,28 @@ public void issue1377() { .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( From 0f7947fada8d96e7d267ef07104673954154ed55 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Tue, 16 Dec 2025 10:55:30 -0800 Subject: [PATCH 8/8] improve comment --- .../java/com/uber/nullaway/generics/GenericsChecks.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 5bac8fa7d5..2ae38b804d 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -460,9 +460,11 @@ private void reportInvalidOverridingMethodParamTypeError( } } 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)) { - // type on the tree itself can be missing nested annotations for arrays; get the type from - // the symbol for the variable instead result = symbol.type; } } else if (tree instanceof AssignmentTree) {