Skip to content

Commit

Permalink
8268885: duplicate checkcast when destination type is not first type …
Browse files Browse the repository at this point in the history
…of intersection type

Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Sep 9, 2021
1 parent ef4a532 commit dd1209e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Expand Up @@ -784,7 +784,7 @@ public void visitTypeCast(JCTypeCast tree) {
Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget;
for (Type c : ict.getExplicitComponents()) {
Type ec = erasure(c);
if (!types.isSameType(ec, tree.type)) {
if (!types.isSameType(ec, tree.type) && (!types.isSameType(ec, pt))) {
tree.expr = coerce(tree.expr, ec);
}
}
Expand Down
Expand Up @@ -23,8 +23,9 @@

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

static final String Code1 =
"""
class IntersectionTypeTest {
interface I1 { }
static class C1 { }
static Object test(Object o) {
return (C1 & I1) o;
}
}
""";

static final String Code2 =
"""
class IntersectionTypeTest {
interface I1 {}
interface I2 {}
static class C {}
void test() {
I2 i = (I1 & I2) new C();
}
}
""";

@Test
public void testDuplicatedCheckcast() throws Exception {
String code = """
class IntersectionTypeTest {
interface I1 { }
static class C1 { }
static Object test(Object o) {
return (C1 & I1) o;
}
}""";
duplicateCheckCastHelper(Code1, "IntersectionTypeTest$I1", "IntersectionTypeTest$C1");
duplicateCheckCastHelper(Code2, "IntersectionTypeTest$I1", "IntersectionTypeTest$I2");
}

private void duplicateCheckCastHelper(String source, String expected1, String expected2) throws Exception {
Path curPath = Path.of(".");
new JavacTask(tb)
.sources(code)
.sources(source)
.outdir(curPath)
.run();
cf = ClassFile.read(curPath.resolve("IntersectionTypeTest.class"));
Expand All @@ -92,8 +113,8 @@ static Object test(Object o) {
throw new AssertionError("The number of the instruction 'checkcast' is not right. " +
"Expected number: 2, actual number: " + checkCastList.size());
}
checkClassName(checkCastList.get(0), "IntersectionTypeTest$I1");
checkClassName(checkCastList.get(1), "IntersectionTypeTest$C1");
checkClassName(checkCastList.get(0), expected1);
checkClassName(checkCastList.get(1), expected2);
}

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

1 comment on commit dd1209e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.