changeset 3000:53dd34e16447

Fix: lambda to method reference translation doesn't preserve unerased types
author mcimadamore
date Tue, 16 Jun 2015 13:17:30 +0100
parents 41ab3a104536
children 5f295bd9529c
files src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java test/tools/javac/valhalla/typespec/TestLambda01.java
diffstat 2 files changed, 67 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Wed Jun 10 12:10:48 2015 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Tue Jun 16 13:17:30 2015 +0100
@@ -966,13 +966,10 @@
                 BootstrapArgument.MethodType(typeToMethodType(tree.getDescriptorType(types))));
 
         //computed indy arg types
-        ListBuffer<Type> indy_args_types = new ListBuffer<>();
-        for (JCExpression arg : indy_args) {
-            indy_args_types.append(arg.type);
-        }
+        List<Type> indy_args_types = indy_args.map(this::extractUnerasedType);
 
         //finally, compute the type of the indy call
-        MethodType indyType = new MethodType(indy_args_types.toList(),
+        MethodType indyType = new MethodType(indy_args_types,
                 tree.unerasedType,
                 List.<Type>nil(),
                 syms.methodClass);
@@ -1025,6 +1022,16 @@
         return makeIndyCall(tree, syms.lambdaMetafactory, metafactoryName, staticArgs, indyType, indy_args, samSym.name);
     }
 
+    Type extractUnerasedType(JCExpression expr) {
+        switch (expr.getTag()) {
+            case NULLCHK:
+                return extractUnerasedType(((JCUnary)expr).arg);
+            default:
+                return expr.unerasedType == null ?
+                        expr.type : expr.unerasedType;
+        }
+    }
+
     // <editor-fold defaultstate="collapsed" desc="Lambda/reference analyzer">
     /**
      * This visitor collects information about translation of a lambda expression.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/valhalla/typespec/TestLambda01.java	Tue Jun 16 13:17:30 2015 +0100
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2015, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 unerased type are preserved when translating away lambdas and method references
+ */
+public class TestLambda01<any X> {
+
+    X x;
+
+	void test() {
+		testLambda().run();
+		testRef().run();
+    }
+
+	Runnable testLambda() {
+		return () -> { x = X.default; };
+    }
+
+	Runnable testRef() {
+		return this::g;
+    }
+
+	void g() {
+		x = X.default;
+    }
+
+	public static void main(String[] args) {
+		new TestLambda01<String>().test();
+		new TestLambda01<int>().test();
+    }
+}