Skip to content

Commit d13334d

Browse files
Ekaterina VergizovaYuri Nesterenko
Ekaterina Vergizova
authored and
Yuri Nesterenko
committed
8268885: duplicate checkcast when destination type is not first type of intersection type
Backport-of: dd1209e4ae3c8e42d121329639ec4bd359d0a456
1 parent ccb2034 commit d13334d

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ public void visitTypeCast(JCTypeCast tree) {
784784
Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget;
785785
for (Type c : ict.getExplicitComponents()) {
786786
Type ec = erasure(c);
787-
if (!types.isSameType(ec, tree.type)) {
787+
if (!types.isSameType(ec, tree.type) && (!types.isSameType(ec, pt))) {
788788
tree.expr = coerce(tree.expr, ec);
789789
}
790790
}

test/langtools/tools/javac/cast/intersection/DuplicatedCheckcastTest.java

+33-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
/**
2525
* @test
26-
* @bug 8263642
26+
* @bug 8263642 8268885
2727
* @summary javac should not emit duplicate checkcast for first bound of intersection type in cast
28+
* duplicate checkcast when destination type is not first type of intersection type
2829
* @library /tools/lib
2930
* @modules jdk.compiler/com.sun.tools.javac.api
3031
* jdk.compiler/com.sun.tools.javac.main
@@ -61,19 +62,39 @@ public static void main(String[] args) throws Exception {
6162
t.runTests();
6263
}
6364

65+
static final String Code1 =
66+
"""
67+
class IntersectionTypeTest {
68+
interface I1 { }
69+
static class C1 { }
70+
static Object test(Object o) {
71+
return (C1 & I1) o;
72+
}
73+
}
74+
""";
75+
76+
static final String Code2 =
77+
"""
78+
class IntersectionTypeTest {
79+
interface I1 {}
80+
interface I2 {}
81+
static class C {}
82+
void test() {
83+
I2 i = (I1 & I2) new C();
84+
}
85+
}
86+
""";
87+
6488
@Test
6589
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-
}""";
90+
duplicateCheckCastHelper(Code1, "IntersectionTypeTest$I1", "IntersectionTypeTest$C1");
91+
duplicateCheckCastHelper(Code2, "IntersectionTypeTest$I1", "IntersectionTypeTest$I2");
92+
}
93+
94+
private void duplicateCheckCastHelper(String source, String expected1, String expected2) throws Exception {
7495
Path curPath = Path.of(".");
7596
new JavacTask(tb)
76-
.sources(code)
97+
.sources(source)
7798
.outdir(curPath)
7899
.run();
79100
cf = ClassFile.read(curPath.resolve("IntersectionTypeTest.class"));
@@ -92,8 +113,8 @@ static Object test(Object o) {
92113
throw new AssertionError("The number of the instruction 'checkcast' is not right. " +
93114
"Expected number: 2, actual number: " + checkCastList.size());
94115
}
95-
checkClassName(checkCastList.get(0), "IntersectionTypeTest$I1");
96-
checkClassName(checkCastList.get(1), "IntersectionTypeTest$C1");
116+
checkClassName(checkCastList.get(0), expected1);
117+
checkClassName(checkCastList.get(1), expected2);
97118
}
98119

99120
public void checkClassName(Instruction ins, String expected) throws Exception {

0 commit comments

Comments
 (0)