8058199: Code generation problem with javac skipping a checkcast instruction
authormcimadamore
Tue, 14 Oct 2014 12:00:39 +0100
changeset 27124 5b69a74c224e
parent 27123 10171e97e021
child 27125 6c718c5086b3
8058199: Code generation problem with javac skipping a checkcast instruction Summary: TransTypes sometimes ignores inferred signatures when emitting synthetic casts Reviewed-by: vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java
langtools/test/tools/javac/generics/inference/8058199/T8058199.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java	Mon Oct 13 17:09:12 2014 -0400
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/TransTypes.java	Tue Oct 14 12:00:39 2014 +0100
@@ -67,11 +67,15 @@
     private Symtab syms;
     private TreeMaker make;
     private Enter enter;
-    private boolean allowInterfaceBridges;
     private Types types;
     private final Resolve resolve;
+    private final CompileStates compileStates;
 
-    private final CompileStates compileStates;
+    /** Switch: is complex graph inference supported? */
+    private final boolean allowGraphInference;
+
+    /** Switch: are default methods supported? */
+    private final boolean allowInterfaceBridges;
 
     protected TransTypes(Context context) {
         context.put(transTypesKey, this);
@@ -81,11 +85,12 @@
         syms = Symtab.instance(context);
         enter = Enter.instance(context);
         overridden = new HashMap<>();
-        Source source = Source.instance(context);
-        allowInterfaceBridges = source.allowDefaultMethods();
         types = Types.instance(context);
         make = TreeMaker.instance(context);
         resolve = Resolve.instance(context);
+        Source source = Source.instance(context);
+        allowInterfaceBridges = source.allowDefaultMethods();
+        allowGraphInference = source.allowGraphInference();
     }
 
     /** A hashtable mapping bridge methods to the methods they override after
@@ -654,7 +659,11 @@
         tree.meth = translate(tree.meth, null);
         Symbol meth = TreeInfo.symbol(tree.meth);
         Type mt = meth.erasure(types);
-        List<Type> argtypes = mt.getParameterTypes();
+        boolean useInstantiatedPtArgs =
+                allowGraphInference && !types.isSignaturePolymorphic((MethodSymbol)meth.baseSymbol());
+        List<Type> argtypes = useInstantiatedPtArgs ?
+                tree.meth.type.getParameterTypes() :
+                mt.getParameterTypes();
         if (meth.name == names.init && meth.owner == syms.enumSym)
             argtypes = argtypes.tail.tail;
         if (tree.varargsElement != null)
@@ -675,14 +684,23 @@
     public void visitNewClass(JCNewClass tree) {
         if (tree.encl != null)
             tree.encl = translate(tree.encl, erasure(tree.encl.type));
+
+        Type erasedConstructorType = tree.constructorType != null ?
+                erasure(tree.constructorType) :
+                null;
+
+        List<Type> argtypes = erasedConstructorType != null && allowGraphInference ?
+                erasedConstructorType.getParameterTypes() :
+                tree.constructor.erasure(types).getParameterTypes();
+
         tree.clazz = translate(tree.clazz, null);
         if (tree.varargsElement != null)
             tree.varargsElement = types.erasure(tree.varargsElement);
         tree.args = translateArgs(
-            tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
+            tree.args, argtypes, tree.varargsElement);
         tree.def = translate(tree.def, null);
-        if (tree.constructorType != null)
-            tree.constructorType = erasure(tree.constructorType);
+        if (erasedConstructorType != null)
+            tree.constructorType = erasedConstructorType;
         tree.type = erasure(tree.type);
         result = tree;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/inference/8058199/T8058199.java	Tue Oct 14 12:00:39 2014 +0100
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2014, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 8058199
+ * @summary Code generation problem with javac skipping a checkcast instruction
+ */
+public class T8058199 {
+
+    final static String SYNTHETIC_CAST_TYPE = "[Ljava.lang.String;";
+
+    @SuppressWarnings("unchecked")
+    <Z> Z[] makeArr(Z z) { return (Z[])new Object[1]; }
+
+    <U> void check(U u) { }
+
+    void testMethod() {
+        test(() -> check(makeArr("")));
+    }
+
+    void testNewDiamond() {
+        class Check<X> {
+            Check(X x) { }
+        }
+        test(()-> new Check<>(makeArr("")));
+    }
+
+    void testNewGeneric() {
+        class Check {
+            <Z> Check(Z z) { }
+        }
+        test(()-> new Check(makeArr("")));
+    }
+
+    private void test(Runnable r) {
+        try {
+            r.run();
+            throw new AssertionError("Missing synthetic cast");
+        } catch (ClassCastException cce) {
+            if (!cce.getMessage().contains(SYNTHETIC_CAST_TYPE)) {
+                throw new AssertionError("Bad type in synthetic cast", cce);
+            }
+        }
+    }
+
+    public static void main(String[] args) {
+        T8058199 test = new T8058199();
+        test.testMethod();
+        test.testNewDiamond();
+        test.testNewGeneric();
+    }
+}