8205052: No compilation error thrown when no valid parameterization exists for functional interface type
authorvromero
Tue, 19 Jun 2018 05:22:07 -0700
changeset 50636 8a18bcdd75ed
parent 50635 5d3c5af82654
child 50637 359607017fb7
8205052: No compilation error thrown when no valid parameterization exists for functional interface type Reviewed-by: mcimadamore
src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.java
test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.out
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Jun 19 05:18:49 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Jun 19 05:22:07 2018 -0700
@@ -676,10 +676,21 @@
 
             public Type getType(Type site) {
                 site = removeWildcards(site);
-                if (!chk.checkValidGenericType(site)) {
-                    //if the inferred functional interface type is not well-formed,
-                    //or if it's not a subtype of the original target, issue an error
-                    throw failure(diags.fragment(Fragments.NoSuitableFunctionalIntfInst(site)));
+                if (site.isIntersection()) {
+                    IntersectionClassType ict = (IntersectionClassType)site;
+                    for (Type component : ict.getExplicitComponents()) {
+                        if (!chk.checkValidGenericType(component)) {
+                            //if the inferred functional interface type is not well-formed,
+                            //or if it's not a subtype of the original target, issue an error
+                            throw failure(diags.fragment(Fragments.NoSuitableFunctionalIntfInst(site)));
+                        }
+                    }
+                } else {
+                    if (!chk.checkValidGenericType(site)) {
+                        //if the inferred functional interface type is not well-formed,
+                        //or if it's not a subtype of the original target, issue an error
+                        throw failure(diags.fragment(Fragments.NoSuitableFunctionalIntfInst(site)));
+                    }
                 }
                 return memberType(site, descSym);
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.java	Tue Jun 19 05:22:07 2018 -0700
@@ -0,0 +1,32 @@
+/*
+ * @test  /nodynamiccopyright/
+ * @bug 8203338
+ * @summary Unboxing in return from lambda miscompiled to throw ClassCastException
+ * @compile/fail/ref=CheckWellFormednessIntersectionTypesTest.out -XDrawDiagnostics CheckWellFormednessIntersectionTypesTest.java
+ */
+
+public class CheckWellFormednessIntersectionTypesTest {
+    class U1 {}
+    class U3 {}
+
+    class X1 extends U1 {}
+    class X3 extends U3 {}
+
+    interface SAM<P1 extends X1, P2 extends P1, P3 extends X3> {
+        P3 m(P1 p1, P2 p2);
+    }
+
+    interface I<T> {}
+
+    @SuppressWarnings("unchecked")
+    class Tester {
+        public X3 foo(X1 x1, Object x2) { return new X3(); }
+        Object method(SAM<?, ?, ?> sam) {
+            return sam.m(null, null);
+        }
+
+        Object foo() {
+            return method((SAM<?, ?, ?> & I<?>) this::foo);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.out	Tue Jun 19 05:22:07 2018 -0700
@@ -0,0 +1,2 @@
+CheckWellFormednessIntersectionTypesTest.java:29:49: compiler.err.prob.found.req: (compiler.misc.no.suitable.functional.intf.inst: java.lang.Object&CheckWellFormednessIntersectionTypesTest.SAM<CheckWellFormednessIntersectionTypesTest.X1,java.lang.Object,CheckWellFormednessIntersectionTypesTest.X3>&CheckWellFormednessIntersectionTypesTest.I<java.lang.Object>)
+1 error