Skip to content

Commit b78043f

Browse files
committed
8320220: Compilation of cyclic hierarchy causes infinite recursion
Reviewed-by: vromero, jlahoda
1 parent a3188e0 commit b78043f

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,12 @@ void checkClass(DiagnosticPosition pos, Symbol c, List<JCTree> supertypes) {
23742374
return;
23752375
if (seenClasses.contains(c)) {
23762376
errorFound = true;
2377-
noteCyclic(pos, (ClassSymbol)c);
2377+
log.error(pos, Errors.CyclicInheritance(c));
2378+
seenClasses.stream()
2379+
.filter(s -> !s.type.isErroneous())
2380+
.filter(ClassSymbol.class::isInstance)
2381+
.map(ClassSymbol.class::cast)
2382+
.forEach(Check.this::handleCyclic);
23782383
} else if (!c.type.isErroneous()) {
23792384
try {
23802385
seenClasses.add(c);
@@ -2451,7 +2456,8 @@ private boolean checkNonCyclicInternal(DiagnosticPosition pos, Type t) {
24512456
if ((c.flags_field & ACYCLIC) != 0) return true;
24522457

24532458
if ((c.flags_field & LOCKED) != 0) {
2454-
noteCyclic(pos, (ClassSymbol)c);
2459+
log.error(pos, Errors.CyclicInheritance(c));
2460+
handleCyclic((ClassSymbol)c);
24552461
} else if (!c.type.isErroneous()) {
24562462
try {
24572463
c.flags_field |= LOCKED;
@@ -2478,9 +2484,10 @@ private boolean checkNonCyclicInternal(DiagnosticPosition pos, Type t) {
24782484
return complete;
24792485
}
24802486

2481-
/** Note that we found an inheritance cycle. */
2482-
private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) {
2483-
log.error(pos, Errors.CyclicInheritance(c));
2487+
/** Handle finding an inheritance cycle on a class by setting
2488+
* the class' and its supertypes' types to the error type.
2489+
**/
2490+
private void handleCyclic(ClassSymbol c) {
24842491
for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail)
24852492
l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType);
24862493
Type st = types.supertype(c.type);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8320220
4+
* @summary Fix infinite recursion in cyclic inheritance situation
5+
* @compile/fail/ref=ClassCycle4.out -XDrawDiagnostics ClassCycle4.java
6+
*/
7+
8+
interface ClassCycle4 extends I1, I2 {}
9+
interface I1 extends ClassCycle4 {}
10+
interface I2 extends ClassCycle4 {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ClassCycle4.java:8:1: compiler.err.cyclic.inheritance: ClassCycle4
2+
1 error

0 commit comments

Comments
 (0)