changeset 936:8d77997dfc50

Bug fix: SAM conversion and raw types Implement latest changes in lambda spec involving SAM conversion and raw types in argument/return types
author mcimadamore
date Fri, 25 Mar 2011 15:51:00 +0000
parents cb9074345db0
children bb800adbe578
files src/share/classes/com/sun/tools/javac/code/Types.java test/tools/javac/lambda/LambdaConv16.java
diffstat 2 files changed, 113 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/code/Types.java	Tue Mar 15 17:21:42 2011 +0000
+++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Fri Mar 25 15:51:00 2011 +0000
@@ -320,7 +320,8 @@
             boolean isReturnOk = t.getReturnType().tag == NONE || (t.getReturnType() == syms.voidType ?
                 (isSameType(mtype.getReturnType(), boxedClass(syms.voidType).type) ||
                     isSameType(mtype.getReturnType(), syms.voidType)) :
-                containsType(boxedTypeOrType(mtype.getReturnType()), boxedTypeOrType(t.getReturnType())));
+                (containsType(boxedTypeOrType(mtype.getReturnType()), boxedTypeOrType(t.getReturnType()))) ||
+                covariantReturnType(boxedTypeOrType(t.getReturnType()), boxedTypeOrType(mtype.getReturnType()), Warner.noWarnings));
 
             boolean argsOk = t.getParameterTypes().size() == mtype.getParameterTypes().size() &&
                 containsType(mtype.getParameterTypes(), t.getParameterTypes());
@@ -603,10 +604,6 @@
                 }
                 else {
                     // size > 1
-                    Type resType = Type.noType;
-                    List<Type> thrownTypes = List.nil();
-                    List<Type> argtypes =
-                            memberType(samType, methodSyms.head).getParameterTypes();
                     for (Symbol msym : methodSyms) {
                         if (msym.type.tag == FORALL) {
                             setDiagnostic("invalid.generic.target.for.lambda.conv");
@@ -615,31 +612,62 @@
                             setDiagnostic("target.not.accessible");
                             return syms.errType;
                         }
-                        Type mtype = memberType(samType, msym);
-
-                        //return type is the smallest between all candidate types
-                        resType = resType == Type.noType ?
-                            mtype.getReturnType() :
-                            bestReturnType(resType, mtype.getReturnType());
-
-                        if (resType.isErroneous()) {
-                            setDiagnostic("incompatible.targets.in.lambda.conv");
-                            return syms.errType;
-                        }
-                        thrownTypes = chk.union(mtype.getThrownTypes(), thrownTypes);
                     }
-                    return new MethodType(argtypes,
+                    Type resType = chooseSAMDescriptorReturnType();
+                    if (resType == null) {
+                        setDiagnostic("incompatible.targets.in.lambda.conv");
+                        return syms.errType;
+                    }
+                    return new MethodType(chooseSAMDescriptorArgumentTypes(),
                             resType,
-                            thrownTypes,
+                            chooseSAMDescriptorThrownTypes(),
                             syms.methodClass);
                 }
             }
         }
         //WHERE
-            private Type bestReturnType(Type bestSoFar, Type t) {
-                if (bestSoFar.tag == VOID) return syms.voidType;
-                else if (bestSoFar.isPrimitive()) return isSameType(bestSoFar, t) ? t : syms.errType;
-                else return glb(bestSoFar, t);
+            private Type chooseSAMDescriptorReturnType() {
+                Type r = chooseSAMDescriptorReturnType(false);
+                return r != null ? r : chooseSAMDescriptorReturnType(true);
+            }
+            
+            private Type chooseSAMDescriptorReturnType(boolean rawOk) {
+                outer: for (Symbol msym1 : methodSyms) {
+                    Type rt1 = memberType(samType, msym1).getReturnType();
+                    for (Symbol msym2 : methodSyms) {
+                        Type rt2 = memberType(samType, msym2).getReturnType();
+                        if ((rt1.isRaw() && !rawOk) || !covariantReturnType(rt1, rt2, Warner.noWarnings)) {
+                            continue outer;
+                        }
+                    }
+                    return rt1;
+                }
+                return null;
+            }
+
+            private List<Type> chooseSAMDescriptorArgumentTypes() {
+                outer: for (Symbol msym1 : methodSyms) {
+                    Type mt1 = memberType(samType, msym1);
+                    for (Symbol msym2 : methodSyms) {
+                        Type mt2 = memberType(samType, msym2);
+                        if (!isSubSignature(mt1, mt2)) {
+                            continue outer;
+                        }
+                    }
+                    return mt1.getParameterTypes();
+                }
+                return erasure(memberType(samType, methodSyms.head)).getParameterTypes();
+            }
+
+            private List<Type> chooseSAMDescriptorThrownTypes() {
+                List<Type> thrown = null;
+                for (Symbol msym1 : methodSyms) {
+                    Type mt1 = memberType(samType, msym1);
+                    thrown = (thrown == null) ?
+                        mt1.getThrownTypes() :
+                        chk.intersect(mt1.getThrownTypes(), thrown);
+                }
+                return thrown;
             }
 
         public JCDiagnostic getDiagnostic(JCDiagnostic.Factory diags) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/LambdaConv16.java	Fri Mar 25 15:51:00 2011 +0000
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2011, 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.
+ *
+ * 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
+ * @summary SAM conversion and raw types in argument/return types
+ * @author  Maurizio Cimadamore
+ * @compile -XDabstractSAM LambdaConv16.java
+ * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableMethodHandles LambdaConv16
+ */
+
+import java.util.*;
+
+public class LambdaConv16 {
+
+    static int assertionCount = 0;
+
+    static void assertTrue(boolean cond) {
+        assertionCount++;
+        if (!cond)
+            throw new AssertionError();
+    }
+
+    interface A {
+        Iterable m(List<String> ls);
+    }
+
+    interface B {
+        Iterable<String> m(List l);
+    }
+
+    interface AB extends A, B {} //SAM type ([List], Iterable<String>, {})
+
+    static void test(AB ab, List l) { ab.m(l); }
+
+    public static void main(String[] args) {
+        AB ab = #{List list -> assertTrue(true); return new ArrayList<String>(); };
+        ab.m(null);
+        test(#{List list -> assertTrue(true); return new ArrayList<String>(); }, null);
+        assertTrue(assertionCount == 2);
+    }
+}