-
Notifications
You must be signed in to change notification settings - Fork 300
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: read upper bound annotations from bytecode and add tests #1004
Merged
+318
−13
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a59f6cc
test case
msridhar c79bf76
WIP
msridhar 19ad1d1
WIP
msridhar 0376517
another test
msridhar 8923083
TODO comment
msridhar e6c20e4
another failing test
msridhar 4eb1bcf
field tests
msridhar 2d1573f
comment
msridhar c92518e
more tests
msridhar be0786d
Merge branch 'master' into bytecode-generics
msridhar fa529e2
test rename
msridhar 6f489f7
Merge branch 'master' into bytecode-generics
msridhar 4398294
Merge branch 'master' into bytecode-generics
msridhar f3b52b7
Merge branch 'master' into bytecode-generics
msridhar 106bbfb
fix diagnostic message
msridhar 21754de
Merge branch 'master' into bytecode-generics
msridhar 956f57b
Merge branch 'master' into bytecode-generics
msridhar 75aab9c
Merge branch 'master' into bytecode-generics
msridhar ae94069
WIP
msridhar c6d852c
tweak and comment
msridhar 6ec649b
add issue link
msridhar dd390eb
Merge branch 'master' into bytecode-generics
msridhar 8b61b6a
improve comment
msridhar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
230 changes: 230 additions & 0 deletions
230
nullaway/src/test/java/com/uber/nullaway/jspecify/BytecodeGenericsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
package com.uber.nullaway.jspecify; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import com.uber.nullaway.NullAwayTestsBase; | ||
import java.util.Arrays; | ||
import org.junit.Test; | ||
|
||
public class BytecodeGenericsTests extends NullAwayTestsBase { | ||
|
||
@Test | ||
public void basicTypeParamInstantiation() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.NonNullTypeParam;", | ||
"import com.uber.lib.generics.NullableTypeParam;", | ||
"class Test {", | ||
" // BUG: Diagnostic contains: Generic type parameter", | ||
" static void testBadNonNull(NonNullTypeParam<@Nullable String> t1) {", | ||
" // BUG: Diagnostic contains: Generic type parameter", | ||
" NonNullTypeParam<@Nullable String> t2 = null;", | ||
" NullableTypeParam<@Nullable String> t3 = null;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void multipleTypeParametersInstantiation() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.MixedTypeParam;", | ||
"class Test {", | ||
" static class PartiallyInvalidSubclass", | ||
" // BUG: Diagnostic contains: Generic type parameter", | ||
" extends MixedTypeParam<@Nullable String, String, String, @Nullable String> {}", | ||
" static class ValidSubclass1", | ||
" extends MixedTypeParam<String, @Nullable String, @Nullable String, String> {}", | ||
" static class PartiallyInvalidSubclass2", | ||
" extends MixedTypeParam<", | ||
" String,", | ||
" String,", | ||
" String,", | ||
" // BUG: Diagnostic contains: Generic type parameter", | ||
" @Nullable String> {}", | ||
" static class ValidSubclass2 extends MixedTypeParam<String, String, String, String> {}", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void genericsChecksForAssignments() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.NullableTypeParam;", | ||
"class Test {", | ||
" static void testPositive(NullableTypeParam<@Nullable String> t1) {", | ||
" // BUG: Diagnostic contains: Cannot assign from type NullableTypeParam<@Nullable String>", | ||
" NullableTypeParam<String> t2 = t1;", | ||
" }", | ||
" static void testNegative(NullableTypeParam<@Nullable String> t1) {", | ||
" NullableTypeParam<@Nullable String> t2 = t1;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void genericsChecksForFieldAssignments() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.NullableTypeParam;", | ||
"class Test {", | ||
" static void testPositive(NullableTypeParam<String> t1) {", | ||
" // BUG: Diagnostic contains: Cannot assign from type NullableTypeParam<String>", | ||
" NullableTypeParam.staticField = t1;", | ||
" // BUG: Diagnostic contains: Cannot assign from type NullableTypeParam<@Nullable String>", | ||
" NullableTypeParam<String> t2 = NullableTypeParam.staticField;", | ||
" }", | ||
" static void testNegative(NullableTypeParam<@Nullable String> t1) {", | ||
" NullableTypeParam.staticField = t1;", | ||
" NullableTypeParam<@Nullable String> t2 = NullableTypeParam.staticField;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void genericsChecksForParamPassingAndReturns() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.NullableTypeParam;", | ||
"import com.uber.lib.generics.GenericTypeArgMethods;", | ||
"class Test {", | ||
" static void testPositive(NullableTypeParam<String> t1) {", | ||
" // BUG: Diagnostic contains: Cannot pass parameter of type NullableTypeParam<String>", | ||
" GenericTypeArgMethods.nullableTypeParamArg(t1);", | ||
" // BUG: Diagnostic contains: Cannot assign from type NullableTypeParam<@Nullable String>", | ||
" NullableTypeParam<String> t2 = GenericTypeArgMethods.nullableTypeParamReturn();", | ||
" }", | ||
" static void testNegative(NullableTypeParam<@Nullable String> t1) {", | ||
" GenericTypeArgMethods.nullableTypeParamArg(t1);", | ||
" NullableTypeParam<@Nullable String> t2 = GenericTypeArgMethods.nullableTypeParamReturn();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void overrideParameterType() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.Fn;", | ||
"class Test {", | ||
" static class TestFunc1 implements Fn<@Nullable String, String> {", | ||
" @Override", | ||
" // BUG: Diagnostic contains: parameter s is", | ||
" public String apply(String s) {", | ||
" return s;", | ||
" }", | ||
" }", | ||
" static class TestFunc2 implements Fn<@Nullable String, String> {", | ||
" @Override", | ||
" public String apply(@Nullable String s) {", | ||
" return \"hi\";", | ||
" }", | ||
" }", | ||
" static class TestFunc3 implements Fn<String, String> {", | ||
" @Override", | ||
" public String apply(String s) {", | ||
" return \"hi\";", | ||
" }", | ||
" }", | ||
" static class TestFunc4 implements Fn<String, String> {", | ||
" // this override is legal, we should get no error", | ||
" @Override", | ||
" public String apply(@Nullable String s) {", | ||
" return \"hi\";", | ||
" }", | ||
" }", | ||
" static void useTestFunc(String s) {", | ||
" Fn<@Nullable String, String> f1 = new TestFunc2();", | ||
" // should get no error here", | ||
" f1.apply(null);", | ||
" Fn<String, String> f2 = new TestFunc3();", | ||
" // BUG: Diagnostic contains: passing @Nullable parameter", | ||
" f2.apply(null);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void overrideReturnTypes() { | ||
makeHelper() | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import org.jspecify.annotations.Nullable;", | ||
"import com.uber.lib.generics.Fn;", | ||
"class Test {", | ||
" static class TestFunc1 implements Fn<String, @Nullable String> {", | ||
" @Override", | ||
" public @Nullable String apply(String s) {", | ||
" return s;", | ||
" }", | ||
" }", | ||
" static class TestFunc2 implements Fn<String, @Nullable String> {", | ||
" @Override", | ||
" public String apply(String s) {", | ||
" return s;", | ||
" }", | ||
" }", | ||
" static class TestFunc3 implements Fn<String, String> {", | ||
" @Override", | ||
" // BUG: Diagnostic contains: method returns @Nullable, but superclass", | ||
" public @Nullable String apply(String s) {", | ||
" return s;", | ||
" }", | ||
" }", | ||
" static class TestFunc4 implements Fn<@Nullable String, String> {", | ||
" @Override", | ||
" // BUG: Diagnostic contains: method returns @Nullable, but superclass", | ||
" public @Nullable String apply(String s) {", | ||
" return s;", | ||
" }", | ||
" }", | ||
" static void useTestFunc(String s) {", | ||
" Fn<String, @Nullable String> f1 = new TestFunc1();", | ||
" String t1 = f1.apply(s);", | ||
" // BUG: Diagnostic contains: dereferenced expression", | ||
" t1.hashCode();", | ||
" TestFunc2 f2 = new TestFunc2();", | ||
" String t2 = f2.apply(s);", | ||
" // There should not be an error here", | ||
" t2.hashCode();", | ||
" Fn<String, @Nullable String> f3 = new TestFunc2();", | ||
" String t3 = f3.apply(s);", | ||
" // BUG: Diagnostic contains: dereferenced expression", | ||
" t3.hashCode();", | ||
" // BUG: Diagnostic contains: dereferenced expression", | ||
" f3.apply(s).hashCode();", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
private CompilationTestHelper makeHelper() { | ||
return makeTestHelperWithArgs( | ||
Arrays.asList( | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", "-XepOpt:NullAway:JSpecifyMode=true")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.uber.lib.generics; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
public interface Fn<P extends @Nullable Object, R extends @Nullable Object> { | ||
R apply(P p); | ||
} |
12 changes: 12 additions & 0 deletions
12
test-java-lib/src/main/java/com/uber/lib/generics/GenericTypeArgMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.uber.lib.generics; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
public class GenericTypeArgMethods { | ||
|
||
public static void nullableTypeParamArg(NullableTypeParam<@Nullable String> s) {} | ||
|
||
public static NullableTypeParam<@Nullable String> nullableTypeParamReturn() { | ||
return new NullableTypeParam<@Nullable String>(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test-java-lib/src/main/java/com/uber/lib/generics/MixedTypeParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.uber.lib.generics; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
public class MixedTypeParam<E1, E2 extends @Nullable Object, E3 extends @Nullable Object, E4> {} |
3 changes: 3 additions & 0 deletions
3
test-java-lib/src/main/java/com/uber/lib/generics/NonNullTypeParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.uber.lib.generics; | ||
|
||
public class NonNullTypeParam<E> {} |
9 changes: 9 additions & 0 deletions
9
test-java-lib/src/main/java/com/uber/lib/generics/NullableTypeParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.uber.lib.generics; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
public class NullableTypeParam<E extends @Nullable Object> { | ||
|
||
public static NullableTypeParam<@Nullable String> staticField = | ||
new NullableTypeParam<@Nullable String>(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code below is the new part. Maybe slightly more context here on why it's needed? (I know the difference between getting type info for source vs bytecode, but it won't be obvious to someone looking at this code why this is needed or where to find more info :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed in 8b61b6a