changeset 560:535313e3087e

Partially revert previous fix; lambda can return from *every* context (as per strawman). However there was a bug when final variable had a lambda initializer that got fixed as part of the previous push.
author mcimadamore
date Tue, 15 Jun 2010 13:40:00 +0100
parents 3e219523bf43
children 77f10f22c3e4
files src/share/classes/com/sun/tools/javac/comp/Attr.java test/tools/javac/lambda/BadReturn02.java test/tools/javac/lambda/BadReturn02.out test/tools/javac/lambda/LambdaExpr02.java test/tools/javac/lambda/LambdaExpr04.java
diffstat 5 files changed, 71 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jun 15 12:56:14 2010 +0100
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Tue Jun 15 13:40:00 2010 +0100
@@ -1215,16 +1215,16 @@
     public void visitReturn(JCReturn tree) {
         // Check that there is an enclosing method which is
         // nested within than the enclosing class.
-        boolean isLambda = env.enclMethod != null &&
-                (env.enclMethod.sym.flags() & LAMBDA) != 0;
         if (env.enclMethod == null ||
-            env.enclMethod.sym.owner != env.enclClass.sym ||
-            (isLambda && env.next.enclMethod == null)) {
+            (env.enclMethod.sym.owner != env.enclClass.sym &&
+            !env.enclMethod.sym.name.equals(names.lambda))) {
             log.error(tree.pos(), "ret.outside.meth");
+
         } else {
             // Attribute return expression, if it exists, and check that
             // it conforms to result type of enclosing method.
             Symbol m = env.enclMethod.sym;
+            boolean isLambda = (env.enclMethod.sym.flags() & LAMBDA) != 0;
             if (m.type.getReturnType().tag == VOID) {
                 if (tree.expr != null)
                     log.error(tree.expr.pos(),
--- a/test/tools/javac/lambda/BadReturn02.java	Tue Jun 15 12:56:14 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * 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 return statement outside method context are flagged with errors
- * @author Jan Lahoda
- * @author  Maurizio Cimadamore
- * @compile/fail/ref=BadReturn02.out -XDrawDiagnostics BadReturn02.java
- */
-
-class BadReturn02 {
-    static Object lambda_01 = #(int pos) {
-        return;
-    };
-
-    static final Object lambda_02 = #(int pos) {
-        return;
-    };
-
-    Object lambda_03 = #(int pos) {
-        return;
-    };
-
-    final Object lambda_04 = #(int pos) {
-        return;
-    };
-}
--- a/test/tools/javac/lambda/BadReturn02.out	Tue Jun 15 12:56:14 2010 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-BadReturn02.java:34:9: compiler.err.ret.outside.meth
-BadReturn02.java:38:9: compiler.err.ret.outside.meth
-BadReturn02.java:42:9: compiler.err.ret.outside.meth
-BadReturn02.java:46:9: compiler.err.ret.outside.meth
-4 errors
--- a/test/tools/javac/lambda/LambdaExpr02.java	Tue Jun 15 12:56:14 2010 +0100
+++ b/test/tools/javac/lambda/LambdaExpr02.java	Tue Jun 15 13:40:00 2010 +0100
@@ -39,6 +39,32 @@
             throw new AssertionError();
     }
 
+    static {
+        int i1 = #(){ return 3; }.();
+        assertTrue(3 == i1);
+        Integer i2 = #(){ return 3; }.();
+        assertTrue(3 == i2);
+        int i3 = #(int x){ return x + 1; }.(3);
+        assertTrue(4 == i3);
+        int i4 = #(Number x){ return x.intValue(); }.(new Float(3.0f));
+        assertTrue(3 == i4);
+        Object o = #(){ return 3; };
+        assertTrue(o != null);
+    }
+
+    {
+        int i1 = #(){ return 3; }.();
+        assertTrue(3 == i1);
+        Integer i2 = #(){ return 3; }.();
+        assertTrue(3 == i2);
+        int i3 = #(int x){ return x + 1; }.(3);
+        assertTrue(4 == i3);
+        int i4 = #(Number x){ return x.intValue(); }.(new Float(3.0f));
+        assertTrue(3 == i4);
+        Object o = #(){ return 3; };
+        assertTrue(o != null);
+    }
+
     static void test1() {
         int i1 = #(){ return 3; }.();
         assertTrue(3 == i1);
@@ -68,6 +94,6 @@
     public static void main(String[] args) {
         test1();
         new LambdaExpr02().test2();
-        assertTrue(assertionCount == 10);
+        assertTrue(assertionCount == 20);
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/LambdaExpr04.java	Tue Jun 15 13:40:00 2010 +0100
@@ -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 lambda initializers compile w/o problems
+ * @author Jan Lahoda
+ * @author  Maurizio Cimadamore
+ * @compile LambdaExpr04.java
+ */
+
+class LambdaExpr04 {
+    static Object lambda_01 = #(int pos) {};
+
+    static final Object lambda_02 = #(int pos) {};
+
+    Object lambda_03 = #(int pos) {};
+
+    final Object lambda_04 = #(int pos) {};
+}