8197439: Crash with -XDfind=lambda for anonymous class in anonymous class.
authorjlahoda
Wed, 21 Feb 2018 17:07:12 +0100
changeset 48931 b25eb74ec283
parent 48930 b1a5b4ad7427
child 48932 9e3f2ec326ba
8197439: Crash with -XDfind=lambda for anonymous class in anonymous class. Summary: Ensuring unresolvable anonymous classes are attributed. Reviewed-by: mcimadamore, sadayapalam
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
test/langtools/jdk/jshell/CompletionSuggestionTest.java
test/langtools/tools/javac/analyzer/AnonymousInAnonymous.java
test/langtools/tools/javac/analyzer/AnonymousInAnonymous.out
test/langtools/tools/javac/failover/PostAttrConstructor.java
test/langtools/tools/javac/generics/diamond/neg/Neg05.out
test/langtools/tools/javac/generics/diamond/neg/Neg06.out
test/langtools/tools/javac/generics/diamond/neg/Neg18.out
test/langtools/tools/javac/resolve/BrokenAnonymous.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Thu Feb 22 11:16:43 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java	Wed Feb 21 17:07:12 2018 +0100
@@ -2193,8 +2193,7 @@
         List<Type> argtypes = argtypesBuf.toList();
         List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv);
 
-        // If we have made no mistakes in the class type...
-        if (clazztype.hasTag(CLASS)) {
+        if (clazztype.hasTag(CLASS) || clazztype.hasTag(ERROR)) {
             // Enums may not be instantiated except implicitly
             if ((clazztype.tsym.flags_field & Flags.ENUM) != 0 &&
                 (!env.tree.hasTag(VARDEF) ||
@@ -2381,7 +2380,8 @@
                 // If we already errored, be careful to avoid a further avalanche. ErrorType answers
                 // false for isInterface call even when the original type is an interface.
                 boolean implementing = clazztype.tsym.isInterface() ||
-                        clazztype.isErroneous() && clazztype.getOriginalType().tsym.isInterface();
+                        clazztype.isErroneous() && !clazztype.getOriginalType().hasTag(NONE) &&
+                        clazztype.getOriginalType().tsym.isInterface();
 
                 if (implementing) {
                     cdef.implementing = List.of(clazz);
@@ -2413,7 +2413,8 @@
                     finalargtypes = finalargtypes.map(deferredAttr.deferredCopier);
                 }
 
-                clazztype = cdef.sym.type;
+                clazztype = clazztype.hasTag(ERROR) ? types.createErrorType(cdef.sym.type)
+                                                    : cdef.sym.type;
                 Symbol sym = tree.constructor = rs.resolveConstructor(
                         tree.pos(), localEnv, clazztype, finalargtypes, typeargtypes);
                 Assert.check(!sym.kind.isResolutionError());
@@ -5080,7 +5081,7 @@
          */
         private Type dummyMethodType(JCMethodDecl md) {
             Type restype = syms.unknownType;
-            if (md != null && md.restype.hasTag(TYPEIDENT)) {
+            if (md != null && md.restype != null && md.restype.hasTag(TYPEIDENT)) {
                 JCPrimitiveTypeTree prim = (JCPrimitiveTypeTree)md.restype;
                 if (prim.typetag == VOID)
                     restype = syms.voidType;
--- a/test/langtools/jdk/jshell/CompletionSuggestionTest.java	Thu Feb 22 11:16:43 2018 +0100
+++ b/test/langtools/jdk/jshell/CompletionSuggestionTest.java	Wed Feb 21 17:07:12 2018 +0100
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8131025 8141092 8153761 8145263 8131019 8175886 8176184 8176241 8176110 8177466
+ * @bug 8131025 8141092 8153761 8145263 8131019 8175886 8176184 8176241 8176110 8177466 8197439
  * @summary Test Completion and Documentation
  * @library /tools/lib
  * @modules jdk.compiler/com.sun.tools.javac.api
@@ -661,6 +661,10 @@
         assertCompletion("CharSequence r = |", true);
     }
 
+    public void testCompletionInAnonymous() {
+        assertCompletionIncludesExcludes("new Undefined() { int i = \"\".l|", Set.of("length()"), Set.of());
+    }
+
     @BeforeMethod
     public void setUp() {
         super.setUp();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/analyzer/AnonymousInAnonymous.java	Wed Feb 21 17:07:12 2018 +0100
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018, 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 8197439
+ * @summary Check anonymous class in anonymous class, where the nested one becomes
+ *          unresolvable with lambda conversion.
+ * @compile/ref=AnonymousInAnonymous.out -XDfind=lambda -XDrawDiagnostics AnonymousInAnonymous.java
+ */
+
+public class AnonymousInAnonymous {
+    static void s(I1 i) {}
+    static {
+        s(
+            new I1() {
+                public I2 get() {
+                    return new I2() {
+                    };
+                }
+            });
+    }
+    public static interface I1 {
+        public static class I2 { }
+        public I2 get();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/analyzer/AnonymousInAnonymous.out	Wed Feb 21 17:07:12 2018 +0100
@@ -0,0 +1,2 @@
+AnonymousInAnonymous.java:36:22: compiler.warn.potential.lambda.found
+1 warning
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/failover/PostAttrConstructor.java	Wed Feb 21 17:07:12 2018 +0100
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2018, 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 8197439
+ * @summary Ensure that constructors don't cause crash in Attr.postAttr
+ * @modules jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.comp
+ *          jdk.compiler/com.sun.tools.javac.tree
+ */
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+import javax.tools.*;
+
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.tools.javac.api.JavacTaskImpl;
+import com.sun.tools.javac.comp.Attr;
+import com.sun.tools.javac.tree.JCTree;
+
+public class PostAttrConstructor {
+
+    static class JavaSource extends SimpleJavaFileObject {
+
+        final static String source =
+                        "class C {\n" +
+                        "    public C() {}\n" +
+                        "}";
+
+        JavaSource() {
+            super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
+        }
+
+        @Override
+        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+            return source;
+        }
+    }
+
+    public static void main(String... args) throws IOException {
+        new PostAttrConstructor().run();
+    }
+
+    void run() throws IOException {
+        File destDir = new File("classes"); destDir.mkdir();
+        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+        JavaSource source = new JavaSource();
+        JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, null, null,
+                Arrays.asList("-d", destDir.getPath()),
+                null,
+                Arrays.asList(source));
+        CompilationUnitTree cut = ct.parse().iterator().next();
+        Attr attr = Attr.instance(ct.getContext());
+        attr.postAttr((JCTree) cut);
+    }
+
+}
--- a/test/langtools/tools/javac/generics/diamond/neg/Neg05.out	Thu Feb 22 11:16:43 2018 +0100
+++ b/test/langtools/tools/javac/generics/diamond/neg/Neg05.out	Wed Feb 21 17:07:12 2018 +0100
@@ -7,21 +7,13 @@
 Neg05.java:26:43: compiler.err.improperly.formed.type.inner.raw.param
 Neg05.java:27:56: compiler.err.improperly.formed.type.inner.raw.param
 Neg05.java:29:48: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:29:35: compiler.err.cant.apply.symbol: kindname.constructor, , V, java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:30:59: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:30:46: compiler.err.cant.apply.symbol: kindname.constructor, , V, java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:31:44: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:31:31: compiler.err.cant.apply.symbol: kindname.constructor, , V, java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:32:57: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:32:44: compiler.err.cant.apply.symbol: kindname.constructor, , V, java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:34:49: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:34:36: compiler.err.cant.apply.symbol: kindname.constructor, , V,Z, java.lang.String,java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.infer.no.conforming.assignment.exists: Z, (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:35:59: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:35:46: compiler.err.cant.apply.symbol: kindname.constructor, , V,Z, java.lang.String,java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.infer.no.conforming.assignment.exists: Z, (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:36:44: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:36:31: compiler.err.cant.apply.symbol: kindname.constructor, , V,Z, java.lang.String,java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.infer.no.conforming.assignment.exists: Z, (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:37:57: compiler.err.improperly.formed.type.inner.raw.param
-Neg05.java:37:44: compiler.err.cant.apply.symbol: kindname.constructor, , V,Z, java.lang.String,java.lang.String, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.infer.no.conforming.assignment.exists: Z, (compiler.misc.inconvertible.types: java.lang.String, V))
 Neg05.java:41:37: compiler.err.improperly.formed.type.inner.raw.param
 Neg05.java:41:44: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: Neg05.Foo), (compiler.misc.infer.no.conforming.instance.exists: V, Neg05.Foo<V>, Neg05<?>.Foo<java.lang.String>))
 Neg05.java:42:47: compiler.err.improperly.formed.type.inner.raw.param
@@ -54,4 +46,4 @@
 Neg05.java:58:40: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: Neg05.Foo), (compiler.misc.infer.no.conforming.instance.exists: V,Z, Neg05.Foo<V>, Neg05<?>.Foo<?>))
 Neg05.java:59:46: compiler.err.improperly.formed.type.inner.raw.param
 Neg05.java:59:53: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: Neg05.Foo), (compiler.misc.infer.no.conforming.instance.exists: V,Z, Neg05.Foo<V>, Neg05<?>.Foo<? super java.lang.String>))
-56 errors
+48 errors
--- a/test/langtools/tools/javac/generics/diamond/neg/Neg06.out	Thu Feb 22 11:16:43 2018 +0100
+++ b/test/langtools/tools/javac/generics/diamond/neg/Neg06.out	Wed Feb 21 17:07:12 2018 +0100
@@ -1,6 +1,4 @@
 Neg06.java:18:36: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: Neg06.IFoo), (compiler.misc.incompatible.eq.upper.bounds: X, java.lang.String, java.lang.Number))
-Neg06.java:18:28: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: java.lang.Object, Neg06.ISuperFoo<java.lang.String>)
 Neg06.java:19:37: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: Neg06.CFoo), (compiler.misc.incompatible.eq.upper.bounds: X, java.lang.String, java.lang.Number))
 Neg06.java:20:37: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: Neg06.CFoo), (compiler.misc.incompatible.eq.upper.bounds: X, java.lang.String, java.lang.Number))
