From 2718d42ae44cb861f4ea37252ea5951cd6dbc393 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sun, 8 Dec 2024 17:33:32 -0800 Subject: [PATCH 1/4] 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 74a7ef481f..bbf4140a4d 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2002,6 +2002,30 @@ public void issue1019() { .doTest(); } + @Test + public void issue1082() { + makeHelper() + .addSourceLines( + "Main.java", + "package com.uber;", + "import java.util.Optional;", + "public class Main {", + " public interface Factory {", + " T create();", + " }", + " public interface Expiry {}", + " static class Config {", + " Config setFactory(Optional>> factory) {", + " return null;", + " }", + " }", + " static void caller(Config config) {", + " config.setFactory(Optional.empty());", + " }", + "}") + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( Arrays.asList( From 70a4453054c4e10d92bb3fa4f2c80e2728b085c8 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sun, 8 Dec 2024 18:12:27 -0800 Subject: [PATCH 2/4] test case --- .../nullaway/generics/CheckIdenticalNullabilityVisitor.java | 6 +++--- .../test/java/com/uber/nullaway/jspecify/GenericsTests.java | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java b/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java index 5f1bac2742..b0bf30b4ba 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java @@ -35,10 +35,10 @@ public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) { // The base type of rhsType may be a subtype of lhsType's base type. In such cases, we must // compare lhsType against the supertype of rhsType with a matching base type. Type rhsTypeAsSuper = types.asSuper(rhsType, lhsType.tsym); - // This is impossible, considering the fact that standard Java subtyping succeeds before - // running NullAway if (rhsTypeAsSuper == null) { - throw new RuntimeException("Did not find supertype of " + rhsType + " matching " + lhsType); + // Surprisingly, this can in fact occur, in cases involving raw types. See, e.g., + // GenericsTests#issue1082. Bail out. + return true; } // bail out of checking raw types for now if (rhsTypeAsSuper.isRaw()) { 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 bbf4140a4d..7acec2c1da 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2016,11 +2016,13 @@ public void issue1082() { " public interface Expiry {}", " static class Config {", " Config setFactory(Optional>> factory) {", + " // BUG: Diagnostic contains: returning @Nullable expression from method", " return null;", " }", " }", " static void caller(Config config) {", - " config.setFactory(Optional.empty());", + " // checking that we don't crash", + " config.setFactory(Optional.empty());", " }", "}") .doTest(); From 524b832a3ac7041eeed41b406326c22e940bd01c Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sun, 8 Dec 2024 18:15:06 -0800 Subject: [PATCH 3/4] add PR link --- .../nullaway/generics/CheckIdenticalNullabilityVisitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java b/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java index b0bf30b4ba..d567c2e028 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java @@ -37,7 +37,7 @@ public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) { Type rhsTypeAsSuper = types.asSuper(rhsType, lhsType.tsym); if (rhsTypeAsSuper == null) { // Surprisingly, this can in fact occur, in cases involving raw types. See, e.g., - // GenericsTests#issue1082. Bail out. + // GenericsTests#issue1082 and https://github.com/uber/NullAway/pull/1086. Bail out. return true; } // bail out of checking raw types for now From 82b188de1ceff213c230732ab28b8e25019b1da6 Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sun, 8 Dec 2024 18:55:26 -0800 Subject: [PATCH 4/4] improve test --- .../test/java/com/uber/nullaway/jspecify/GenericsTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 7acec2c1da..946b6963c4 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java @@ -2016,8 +2016,7 @@ public void issue1082() { " public interface Expiry {}", " static class Config {", " Config setFactory(Optional>> factory) {", - " // BUG: Diagnostic contains: returning @Nullable expression from method", - " return null;", + " return this;", " }", " }", " static void caller(Config config) {",