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;
}
}
}
Expand Up @@ -804,7 +804,17 @@ 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 a local variable of type ? extends T is assigned to a
Copy link
Member

Choose a reason for hiding this comment

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

A local variable can't have a wildcard type. I think you when an expression has type ? extends T.

// field of type T, such as in the test ainfer-index/non-annotated/Dataset6Crash.java.
try {
updateAtmWithLub(rhsATM, atmFromStorage);
} catch (ClassCastException c) {
AnnotatedTypeMirror compatibleRHSAtm =
AnnotatedTypes.asSuper(this.atypeFactory, rhsATM, atmFromStorage);
updateAtmWithLub(compatibleRHSAtm, atmFromStorage);
}
if (lhsATM instanceof AnnotatedTypeVariable) {
Set<AnnotationMirror> upperAnnos =
((AnnotatedTypeVariable) lhsATM).getUpperBound().getEffectiveAnnotations();
Expand Down