-Neg06.java:20:29: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: compiler.misc.anonymous.class: <any>, Neg06.CSuperFoo<java.lang.String>)
-5 errors
+3 errors
--- a/test/langtools/tools/javac/generics/diamond/neg/Neg18.out	Thu Feb 22 11:16:43 2018 +0100
+++ b/test/langtools/tools/javac/generics/diamond/neg/Neg18.out	Wed Feb 21 17:07:12 2018 +0100
@@ -1,3 +1,2 @@
 Neg18.java:14:21: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: pkg.Neg18_01), (compiler.misc.inaccessible.varargs.type: pkg.Neg18_01.PkgPrivate, kindname.class, Neg18))
-Neg18.java:14:9: compiler.err.cant.apply.symbol: kindname.constructor, , pkg.Neg18_01.PkgPrivate[], compiler.misc.no.args, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.inaccessible.varargs.type: pkg.Neg18_01.PkgPrivate, kindname.class, Neg18)
-2 errors
+1 error
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/resolve/BrokenAnonymous.java	Wed Feb 21 17:07:12 2018 +0100
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2018, 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 8197439
+ * @summary Ensuring that unresolvable anonymous class is still attributed.
+ * @modules jdk.compiler
+ */
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+import javax.tools.*;
+
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.IdentifierTree;
+import com.sun.source.tree.MethodTree;
+import com.sun.source.tree.VariableTree;
+import com.sun.source.util.*;
+
+public class BrokenAnonymous {
+
+    static class JavaSource extends SimpleJavaFileObject {
+
+        final static String source =
+                        "class C {\n" +
+                        "    Object o1 = new Undef1() {" +
+                        "        int i = 0;\n" +
+                        "        int m(int j) { return i + j; }\n" +
+                        "    }\n" +
+                        "    Object o2 = new Undef2<>() {" +
+                        "        int i = 0;\n" +
+                        "        int m(int j) { return i + j; }\n" +
+                        "    }\n" +
+                        "}";
+
+        JavaSource() {
+            super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
+        }
+
+        @Override
+        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+            return source;
+        }
+    }
+
+    public static void main(String... args) throws IOException {
+        new BrokenAnonymous().run();
+    }
+
+    void run() throws IOException {
+        File destDir = new File("classes"); destDir.mkdir();
+        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+        JavaSource source = new JavaSource();
+        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
+                Arrays.asList("-d", destDir.getPath()),
+                null,
+                Arrays.asList(source));
+        CompilationUnitTree cut = ct.parse().iterator().next();
+        Trees trees = Trees.instance(ct);
+        ct.analyze();
+        new TreePathScanner<Void, Void>() {
+            @Override
+            public Void visitVariable(VariableTree node, Void p) {
+                verifyElementType();
+                return super.visitVariable(node, p);
+            }
+            @Override
+            public Void visitMethod(MethodTree node, Void p) {
+                verifyElementType();
+                return super.visitMethod(node, p);
+            }
+            @Override
+            public Void visitIdentifier(IdentifierTree node, Void p) {
+                verifyElementType();
+                return super.visitIdentifier(node, p);
+            }
+            private void verifyElementType() {
+                TreePath tp = getCurrentPath();
+                assertNotNull(trees.getElement(tp));
+                assertNotNull(trees.getTypeMirror(tp));
+            }
+        }.scan(cut, null);
+    }
+
+    private void assertNotNull(Object obj) {
+        if (obj == null) {
+            throw new AssertionError("Unexpected null value.");
+        }
+    }
+}