8081271: NPE while compiling a program with erroneous use of constructor reference expressions
authorsadayapalam
Fri, 29 May 2015 09:15:42 +0530
changeset 30998 9e8f3b991f97
parent 30850 56166ce66037
child 30999 f1bb0191a5a7
8081271: NPE while compiling a program with erroneous use of constructor reference expressions Summary: Missing enclosing instance error should not be swallowed when attributing constructor reference expressions. Reviewed-by: mcimadamore, vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/lambda/MethodReference23.java
langtools/test/tools/javac/lambda/MethodReference23.out
langtools/test/tools/javac/lambda/MethodReference37.java
langtools/test/tools/javac/lambda/MethodReference37.out
langtools/test/tools/javac/lambda/methodReference/MethodRefToInnerWithoutOuter.java
langtools/test/tools/javac/lambda/methodReference/MethodRefToInnerWithoutOuter.out
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Jul 05 20:36:16 2017 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri May 29 09:15:42 2015 +0530
@@ -3185,10 +3185,8 @@
                 findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
                 findMethod(env, site, name, argtypes, typeargtypes,
                         phase.isBoxingRequired(), phase.isVarargsRequired());
-            return (sym.kind != MTH ||
-                    site.getEnclosingType().hasTag(NONE) ||
-                    hasEnclosingInstance(env, site)) ?
-                    sym : new BadConstructorReferenceError(sym);
+            return site.getEnclosingType().hasTag(CLASS) && !hasEnclosingInstance(env, site) ?
+                        new BadConstructorReferenceError(sym) : sym;
         }
 
         @Override
--- a/langtools/test/tools/javac/lambda/MethodReference23.java	Wed Jul 05 20:36:16 2017 +0200
+++ b/langtools/test/tools/javac/lambda/MethodReference23.java	Fri May 29 09:15:42 2015 +0530
@@ -1,6 +1,6 @@
 /*
  * @test /nodynamiccopyright/
- * @bug 8003280 8075184
+ * @bug 8003280 8075184 8081271
  * @summary Add lambda tests
  *  check that pair of bound/non-bound constructor references is flagged as ambiguous
  * @author  Maurizio Cimadamore
@@ -49,8 +49,8 @@
     static void call3(SAM22 s) {   }
 
     static void test11() {
-        SAM11 s = MethodReference23.Inner1::new; //ok
-        call11(MethodReference23.Inner1::new); //ok
+        SAM11 s = MethodReference23.Inner1::new; // fail.
+        call11(MethodReference23.Inner1::new); // fail.
     }
 
     static void test12() {
--- a/langtools/test/tools/javac/lambda/MethodReference23.out	Wed Jul 05 20:36:16 2017 +0200
+++ b/langtools/test/tools/javac/lambda/MethodReference23.out	Fri May 29 09:15:42 2015 +0530
@@ -1,5 +1,5 @@
 MethodReference23.java:52:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
-MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
+MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
 MethodReference23.java:57:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
 MethodReference23.java:58:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
 MethodReference23.java:72:9: compiler.err.ref.ambiguous: call3, kindname.method, call3(MethodReference23.SAM21), MethodReference23, kindname.method, call3(MethodReference23.SAM22), MethodReference23
--- a/langtools/test/tools/javac/lambda/MethodReference37.java	Wed Jul 05 20:36:16 2017 +0200
+++ b/langtools/test/tools/javac/lambda/MethodReference37.java	Fri May 29 09:15:42 2015 +0530
@@ -1,6 +1,6 @@
 /*
  * @test /nodynamiccopyright/
- * @bug 8003280
+ * @bug 8003280 8081271
  * @summary Add lambda tests
  *  spurious exceptions when checking references to inner constructors where
  *          the enclosing class is not defined in any outer context
@@ -20,7 +20,7 @@
     static class Outer {
         class Inner { }
 
-        static void test1() {
+        void test1() {
             SAM2<Inner, Outer> sam = Inner::new;
         }
 
--- a/langtools/test/tools/javac/lambda/MethodReference37.out	Wed Jul 05 20:36:16 2017 +0200
+++ b/langtools/test/tools/javac/lambda/MethodReference37.out	Fri May 29 09:15:42 2015 +0530
@@ -1,5 +1,5 @@
 MethodReference37.java:24:38: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
 MethodReference37.java:29:39: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
-MethodReference37.java:34:40: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
-MethodReference37.java:38:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
+MethodReference37.java:34:40: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner, MethodReference37.Outer, MethodReference37.Outer)
+MethodReference37.java:38:41: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner, MethodReference37.Outer, MethodReference37.Outer)
 4 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/methodReference/MethodRefToInnerWithoutOuter.java	Fri May 29 09:15:42 2015 +0530
@@ -0,0 +1,24 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8081271
+ * @summary NPE while compiling a program with erroneous use of constructor reference expressions.
+ * @compile/fail/ref=MethodRefToInnerWithoutOuter.out -XDrawDiagnostics MethodRefToInnerWithoutOuter.java
+*/
+
+import java.util.List;
+import java.util.ArrayList;
+
+class MethodRefToInnerBase {
+    class TestString {
+        String str;
+        TestString(String strin) {
+            str = strin;
+        }
+    }
+}
+public class MethodRefToInnerWithoutOuter extends MethodRefToInnerBase {
+    public static void main(String[] args) {
+        List<String> list = new ArrayList<>();
+        list.stream().forEach(TestString::new);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/methodReference/MethodRefToInnerWithoutOuter.out	Fri May 29 09:15:42 2015 +0530
@@ -0,0 +1,2 @@
+MethodRefToInnerWithoutOuter.java:22:31: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: TestString, , MethodRefToInnerBase)
+1 error