Skip to content

Commit

Permalink
8263642: javac emits duplicate checkcast for first bound of intersect…
Browse files Browse the repository at this point in the history
…ion type in cast

Reviewed-by: vromero
  • Loading branch information
lgxbslgx authored and Vicente Romero committed Jun 2, 2021
1 parent e1462e7 commit b7ac705
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -767,7 +767,7 @@ public void visitTypeCast(JCTypeCast tree) {
JCTypeCast typeCast = newExpression.hasTag(Tag.TYPECAST)
? (JCTypeCast) newExpression
: null;
tree.expr = typeCast != null && types.isSameType(typeCast.type, originalTarget)
tree.expr = typeCast != null && types.isSameType(typeCast.type, tree.type)
? typeCast.expr
: newExpression;
}
Expand Down
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @bug 8263642
* @summary javac should not emit duplicate checkcast for first bound of intersection type in cast
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.jdeps/com.sun.tools.classfile
* @build toolbox.ToolBox toolbox.JavacTask
* @run main DuplicatedCheckcastTest
*/

import java.nio.file.Path;
import java.util.ArrayList;

import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.Method;
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.Code_attribute;
import com.sun.tools.classfile.Instruction;
import com.sun.tools.classfile.ConstantPool.CONSTANT_Class_info;

import toolbox.JavacTask;
import toolbox.TestRunner;
import toolbox.ToolBox;

public class DuplicatedCheckcastTest extends TestRunner {
ToolBox tb;
ClassFile cf;

public DuplicatedCheckcastTest() {
super(System.err);
tb = new ToolBox();
}

public static void main(String[] args) throws Exception {
DuplicatedCheckcastTest t = new DuplicatedCheckcastTest();
t.runTests();
}

@Test
public void testDuplicatedCheckcast() throws Exception {
String code = """
class IntersectionTypeTest {
interface I1 { }
static class C1 { }
static Object test(Object o) {
return (C1 & I1) o;
}
}""";
Path curPath = Path.of(".");
new JavacTask(tb)
.sources(code)
.outdir(curPath)
.run();
cf = ClassFile.read(curPath.resolve("IntersectionTypeTest.class"));
ArrayList<Instruction> checkCastList = new ArrayList<>();
for (Method method : cf.methods) {
if ("test".equals(method.getName(cf.constant_pool))) {
Code_attribute code_attribute = (Code_attribute) method.attributes.get(Attribute.Code);
for (Instruction instruction : code_attribute.getInstructions()) {
if ("checkcast".equals(instruction.getMnemonic())) {
checkCastList.add(instruction);
}
}
}
}
if (checkCastList.size() != 2) {
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");
}

public void checkClassName(Instruction ins, String expected) throws Exception {
int classIndex = ins.getUnsignedShort(1);
CONSTANT_Class_info classInfo = cf.constant_pool.getClassInfo(classIndex);
String className = classInfo.getName();
if (!expected.equals(className)) {
throw new AssertionError("The type of the 'checkcast' is not right. Expected: " +
expected + ", actual: " + className);
}
}
}

1 comment on commit b7ac705

@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.