8222035: minimal inference context optimization is forcing resolution with incomplete constraints
authorvromero
Wed, 10 Apr 2019 17:15:53 -0400
changeset 54493 17b1c2c467ad
parent 54492 9d0ae9508d53
child 54494 1bbce3048d20
8222035: minimal inference context optimization is forcing resolution with incomplete constraints Reviewed-by: mcimadamore, cushon
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/InferenceContext.java
test/langtools/tools/javac/T8222035/MinContextOpTest.java
test/langtools/tools/javac/T8222035/MinContextOpTest.out
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/InferenceContext.java	Wed Apr 10 20:03:07 2019 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/InferenceContext.java	Wed Apr 10 17:15:53 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, 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
@@ -30,6 +30,7 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
 
@@ -366,9 +367,11 @@
         for (Type t : minContext.inferencevars) {
             //add listener that forwards notifications to original context
             minContext.addFreeTypeListener(List.of(t), (inferenceContext) -> {
-                ((UndetVar)asUndetVar(t)).setInst(inferenceContext.asInstType(t));
-                infer.doIncorporation(inferenceContext, warn);
-                solve(List.from(rv.minMap.get(t)), warn);
+                Type instType = inferenceContext.asInstType(t);
+                for (Type eq : rv.minMap.get(t)) {
+                    ((UndetVar)asUndetVar(eq)).setInst(instType);
+                }
+                infer.doIncorporation(this, warn);
                 notifyChange();
             });
         }
@@ -385,9 +388,9 @@
 
     class ReachabilityVisitor extends Types.UnaryVisitor<Void> {
 
-        Set<Type> equiv = new HashSet<>();
-        Set<Type> min = new HashSet<>();
-        Map<Type, Set<Type>> minMap = new HashMap<>();
+        Set<Type> equiv = new LinkedHashSet<>();
+        Set<Type> min = new LinkedHashSet<>();
+        Map<Type, Set<Type>> minMap = new LinkedHashMap<>();
 
         void scan(List<Type> roots) {
             roots.stream().forEach(this::visit);
@@ -401,7 +404,7 @@
         @Override
         public Void visitUndetVar(UndetVar t, Void _unused) {
             if (min.add(t.qtype)) {
-                Set<Type> deps = minMap.getOrDefault(t.qtype, new HashSet<>(Collections.singleton(t.qtype)));
+                Set<Type> deps = minMap.getOrDefault(t.qtype, new LinkedHashSet<>(Collections.singleton(t.qtype)));
                 for (InferenceBound boundKind : InferenceBound.values()) {
                     for (Type b : t.getBounds(boundKind)) {
                         Type undet = asUndetVar(b);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8222035/MinContextOpTest.java	Wed Apr 10 17:15:53 2019 -0400
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2019, Google LLC. 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 8222035
+ * @summary minimal inference context optimization is forcing resolution with incomplete constraints
+ * @compile/fail/ref=MinContextOpTest.out -XDrawDiagnostics MinContextOpTest.java
+ */
+
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collector;
+import java.util.stream.Stream;
+
+public class MinContextOpTest {
+    abstract class A {
+        abstract static class T<K> {
+            abstract String f();
+        }
+
+        abstract <E> Function<E, E> id();
+
+        abstract static class ImmutableMap<K, V> implements Map<K, V> {}
+
+        abstract <T, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
+                Function<? super T, ? extends K> k, Function<? super T, ? extends V> v);
+
+        ImmutableMap<String, T<?>> test(Stream<T> stream) {
+            return stream.collect(toImmutableMap(T::f, id()));
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8222035/MinContextOpTest.out	Wed Apr 10 17:15:53 2019 -0400
@@ -0,0 +1,4 @@
+MinContextOpTest.java:38:25: compiler.err.mod.not.allowed.here: static
+MinContextOpTest.java:44:25: compiler.err.mod.not.allowed.here: static
+MinContextOpTest.java:50:34: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T,K,V,E, (compiler.misc.inconvertible.types: java.util.function.Function<MinContextOpTest.A.T,MinContextOpTest.A.T>, java.util.function.Function<? super MinContextOpTest.A.T,? extends MinContextOpTest.A.T<?>>))
+3 errors