8168134: Inference: javac incorrectly propagating inner constraint with primitive target
authormcimadamore
Wed, 26 Oct 2016 12:27:51 +0100
changeset 41855 0d1a58e6beb7
parent 41854 12089a7805af
child 41856 13a056e8f16e
8168134: Inference: javac incorrectly propagating inner constraint with primitive target Summary: Check for propagation doesn't take into account primitive type constraints Reviewed-by: vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
langtools/test/tools/javac/generics/inference/8168134/T8168134.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java	Wed Oct 26 11:22:50 2016 +0530
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java	Wed Oct 26 12:27:51 2016 +0100
@@ -404,15 +404,11 @@
         } else if (to.hasTag(NONE)) {
             to = from.isPrimitive() ? from : syms.objectType;
         } else if (qtype.hasTag(UNDETVAR)) {
-            if (resultInfo.pt.isReference()) {
-                if (needsEagerInstantiation((UndetVar)qtype, to, inferenceContext)) {
-                    to = generateReferenceToTargetConstraint(tree, (UndetVar)qtype, to, resultInfo, inferenceContext);
-                }
-            } else {
-                if (to.isPrimitive()) {
-                    to = generateReturnConstraintsPrimitive(tree, (UndetVar)qtype, to,
-                        resultInfo, inferenceContext);
-                }
+            if (needsEagerInstantiation((UndetVar)qtype, to, inferenceContext) &&
+                    (allowGraphInference || !to.isPrimitive())) {
+                to = generateReferenceToTargetConstraint(tree, (UndetVar)qtype, to, resultInfo, inferenceContext);
+            } else if (to.isPrimitive()) {
+                to = types.boxedClass(to).type;
             }
         } else if (rsInfoInfContext.free(resultInfo.pt)) {
             //propagation - cache captured vars
@@ -432,26 +428,21 @@
         return from;
     }
 
-    private Type generateReturnConstraintsPrimitive(JCTree tree, UndetVar from,
-            Type to, Attr.ResultInfo resultInfo, InferenceContext inferenceContext) {
-        if (!allowGraphInference) {
-            //if legacy, just return boxed type
-            return types.boxedClass(to).type;
+    private boolean needsEagerInstantiation(UndetVar from, Type to, InferenceContext inferenceContext) {
+        if (to.isPrimitive()) {
+            /* T is a primitive type, and one of the primitive wrapper classes is an instantiation,
+             * upper bound, or lower bound for alpha in B2.
+             */
+            for (Type t : from.getBounds(InferenceBound.values())) {
+                Type boundAsPrimitive = types.unboxedType(t);
+                if (boundAsPrimitive == null || boundAsPrimitive.hasTag(NONE)) {
+                    continue;
+                }
+                return true;
+            }
+            return false;
         }
-        //if graph inference we need to skip conflicting boxed bounds...
-        for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.UPPER,
-                InferenceBound.LOWER)) {
-            Type boundAsPrimitive = types.unboxedType(t);
-            if (boundAsPrimitive == null || boundAsPrimitive.hasTag(NONE)) {
-                continue;
-            }
-            return generateReferenceToTargetConstraint(tree, from, to,
-                    resultInfo, inferenceContext);
-        }
-        return types.boxedClass(to).type;
-    }
 
-    private boolean needsEagerInstantiation(UndetVar from, Type to, InferenceContext inferenceContext) {
         Type captureOfTo = types.capture(to);
         /* T is a reference type, but is not a wildcard-parameterized type, and either
          */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/inference/8168134/T8168134.java	Wed Oct 26 12:27:51 2016 +0100
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2016, 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 8168134
+ * @summary Inference: javac incorrectly propagating inner constraint with primitive target
+ * @compile T8168134.java
+ */
+
+abstract class T8168134 {
+    interface W<A> {}
+    abstract <B> B f(W<B> e);
+    abstract <C> C g(C b, long i);
+
+    void h(W<Long> i) {
+        g("", f(i));
+    }
+}