8320220: Compilation of cyclic hierarchy causes infinite recursion

Reviewed-by: vromero, jlahoda
This commit is contained in:
Archie Cobbs 2025-02-25 19:39:21 +00:00
parent a3188e0406
commit b78043fdc6
3 changed files with 24 additions and 5 deletions

View File

@ -2374,7 +2374,12 @@ public class Check {
return;
if (seenClasses.contains(c)) {
errorFound = true;
noteCyclic(pos, (ClassSymbol)c);
log.error(pos, Errors.CyclicInheritance(c));
seenClasses.stream()
.filter(s -> !s.type.isErroneous())
.filter(ClassSymbol.class::isInstance)
.map(ClassSymbol.class::cast)
.forEach(Check.this::handleCyclic);
} else if (!c.type.isErroneous()) {
try {
seenClasses.add(c);
@ -2451,7 +2456,8 @@ public class Check {
if ((c.flags_field & ACYCLIC) != 0) return true;
if ((c.flags_field & LOCKED) != 0) {
noteCyclic(pos, (ClassSymbol)c);
log.error(pos, Errors.CyclicInheritance(c));
handleCyclic((ClassSymbol)c);
} else if (!c.type.isErroneous()) {
try {
c.flags_field |= LOCKED;
@ -2478,9 +2484,10 @@ public class Check {
return complete;
}
/** Note that we found an inheritance cycle. */
private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) {
log.error(pos, Errors.CyclicInheritance(c));
/** Handle finding an inheritance cycle on a class by setting
* the class' and its supertypes' types to the error type.
**/
private void handleCyclic(ClassSymbol c) {
for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail)
l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType);
Type st = types.supertype(c.type);

View File

@ -0,0 +1,10 @@
/*
* @test /nodynamiccopyright/
* @bug 8320220
* @summary Fix infinite recursion in cyclic inheritance situation
* @compile/fail/ref=ClassCycle4.out -XDrawDiagnostics ClassCycle4.java
*/
interface ClassCycle4 extends I1, I2 {}
interface I1 extends ClassCycle4 {}
interface I2 extends ClassCycle4 {}

View File

@ -0,0 +1,2 @@
ClassCycle4.java:8:1: compiler.err.cyclic.inheritance: ClassCycle4
1 error