1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8177068
27  * @summary CompletionFailures occurring during speculative attribution should
28  *          not be lost forever.
29  * @library /tools/lib
30  * @modules jdk.compiler/com.sun.tools.javac.api
31  *          jdk.compiler/com.sun.tools.javac.main
32  *          jdk.compiler/com.sun.tools.javac.util
33  * @build toolbox.ToolBox
34  * @run main NoCompletionFailureSkipOnSpeculativeAttribution
35  */
36 
37 import java.io.File;
38 import java.nio.file.Paths;
39 import java.util.List;
40 
41 import com.sun.tools.javac.util.Assert;
42 
43 import toolbox.JavacTask;
44 import toolbox.Task;
45 import toolbox.ToolBox;
46 
47 public class NoCompletionFailureSkipOnSpeculativeAttribution {
48 
49     private static final String TSrc =
50         "import one.A;\n" +
51         "class T {\n" +
52         "  {\n" +
53         "    System.err.println(two.C.D.g());\n" +
54         "  }\n" +
55         "}";
56 
57     private static final String CSrc =
58         "package two;\n" +
59         "public class C {\n" +
60         "  public static class D {\n" +
61         "    public static int g() {\n" +
62         "      return 1;\n" +
63         "    }\n" +
64         "  }\n" +
65         "}";
66 
67     private static final String ASrc =
68         "package one;\n" +
69         "public class A {\n" +
70         "  public A(two.C.D x) {}\n" +
71         "}";
72 
main(String[] args)73     public static void main(String[] args) throws Exception {
74         new NoCompletionFailureSkipOnSpeculativeAttribution().test();
75     }
76 
test()77     public void test() throws Exception {
78         ToolBox tb = new ToolBox();
79         tb.writeJavaFiles(Paths.get("."), ASrc, CSrc, TSrc);
80 
81         new JavacTask(tb)
82                 .classpath(".")
83                 .files("T.java")
84                 .run();
85 
86         tb.deleteFiles("two/C.class", "two/C$D.class");
87 
88         List<String> output = new JavacTask(tb)
89                 .sourcepath(File.pathSeparator)
90                 .options("-XDrawDiagnostics", "-XDshould-stop.ifError=FLOW")
91                 .classpath(".")
92                 .files("T.java")
93                 .run(Task.Expect.FAIL)
94                 .writeAll()
95                 .getOutputLines(Task.OutputKind.DIRECT);
96 
97         List<String> expectedOutput = List.of(
98                 "T.java:4:29: compiler.err.cant.access: two.C.D, (compiler.misc.class.file.not.found: two.C$D)",
99                 "1 error"
100         );
101 
102         Assert.check(output.equals(expectedOutput));
103     }
104 }
105