Skip to content

Commit b7ac705

Browse files
lgxbslgxVicente Romero
authored and
Vicente Romero
committed
8263642: javac emits duplicate checkcast for first bound of intersection type in cast
Reviewed-by: vromero
1 parent e1462e7 commit b7ac705

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -767,7 +767,7 @@ public void visitTypeCast(JCTypeCast tree) {
767767
JCTypeCast typeCast = newExpression.hasTag(Tag.TYPECAST)
768768
? (JCTypeCast) newExpression
769769
: null;
770-
tree.expr = typeCast != null && types.isSameType(typeCast.type, originalTarget)
770+
tree.expr = typeCast != null && types.isSameType(typeCast.type, tree.type)
771771
? typeCast.expr
772772
: newExpression;
773773
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2021, 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 8263642
27+
* @summary javac should not emit duplicate checkcast for first bound of intersection type in cast
28+
* @library /tools/lib
29+
* @modules jdk.compiler/com.sun.tools.javac.api
30+
* jdk.compiler/com.sun.tools.javac.main
31+
* jdk.jdeps/com.sun.tools.classfile
32+
* @build toolbox.ToolBox toolbox.JavacTask
33+
* @run main DuplicatedCheckcastTest
34+
*/
35+
36+
import java.nio.file.Path;
37+
import java.util.ArrayList;
38+
39+
import com.sun.tools.classfile.ClassFile;
40+
import com.sun.tools.classfile.Method;
41+
import com.sun.tools.classfile.Attribute;
42+
import com.sun.tools.classfile.Code_attribute;
43+
import com.sun.tools.classfile.Instruction;
44+
import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;
45+
46+
import toolbox.JavacTask;
47+
import toolbox.TestRunner;
48+
import toolbox.ToolBox;
49+
50+
public class DuplicatedCheckcastTest extends TestRunner {
51+
ToolBox tb;
52+
ClassFile cf;
53+
54+
public DuplicatedCheckcastTest() {
55+
super(System.err);
56+
tb = new ToolBox();
57+
}
58+
59+
public static void main(String[] args) throws Exception {
60+
DuplicatedCheckcastTest t = new DuplicatedCheckcastTest();
61+
t.runTests();
62+
}
63+
64+
@Test
65+
public void testDuplicatedCheckcast() throws Exception {
66+
String code = """
67+
class IntersectionTypeTest {
68+
interface I1 { }
69+
static class C1 { }
70+
static Object test(Object o) {
71+
return (C1 & I1) o;
72+
}
73+
}""";
74+
Path curPath = Path.of(".");
75+
new JavacTask(tb)
76+
.sources(code)
77+
.outdir(curPath)
78+
.run();
79+
cf = ClassFile.read(curPath.resolve("IntersectionTypeTest.class"));
80+
ArrayList<Instruction> checkCastList = new ArrayList<>();
81+
for (Method method : cf.methods) {
82+
if ("test".equals(method.getName(cf.constant_pool))) {
83+
Code_attribute code_attribute = (Code_attribute) method.attributes.get(Attribute.Code);
84+
for (Instruction instruction : code_attribute.getInstructions()) {
85+
if ("checkcast".equals(instruction.getMnemonic())) {
86+
checkCastList.add(instruction);
87+
}
88+
}
89+
}
90+
}
91+
if (checkCastList.size() != 2) {
92+
throw new AssertionError("The number of the instruction 'checkcast' is not right. " +
93+
"Expected number: 2, actual number: " + checkCastList.size());
94+
}
95+
checkClassName(checkCastList.get(0), "IntersectionTypeTest$I1");
96+
checkClassName(checkCastList.get(1), "IntersectionTypeTest$C1");
97+
}
98+
99+
public void checkClassName(Instruction ins, String expected) throws Exception {
100+
int classIndex = ins.getUnsignedShort(1);
101+
CONSTANT_Class_info classInfo = cf.constant_pool.getClassInfo(classIndex);
102+
String className = classInfo.getName();
103+
if (!expected.equals(className)) {
104+
throw new AssertionError("The type of the 'checkcast' is not right. Expected: " +
105+
expected + ", actual: " + className);
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)