Skip to content

Commit 39a55e9

Browse files
author
Vicente Romero
committed
8324809: compiler can crash with SOE while proving if two recursive types are disjoint
Reviewed-by: mcimadamore
1 parent b7ae0ae commit 39a55e9

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,6 +4734,10 @@ class Rewriter extends UnaryVisitor<Type> {
47344734

47354735
boolean high;
47364736
boolean rewriteTypeVars;
4737+
// map to avoid visiting same type argument twice, like in Foo<T>.Bar<T>
4738+
Map<Type, Type> argMap = new HashMap<>();
4739+
// cycle detection within an argument, see JDK-8324809
4740+
Set<Type> seen = new HashSet<>();
47374741

47384742
Rewriter(boolean high, boolean rewriteTypeVars) {
47394743
this.high = high;
@@ -4745,7 +4749,10 @@ public Type visitClassType(ClassType t, Void s) {
47454749
ListBuffer<Type> rewritten = new ListBuffer<>();
47464750
boolean changed = false;
47474751
for (Type arg : t.allparams()) {
4748-
Type bound = visit(arg);
4752+
Type bound = argMap.get(arg);
4753+
if (bound == null) {
4754+
argMap.put(arg, bound = visit(arg));
4755+
}
47494756
if (arg != bound) {
47504757
changed = true;
47514758
}
@@ -4774,13 +4781,17 @@ public Type visitCapturedType(CapturedType t, Void s) {
47744781

47754782
@Override
47764783
public Type visitTypeVar(TypeVar t, Void s) {
4777-
if (rewriteTypeVars) {
4778-
Type bound = t.getUpperBound().contains(t) ?
4779-
erasure(t.getUpperBound()) :
4780-
visit(t.getUpperBound());
4781-
return rewriteAsWildcardType(bound, t, EXTENDS);
4784+
if (seen.add(t)) {
4785+
if (rewriteTypeVars) {
4786+
Type bound = t.getUpperBound().contains(t) ?
4787+
erasure(t.getUpperBound()) :
4788+
visit(t.getUpperBound());
4789+
return rewriteAsWildcardType(bound, t, EXTENDS);
4790+
} else {
4791+
return t;
4792+
}
47824793
} else {
4783-
return t;
4794+
return rewriteTypeVars ? makeExtendsWildcard(syms.objectType, t) : t;
47844795
}
47854796
}
47864797

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8324809
27+
* @summary compiler can crash with SOE while proving if two recursive types are disjoint
28+
* @compile SOEWhileProvingDisjointnessTest.java
29+
*/
30+
31+
class SOEWhileProvingDisjointnessTest {
32+
class Criteria<B extends Builder<? extends Criteria>> {
33+
public <D extends Builder<E>, E extends Criteria<D>> D builder() {
34+
return (D) new Builder<>();
35+
}
36+
}
37+
38+
class Builder<C extends Criteria<? extends Builder<C>>> {}
39+
}

0 commit comments

Comments
 (0)