changeset 937:cb9074345db0

Bug fix: attribution of lambda expression throw AssertionError Attribution of lambda expression crashes if lambda contains a cast involving lambda parameter w/o explicit type.
author mcimadamore
date Tue, 15 Mar 2011 17:21:42 +0000
parents 1623ce0ec047
children e4d6738ea2da 8d77997dfc50
files src/share/classes/com/sun/tools/javac/comp/Attr.java test/tools/javac/lambda/TargetType18.java
diffstat 2 files changed, 57 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Mon Mar 07 13:54:51 2011 +0000
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Mar 15 17:21:42 2011 +0000
@@ -2306,18 +2306,28 @@
                 argTypes.append(attribType(param.vartype, env));
             }
         }
-        Type ft = attribLambda(tempLambda);
-        final boolean needsReturnTypeInferred = ft.getReturnType() == Type.noType;
-        if (needsReturnTypeInferred) {
-            TypeSymbol tvSym = new TypeSymbol(LAMBDA,
+
+        TypeSymbol tvSym = new TypeSymbol(LAMBDA,
                         names.fromString("R"),
                         null,
                         env.info.scope.owner);
-            TypeVar resType = new TypeVar(tvSym, Type.noType, null);
+        TypeVar resType = new TypeVar(tvSym, Type.noType, null);
+
+        Type ft;
+        
+        try {
+            ft = attribLambda(tempLambda);
+            if (ft.getReturnType() == Type.noType) {
+                tvars.prepend(resType);
+                ft = new FunctionType(ft.getParameterTypes(),
+                        resType, ft.getThrownTypes(), ft.tsym);
+            }
+        } catch(Throwable t) {
             tvars.prepend(resType);
-            ft = new FunctionType(ft.getParameterTypes(),
-                    resType, ft.getThrownTypes(), ft.tsym);
+            ft = new FunctionType(argTypes.toList(), resType, List.<Type>nil(), syms.methodClass);
         }
+        final boolean needsReturnTypeInferred = ft.getReturnType() == resType;
+        
         return that.type = new ForAll(tvars.toList(), ft) {
             @Override
             public Type inst(List<Type> actuals, Type to, Types types, ForAll.InstantiationPhase phase) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/TargetType18.java	Tue Mar 15 17:21:42 2011 +0000
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2010, 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 check that javac doesn't crash if implicit lambda parameter is involved in cast
+ * @compile TargetType18.java
+ */
+
+class TargetType18 {
+
+    interface Folder<T> {
+        public T fold(T a, T b);
+    }
+
+
+    public static <T> Folder<T> max() {
+        return #{ a, b -> (((Comparable)a).compareTo(b)<0)? b: a };
+    }
+}