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

Fix crash in ajava-based WPI related to captures #5335

Merged
merged 10 commits into from Sep 28, 2022
57 changes: 57 additions & 0 deletions checker/tests/ainfer-index/non-annotated/Dataset6Crash.java
@@ -0,0 +1,57 @@
// Test case for a WPI crash caused by mismatches between captured type variables
// and the declared type of a field: in particular, the issue is that base.next()
// the next field actually have slightly different types: base.next()'s type is
// a capture that extends T.

import java.util.Iterator;

public class Dataset6Crash {

public static <T> Iterator<T> limit(final Iterator<? extends T> base, final CountingPredicate<?
super T> filter) {
return new Iterator<T>() {

private T next;

private boolean end;

private int index = 0;

public boolean hasNext() {
return true;
}

public T next() {
fetch();
T r = next;
next = null;
return r;
}

private void fetch() {
if (next == null && !end) {
if (base.hasNext()) {
next = base.next();
if (!filter.apply(index++, next)) {
next = null;
end = true;
}
} else {
end = true;
}
}
}

public void remove() {
throw new UnsupportedOperationException();
}
};
}


private static class CountingPredicate<T> {
public boolean apply(int i, T next) {
return false;
}
}
}
@@ -0,0 +1,6 @@
public class TypeVarAssignment<T, S extends T> {
T t;
void foo(S s) {
t = s;
}
}
Expand Up @@ -804,7 +804,26 @@ protected void updateAnnotationSet(

AnnotatedTypeMirror atmFromStorage =
storage.atmFromStorageLocation(rhsATM.getUnderlyingType(), annotationsToUpdate);
updateAtmWithLub(rhsATM, atmFromStorage);
// It is possible that rhsATM is a type variable but atmFromStorage is not, which
// would cause a ClassCastException in updateAtmWithLub(). This can
// happen for example when an expression of type ? extends T is assigned to a
// field of type T, such as in the test ainfer-index/non-annotated/Dataset6Crash.java.
try {
updateAtmWithLub(rhsATM, atmFromStorage);
} catch (ClassCastException c) {
System.out.println("Catching a ClassCastException: " + c);
System.out.println(
Copy link
Member

Choose a reason for hiding this comment

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

The problem isn't a AnnotatedDeclaredType with an underlying type of TYPEVAR, it's when updateAtmWithLub recurs on the upper bounds of the type var, they may not be the same kind of type. This happens with capture conversion, but also two type variables. See the test case I added.

Hopefully using that information you can fix the problem, rather than catching the exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You were exactly right - thanks for figuring this out! I'm glad there wasn't a deeper problem, and I've now written a proper fix.

"The cast must be one of the following two AnnotatedTypeMirrors. The"
+ " type to cast to is decided by the kind of rhsATM.");
System.out.println("rhsATM: " + rhsATM);
System.out.println("rhsATM.getKind(): " + rhsATM.getKind());
System.out.println("atmFromStorage: " + atmFromStorage);
System.out.println("atmFromStorage.getKind(): " + atmFromStorage.getKind());

AnnotatedTypeMirror compatibleRHSAtm =
AnnotatedTypes.asSuper(this.atypeFactory, rhsATM, atmFromStorage);
updateAtmWithLub(compatibleRHSAtm, atmFromStorage);
}
if (lhsATM instanceof AnnotatedTypeVariable) {
Set<AnnotationMirror> upperAnnos =
((AnnotatedTypeVariable) lhsATM).getUpperBound().getEffectiveAnnotations();
Expand Down