From 58f944b75e5c2e3415af3cfbd2d19e7a1d0af7f5 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Tue, 19 Jun 2018 05:22:07 -0700 Subject: [PATCH] 8205052: No compilation error thrown when no valid parameterization exists for functional interface type Reviewed-by: mcimadamore --- .../com/sun/tools/javac/code/Types.java | 19 ++++++++--- ...ckWellFormednessIntersectionTypesTest.java | 32 +++++++++++++++++++ ...eckWellFormednessIntersectionTypesTest.out | 2 ++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.java create mode 100644 test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java index d745ae5b3ca..3470ef544dd 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java @@ -676,10 +676,21 @@ public class Types { 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); } diff --git a/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.java b/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.java new file mode 100644 index 00000000000..b5293b942ae --- /dev/null +++ b/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.java @@ -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 { + P3 m(P1 p1, P2 p2); + } + + interface I {} + + @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); + } + } +} diff --git a/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.out b/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.out new file mode 100644 index 00000000000..12e2ce5fc38 --- /dev/null +++ b/test/langtools/tools/javac/T8203338/CheckWellFormednessIntersectionTypesTest.out @@ -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.I) +1 error