Skip to content

Commit

Permalink
Work around javac bug
Browse files Browse the repository at this point in the history
  • Loading branch information
smillst authored Mar 25, 2022
1 parent f6f6c58 commit 7493691
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5055,11 +5055,22 @@ private void annotateCapturedTypeVar(
typeVarToAnnotatedTypeArg, typeVariable.getUpperBound());
AnnotatedTypeMirror upperBound =
AnnotatedTypes.annotatedGLB(this, typeVarUpperBound, wildcard.getExtendsBound());
// There is a bug in javac such that the upper bound of the captured type variable is not the
// greatest lower bound. So the captureTypeVar.getUnderlyingType().getUpperBound() may not
// be the same type as upperbound.getUnderlyingType(). See
// framework/tests/all-systems/Issue4890Interfaces.java,
// framework/tests/all-systems/Issue4890.java and framework/tests/all-systems/Issue4877.java.
if (upperBound.getKind() == TypeKind.INTERSECTION
&& capturedTypeVar.getUpperBound().getKind() != TypeKind.INTERSECTION) {
// There is a bug in javac such that the upper bound of the captured type variable is not the
// greatest lower bound. So the captureTypeVar.getUnderlyingType().getUpperBound() may not
// be the same type as upperbound.getUnderlyingType(). See
// framework/tests/all-systems/Issue4890Interfaces.java,
// framework/tests/all-systems/Issue4890.java and framework/tests/all-systems/Issue4877.java.
// (I think this is https://bugs.openjdk.java.net/browse/JDK-8039222.)
for (AnnotatedTypeMirror bound : ((AnnotatedIntersectionType) upperBound).getBounds()) {
if (types.isSameType(
bound.underlyingType, capturedTypeVar.getUpperBound().getUnderlyingType())) {
upperBound = bound;
}
}
}

capturedTypeVar.setUpperBound(upperBound);

// typeVariable's lower bound is a NullType, so there's nothing to substitute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,20 @@ && isPrimarySubtype(bound, supertype)) {
return false;
}
}
return isSubtypeCaching(subtypeUpperBound, supertype);
try {
return isSubtypeCaching(subtypeUpperBound, supertype);
} catch (BugInCF e) {
if (TypesUtils.isCapturedTypeVariable(subtype.underlyingType)) {
// The upper bound of captured type variable may be computed incorrectly by javac. javac
// computes the upper bound as a declared type, when it should be an intersection type.
// (This is a bug in the GLB algorithm; see
// https://bugs.openjdk.java.net/browse/JDK-8039222)
// In this case, the upperbound is not a subtype of `supertype` and the Checker Framework
// crashes. So catch that crash and just return false.
return false;
}
throw e;
}
}

/**
Expand Down

0 comments on commit 7493691

Please sign in to comment.