Skip to content
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: initial checks for generic type compatibility at assignments #715

Merged
merged 204 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
204 commits
Select commit Hold shift + click to select a range
f306db2
an initial test case
msridhar Sep 13, 2022
16654f7
sample code
msridhar Sep 27, 2022
7f9418b
add-generics-checks
NikitaAware Sep 29, 2022
441ab7c
first-case-of-generics-support
NikitaAware Sep 30, 2022
4a9c86d
first-case-of-generics-support
NikitaAware Sep 30, 2022
01377b6
restore bad deletion
msridhar Sep 30, 2022
359dcc8
add-generics-spport-task-1-updates
NikitaAware Sep 30, 2022
731bf3d
add-generics-spport-task-1-updates
NikitaAware Sep 30, 2022
b9b8239
add-generics-spport-task-1-updates
NikitaAware Sep 30, 2022
fb1beb9
add generics check for variables
NikitaAware Oct 2, 2022
1a23f8e
Merge branch 'master' into add-generics-checks
msridhar Oct 3, 2022
90d471d
tweak error message
msridhar Oct 3, 2022
98f7fa6
more tests
msridhar Oct 3, 2022
53ddad7
code cleanup.remove print statements
NikitaAware Oct 3, 2022
7daa52b
Add temporary check to consider unannoted base type
NikitaAware Oct 3, 2022
cd01ff9
minor code changes
NikitaAware Oct 4, 2022
202d5db
add generics check for new class - check if the class is generic
NikitaAware Oct 6, 2022
3998949
add generics check for new class - check for the type arguments
NikitaAware Oct 7, 2022
fc8b99f
generics check for new class
NikitaAware Oct 10, 2022
6425909
generics check for extended class
NikitaAware Oct 10, 2022
bae84dc
generics check for new class
NikitaAware Oct 10, 2022
4aba42c
generics check for new class - minor changes for error message
NikitaAware Oct 10, 2022
bca4f24
generics check for interface implementation
NikitaAware Oct 10, 2022
99e8bf9
minor identation changes
NikitaAware Oct 10, 2022
a0caac8
nullability checks for build failures
NikitaAware Oct 11, 2022
5b9d918
Add Test: generics check for subclasses
NikitaAware Oct 11, 2022
4cb51d9
restructure generics tests
NikitaAware Oct 11, 2022
2e4a2ef
restructure generics tests
NikitaAware Oct 11, 2022
b1668e9
Add tests
NikitaAware Oct 11, 2022
9c8b3ae
restructure code
NikitaAware Oct 11, 2022
97e088b
restructure code
NikitaAware Oct 11, 2022
036febf
restructure code
NikitaAware Oct 11, 2022
f065bd9
Merge branch 'master' into add-generics-checks
msridhar Oct 11, 2022
00ad702
add jspecify mode flag
msridhar Oct 11, 2022
31a0e4d
check jspecify flag
msridhar Oct 11, 2022
fe66b4a
add test case for nested type parameter instantiation
msridhar Oct 11, 2022
89ca225
nested type
NikitaAware Oct 13, 2022
8ba736b
nested generics type check for new class
NikitaAware Oct 13, 2022
fa627be
Add test - generics check for function return type
NikitaAware Oct 14, 2022
5745cf7
Add - generics check for function return type
NikitaAware Oct 14, 2022
1518629
code cleaning - generics check
NikitaAware Oct 18, 2022
b25ba39
code cleaning - generics check
NikitaAware Oct 18, 2022
bd96079
Merge branch 'master' into add-generics-checks
msridhar Oct 19, 2022
975dccd
Suggested changes
NikitaAware Oct 23, 2022
6d22b00
Suggested changes
NikitaAware Oct 23, 2022
2ddbea9
Suggested changes
NikitaAware Oct 25, 2022
b9cbbff
working changes
NikitaAware Oct 25, 2022
2002aec
working changes
NikitaAware Oct 25, 2022
5c11fbe
suggested changes
NikitaAware Oct 25, 2022
736a613
suggested changes
NikitaAware Oct 27, 2022
80ba7a5
suggested changes
NikitaAware Oct 27, 2022
e1864ee
suggested changes
NikitaAware Oct 27, 2022
8d5e193
extend test
msridhar Oct 27, 2022
91aab18
@Nullable annotation check for Paramterized typed tree
NikitaAware Oct 27, 2022
799f5dc
@Nullable annotation check for Paramterized typed tree
NikitaAware Oct 27, 2022
f042e60
added updates
NikitaAware Nov 1, 2022
852e4b1
added updates
NikitaAware Nov 1, 2022
8b484c2
extra test cases for other annotations
NikitaAware Nov 1, 2022
1727458
extra test cases for other annotations
NikitaAware Nov 1, 2022
7ecc2c4
Merge branch 'master' into add-generics-checks
NikitaAware Nov 1, 2022
f0a0b62
some comments and a todo
msridhar Nov 3, 2022
bc3c0be
reuse code
msridhar Nov 6, 2022
832f733
minor cleanup
msridhar Nov 6, 2022
0b5268c
fix
msridhar Nov 6, 2022
66a1068
Suggested changes
NikitaAware Nov 6, 2022
1bc1ea6
Suggested changes
NikitaAware Nov 6, 2022
cb1690a
Suggested changes
NikitaAware Nov 6, 2022
c523f2d
Javadoc tweaks
msridhar Nov 7, 2022
8cf3819
add private constructor
msridhar Nov 7, 2022
27d5488
suggested changes
NikitaAware Nov 10, 2022
263edac
suggested changes
NikitaAware Nov 10, 2022
2fa8569
suggested changes
NikitaAware Nov 10, 2022
f40a0da
suggested changes
NikitaAware Nov 10, 2022
8308cf6
code cleanup
msridhar Nov 10, 2022
af6404a
tests for error positions
msridhar Nov 10, 2022
ad67e01
Clean up by implementing ParameterizedTypeTreeMatcher
msridhar Nov 11, 2022
67994da
remove extra blank line
msridhar Nov 11, 2022
2631d63
add test
msridhar Nov 11, 2022
36a5c60
suggested changes
NikitaAware Nov 16, 2022
c8ebfe6
suggested changes
NikitaAware Nov 16, 2022
938c595
Merge branch 'master' into add-generics-checks
msridhar Nov 16, 2022
3fb6856
tweak error message
msridhar Nov 16, 2022
ee57c56
clarify test name and purpose
msridhar Nov 16, 2022
7349744
Assignment interface added
NikitaAware Nov 16, 2022
b5eb8a0
structure for assignments checks
NikitaAware Nov 16, 2022
eecc4c3
Assignment changes
NikitaAware Nov 18, 2022
063c580
supertype changes
NikitaAware Dec 7, 2022
696aa90
Merge branch 'master' into assigments1
NikitaAware Dec 9, 2022
823da48
multiple interface implementation
NikitaAware Dec 9, 2022
e26e088
multilevel inheritance
NikitaAware Dec 9, 2022
f7009ae
changes
NikitaAware Dec 9, 2022
3b2cdf1
changes
NikitaAware Dec 9, 2022
d957f56
nested checks
NikitaAware Dec 9, 2022
2d061fc
working changes
NikitaAware Dec 9, 2022
80f296e
working changes
NikitaAware Dec 9, 2022
c33fe3f
Working changes
NikitaAware Dec 9, 2022
31a3dca
updates
NikitaAware Dec 10, 2022
3eca5a3
failing 3 tests
NikitaAware Dec 15, 2022
3435f89
working tests
NikitaAware Dec 16, 2022
7972c05
npe for variable initialization changes
NikitaAware Dec 20, 2022
4924722
npe for variable initialization changes
NikitaAware Dec 20, 2022
35e2bc7
All tests pass including the existing tests
NikitaAware Dec 20, 2022
a59d223
Merge branch 'master' into assigments1
msridhar Dec 21, 2022
25b840b
Merge branch 'master' into assigments1
msridhar Dec 21, 2022
c8456f3
some cleanup
msridhar Dec 21, 2022
945df87
enable check for multiple top-level classes
msridhar Dec 21, 2022
a3fa869
suggested changes - enclosing class member fields
NikitaAware Jan 3, 2023
601f2b5
code cleanup
NikitaAware Jan 3, 2023
a0aacdb
suggested changes
NikitaAware Jan 3, 2023
7b39522
Merge branch 'master' into assigments1
msridhar Jan 4, 2023
84140d0
some suggested changes
NikitaAware Jan 4, 2023
20fae01
suggested changes with working normal typed tree
NikitaAware Jan 4, 2023
c517b4b
Changes with parameterized typed tree without the error generation
NikitaAware Jan 4, 2023
def7ce2
suggested changes with two test cases failing
NikitaAware Jan 4, 2023
75f36df
suggested changes with all passing tests
NikitaAware Jan 4, 2023
0125647
All working changes
NikitaAware Jan 4, 2023
8fc5d18
Continuous integration working
NikitaAware Jan 4, 2023
090ac0c
Continuous integration working
NikitaAware Jan 4, 2023
ba6b229
changes
NikitaAware Jan 4, 2023
bd6700f
minor changes
NikitaAware Jan 4, 2023
fd0b096
fix naming case
msridhar Jan 4, 2023
d5f34f2
failing test
msridhar Jan 4, 2023
01c5503
refactoring and comments
msridhar Jan 4, 2023
25599dd
add a case
msridhar Jan 4, 2023
bf80848
working code with class type
NikitaAware Jan 6, 2023
8ca41d0
minor refactoring
NikitaAware Jan 6, 2023
4a87b6c
minor changes
NikitaAware Jan 6, 2023
44bea9b
code cleaning
NikitaAware Jan 6, 2023
b9a2ed4
code cleaning
NikitaAware Jan 6, 2023
ac7f33a
fancy test works!
msridhar Jan 7, 2023
851fbfa
Merge branch 'master' into assigments1
msridhar Jan 7, 2023
b1119ab
only run check in JSpecify mode
msridhar Jan 7, 2023
92989ec
Fix NullAway errors
msridhar Jan 7, 2023
334d1e8
modified assignment message
NikitaAware Jan 7, 2023
79b474b
remove unneeded suppression
msridhar Jan 7, 2023
05292c0
remove change to build.gradle
msridhar Jan 7, 2023
d9e2689
remove local used only once
msridhar Jan 7, 2023
1cc72f2
cleanup supertypeMatchingLHS
msridhar Jan 7, 2023
85741be
restore private
msridhar Jan 7, 2023
c58d819
add private
msridhar Jan 7, 2023
8829bcd
changed the error message
NikitaAware Jan 8, 2023
fd16446
updated the error message text
NikitaAware Jan 8, 2023
c0fd28e
Type.ClassType suggestion changes
NikitaAware Jan 8, 2023
abe3ed9
changes
NikitaAware Jan 8, 2023
7a18462
changes
NikitaAware Jan 8, 2023
23c8fbb
changes
NikitaAware Jan 8, 2023
88accab
extra test case
NikitaAware Jan 8, 2023
124e7c8
add failing test
msridhar Jan 8, 2023
00802a1
updated logic
NikitaAware Jan 8, 2023
021435a
minor updates
NikitaAware Jan 8, 2023
a12d549
minor code cleaning
NikitaAware Jan 8, 2023
82d9e8d
minor code cleaning
NikitaAware Jan 8, 2023
f381d83
suggested changes
NikitaAware Jan 9, 2023
75234b8
suggested changes
NikitaAware Jan 9, 2023
1be7c40
replaced super type matching lhs with asSuper method
NikitaAware Jan 9, 2023
676e07d
updated assignment checks
NikitaAware Jan 10, 2023
a085443
changes
NikitaAware Jan 10, 2023
c8303d7
changes
NikitaAware Jan 10, 2023
4238a9a
suggested changes
NikitaAware Jan 10, 2023
f5ec4cd
suggested changes
NikitaAware Jan 10, 2023
257c42c
suggested changes
NikitaAware Jan 10, 2023
a9e4890
suggested changes
NikitaAware Jan 10, 2023
f3fd30d
suggested changes
NikitaAware Jan 10, 2023
c2f66e2
Merge branch 'master' into assigments1
msridhar Jan 11, 2023
aeddd14
fix nullaway error
msridhar Jan 11, 2023
c67db12
Merge branch 'master' into assigments1
msridhar Jan 11, 2023
e244e55
fixes
msridhar Jan 11, 2023
53f8546
more cleanup and javadoc
msridhar Jan 11, 2023
0fe788d
Merge branch 'master' into assigments1
msridhar Jan 14, 2023
3fb81c9
Merge branch 'master' into assigments1
msridhar Jan 20, 2023
0bfe208
Merge branch 'master' into assigments1
msridhar Jan 22, 2023
d6bc810
suggested changes in test nestedChecksForAssignmentsMultipleArguments
NikitaAware Jan 23, 2023
3127339
suggested changes in test - superTypeAssignmentChecksSingleInterface
NikitaAware Jan 23, 2023
4aacb5a
suggested indentation changes in test - superTypeAssignmentChecksMult…
NikitaAware Jan 23, 2023
f3e6b45
suggested changes
NikitaAware Jan 23, 2023
38f2ef6
suggested changes
NikitaAware Jan 23, 2023
5245c4d
suggested test for the lambda expressions
NikitaAware Jan 23, 2023
0344360
Java doc for type with preserved annotations
NikitaAware Jan 23, 2023
00cad6f
comments
NikitaAware Jan 23, 2023
d09c190
code cleaning for the tests
NikitaAware Jan 23, 2023
8bb552c
code cleaning for the tests
NikitaAware Jan 23, 2023
0cff82f
changes
NikitaAware Jan 23, 2023
9906f25
suggested changes
NikitaAware Jan 23, 2023
f6dcc69
negative tests
NikitaAware Jan 23, 2023
4323d48
fix formatting in tests
msridhar Jan 23, 2023
5ec8fa1
test cases for diamond operator and the lambdas
NikitaAware Jan 24, 2023
7c07d98
test cases for diamond operator and the lambdas
NikitaAware Jan 24, 2023
6755d66
private fields
NikitaAware Jan 25, 2023
41f45b1
check only for jspecify nullable annotation
NikitaAware Jan 25, 2023
322c3c2
move test method to more appropriate place
msridhar Jan 25, 2023
4a46a46
tweak method reference test
msridhar Jan 25, 2023
4a1ce05
re-add blank line
msridhar Jan 25, 2023
fce345b
clarify method ref test
msridhar Jan 25, 2023
02bbd9f
tweak lambda and diamond operator tests
msridhar Jan 25, 2023
514829d
make test Java 8 compatible
msridhar Jan 26, 2023
2270d8d
Merge branch 'master' into assigments1
msridhar Jan 31, 2023
924883b
Merge branch 'master' into assigments1
msridhar Feb 1, 2023
c070c22
Merge branch 'master' into assigments1
msridhar Feb 6, 2023
e37aa61
Merge branch 'master' into assigments1
msridhar Feb 6, 2023
7412439
suggested changes
NikitaAware Feb 7, 2023
228cf0f
suggested changes
NikitaAware Feb 7, 2023
b7bbf7c
suggested changes
NikitaAware Feb 7, 2023
edb4f86
suggested changes
NikitaAware Feb 7, 2023
18f3134
Merge branch 'master' into assigments1
lazaroclapp Feb 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public final class GenericsChecks {

private static final Supplier<Type> NULLABLE_TYPE_SUPPLIER =
Suppliers.typeFromString(NULLABLE_NAME);
VisitorState state;
Config config;
NullAway analysis;
private VisitorState state;
private Config config;
private NullAway analysis;

public GenericsChecks(VisitorState state, Config config, NullAway analysis) {
this.state = state;
Expand Down Expand Up @@ -172,11 +172,9 @@ public void checkTypeParameterNullnessForAssignability(Tree tree) {
// no explicit type parameters
return;
}
rhsType =
typeWithPreservedAnnotations(
(ParameterizedTypeTree) ((NewClassTree) rhsTree).getIdentifier());
rhsType = typeWithPreservedAnnotations(paramTypedTree);
}
if (lhsType != null && rhsType != null) {
if (lhsType instanceof Type.ClassType && rhsType instanceof Type.ClassType) {
compareNullabilityAnnotations((Type.ClassType) lhsType, (Type.ClassType) rhsType, tree);
msridhar marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand All @@ -199,26 +197,40 @@ private void compareNullabilityAnnotations(
// 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.
rhsType = (Type.ClassType) types.asSuper(rhsType, lhsType.tsym);
// This is impossible, considering the fact that standard Java subtyping succeeds before running
// NullAway
if (rhsType == null) {
throw new RuntimeException("did not find supertype of " + rhsType + " matching " + lhsType);
throw new RuntimeException("Did not find supertype of " + rhsType + " matching " + lhsType);
}
List<Type> lhsTypeArguments = lhsType.getTypeArguments();
List<Type> rhsTypeArguments = rhsType.getTypeArguments();
// This is impossible, considering the fact that standard Java subtyping succeeds before running
// NullAway
if (lhsTypeArguments.size() != rhsTypeArguments.size()) {
throw new RuntimeException(
"number of types arguments in " + rhsType + " does not match " + lhsType);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also capitalize the start of the error message and again, if we are assuming the compiler will take care of this, a comment saying so is worth it. I think we are betting on this line being unreachable but still being cautious (which is good), right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Will update this as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lazaroclapp did you mean this in terms of capitalization?

Suggested change
"number of types arguments in " + rhsType + " does not match " + lhsType);
"Number of type arguments in " + rhsType + " does not match " + lhsType);

NikitaAware marked this conversation as resolved.
Show resolved Hide resolved
}
for (int i = 0; i < lhsTypeArguments.size(); i++) {
Type lhsTypeArgument = lhsTypeArguments.get(i);
Type rhsTypeArgument = rhsTypeArguments.get(i);
com.sun.tools.javac.util.List<Attribute.TypeCompound> annotationMirrorsLHS =
lhsTypeArgument.getAnnotationMirrors();
com.sun.tools.javac.util.List<Attribute.TypeCompound> annotationMirrorsRHS =
rhsTypeArgument.getAnnotationMirrors();
boolean isLHSNullableAnnotated =
Nullness.hasNullableAnnotation(annotationMirrorsLHS.stream(), config);
boolean isRHSNullableAnnotated =
Nullness.hasNullableAnnotation(annotationMirrorsRHS.stream(), config);
boolean isLHSNullableAnnotated = false;
List<Attribute.TypeCompound> lhsAnnotations = lhsTypeArgument.getAnnotationMirrors();
// To ensure that we are checking only jspecify nullable annotations
for (Attribute.TypeCompound annotation : lhsAnnotations) {
if (annotation.getAnnotationType().toString().equals(NULLABLE_NAME)) {
isLHSNullableAnnotated = true;
break;
}
}
boolean isRHSNullableAnnotated = false;
List<Attribute.TypeCompound> rhsAnnotations = rhsTypeArgument.getAnnotationMirrors();
// To ensure that we are checking only jspecify nullable annotations
for (Attribute.TypeCompound annotation : rhsAnnotations) {
if (annotation.getAnnotationType().toString().equals(NULLABLE_NAME)) {
isRHSNullableAnnotated = true;
break;
}
}
if (isLHSNullableAnnotated != isRHSNullableAnnotated) {
reportInvalidAssignmentInstantiationError(tree, lhsType, rhsType, state, analysis);
return;
Expand All @@ -232,6 +244,10 @@ private void compareNullabilityAnnotations(
}

/**
NikitaAware marked this conversation as resolved.
Show resolved Hide resolved
* For the Parameterized typed trees ASTHelpers.getType(tree) does not return a Type with
NikitaAware marked this conversation as resolved.
Show resolved Hide resolved
* preserved annotations. This method takes a Parameterized typed tree as an input and returns the
* Type of the tree with the annotations.
*
* @param tree A parameterized typed tree for which we need class type with preserved annotations.
* @return A Type with preserved annotations.
*/
Expand Down
1 change: 1 addition & 0 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ private enum NullMarking {
* <p>TODO remove this once NullAway requires JDK 11
*/
@Nullable private final Class<?> moduleElementClass;

/**
* Error Prone requires us to have an empty constructor for each Plugin, in addition to the
* constructor taking an ErrorProneFlags object. This constructor should not be used anywhere
Expand Down
Loading