changeset 2147:c4292590fc70

8024809: javac, some lambda programs are rejected by flow analysis Reviewed-by: jjg, dlsmith
author vromero
date Sat, 19 Oct 2013 17:43:09 +0100
parents 130b8c0e570e
children e5d3cd43c85e
files src/share/classes/com/sun/tools/javac/comp/Attr.java test/tools/javac/lambda/8016081/T8016081.java test/tools/javac/lambda/LambdaExpr13.java test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTesta.java test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTesta.out test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTestb.java test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTestb.out test/tools/javac/lambda/TestSelfRef.java
diffstat 8 files changed, 131 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/comp/Attr.java	Fri Oct 18 16:34:42 2013 -0700
+++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java	Sat Oct 19 17:43:09 2013 +0100
@@ -314,9 +314,6 @@
                 case CLASSDEF:
                     //class def is always an owner
                     return ((JCClassDecl)env.tree).sym;
-                case LAMBDA:
-                    //a lambda is an owner - return a fresh synthetic method symbol
-                    return new MethodSymbol(0, names.empty, null, syms.methodClass);
                 case BLOCK:
                     //static/instance init blocks are owner
                     Symbol blockSym = env.info.scope.owner;
--- a/test/tools/javac/lambda/8016081/T8016081.java	Fri Oct 18 16:34:42 2013 -0700
+++ b/test/tools/javac/lambda/8016081/T8016081.java	Sat Oct 19 17:43:09 2013 +0100
@@ -32,7 +32,7 @@
     interface fint { int get(); }
 
     @interface atype {
-        fint fld = ()->( fld == null ?0 : 1);
+        fint fld = ()->1;
     }
 
     @atype class T {}
--- a/test/tools/javac/lambda/LambdaExpr13.java	Fri Oct 18 16:34:42 2013 -0700
+++ b/test/tools/javac/lambda/LambdaExpr13.java	Sat Oct 19 17:43:09 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2013, 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
@@ -26,13 +26,14 @@
  * @bug 8003280
  * @summary Add lambda tests
  *  check that recursive lambda (through field ref) is accepted in all contexts
+ *  but field initialization
  * @compile LambdaExpr13.java
  */
 
 class LambdaExpr13 {
 
-    Runnable ir = () -> { ir.run(); };;
-    static Runnable sr = () -> { sr.run(); };
+    Runnable ir;
+    static Runnable sr;
 
     { ir = () -> { ir.run(); }; }
     static { sr = () -> { sr.run(); }; }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTesta.java	Sat Oct 19 17:43:09 2013 +0100
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2013, 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
+ * @bug 8024809
+ * @summary javac, some lambda programs are rejected by flow analysis
+ * @compile/fail/ref=SelfInitializerInLambdaTesta.out -XDrawDiagnostics SelfInitializerInLambdaTesta.java
+ */
+
+public class SelfInitializerInLambdaTesta {
+
+    final Runnable r1 = ()->System.out.println(r1);
+
+    final Object lock = new Object();
+
+    final Runnable r2 = ()->{
+        System.out.println(r2);
+        synchronized (lock){}
+    };
+
+    final Runnable r3 = ()->{
+        synchronized (lock){
+            System.out.println(r3);
+        }
+    };
+
+    final Runnable r4 = ()->{
+        System.out.println(r4);
+    };
+
+    interface SAM {
+        int m(String s);
+    }
+
+    final SAM s1 = (String s)->{
+        System.out.println(s + s1.toString());
+        return 0;
+    };
+
+    final SAM s2 = (s)->{
+        System.out.println(s + s2.toString());
+        return 0;
+    };
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTesta.out	Sat Oct 19 17:43:09 2013 +0100
@@ -0,0 +1,7 @@
+SelfInitializerInLambdaTesta.java:33:48: compiler.err.illegal.self.ref
+SelfInitializerInLambdaTesta.java:38:28: compiler.err.illegal.self.ref
+SelfInitializerInLambdaTesta.java:44:32: compiler.err.illegal.self.ref
+SelfInitializerInLambdaTesta.java:49:28: compiler.err.illegal.self.ref
+SelfInitializerInLambdaTesta.java:57:32: compiler.err.illegal.self.ref
+SelfInitializerInLambdaTesta.java:62:32: compiler.err.illegal.self.ref
+6 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTestb.java	Sat Oct 19 17:43:09 2013 +0100
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2013, 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
+ * @bug 8024809
+ * @summary javac, some lambda programs are rejected by flow analysis
+ * @compile/fail/ref=SelfInitializerInLambdaTestb.out -XDrawDiagnostics SelfInitializerInLambdaTestb.java
+ */
+
+public class SelfInitializerInLambdaTestb {
+
+    final Runnable r1;
+
+    final Runnable r2 = ()-> System.out.println(r1);
+
+    SelfInitializerInLambdaTestb() {
+        r1 = ()->System.out.println(r1);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/lambda/T8024809/SelfInitializerInLambdaTestb.out	Sat Oct 19 17:43:09 2013 +0100
@@ -0,0 +1,3 @@
+SelfInitializerInLambdaTestb.java:35:49: compiler.err.var.might.not.have.been.initialized: r1
+SelfInitializerInLambdaTestb.java:38:37: compiler.err.var.might.not.have.been.initialized: r1
+2 errors
--- a/test/tools/javac/lambda/TestSelfRef.java	Fri Oct 18 16:34:42 2013 -0700
+++ b/test/tools/javac/lambda/TestSelfRef.java	Sat Oct 19 17:43:09 2013 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2013, 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
@@ -29,7 +29,6 @@
  *          consistently w.r.t. local inner classes
  */
 
-import com.sun.source.util.JavacTask;
 import java.net.URI;
 import java.util.Arrays;
 import javax.tools.Diagnostic;
@@ -38,6 +37,7 @@
 import javax.tools.SimpleJavaFileObject;
 import javax.tools.StandardJavaFileManager;
 import javax.tools.ToolProvider;
+import com.sun.source.util.JavacTask;
 
 public class TestSelfRef {
 
@@ -176,10 +176,16 @@
         check();
     }
 
+    boolean isErrorExpected() {
+        //illegal forward ref
+        boolean result = ik.inMethodContext(sk) && (rk.selfRef || rk.forwardRef);
+        result |= (rk == RefKind.SELF_LAMBDA || rk == RefKind.FORWARD_LAMBDA);
+        return result;
+    }
+
     void check() {
-        //illegal forward ref
-        boolean errorExpected = ik.inMethodContext(sk) &&
-                (rk.selfRef || rk.forwardRef);
+        checkCount++;
+        boolean errorExpected = isErrorExpected();
         if (diagChecker.errorFound != errorExpected) {
             throw new Error("invalid diagnostics for source:\n" +
                 source.getCharContent(true) +