Skip to content

Commit 2fa3edd

Browse files
committed
8177068: incomplete classpath causes NPE in Flow
Undo completions that failed during speculative attribution, so that the appropriate CompletionFailures are thrown again and properly reported. Reviewed-by: vromero
1 parent 5c4be9c commit 2fa3edd

File tree

4 files changed

+132
-31
lines changed

4 files changed

+132
-31
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/DeferredCompletionFailureHandler.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ public void uninstall() {
8686
}
8787
};
8888

89+
public final Handler speculativeCodeHandler = new Handler() {
90+
private final Map<ClassSymbol, FlipSymbolDescription> class2Flip = new HashMap<>();
91+
92+
public void install() {
93+
}
94+
public void handleAPICompletionFailure(CompletionFailure cf) {
95+
throw cf;
96+
}
97+
public void classSymbolCompleteFailed(ClassSymbol sym, Completer origCompleter) {
98+
class2Flip.put(sym, new FlipSymbolDescription(sym, new DeferredCompleter(origCompleter)));
99+
}
100+
public void classSymbolRemoved(ClassSymbol sym) {
101+
class2Flip.remove(sym);
102+
}
103+
public void uninstall() {
104+
class2Flip.values().forEach(f -> f.flip());
105+
class2Flip.clear();
106+
}
107+
};
108+
89109
public final Handler javacCodeHandler = new Handler() {
90110
public void install() {
91111
}

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,6 +2774,8 @@ public void visitLambda(final JCLambda that) {
27742774
resultInfo.checkContext.report(that, cause);
27752775
result = that.type = types.createErrorType(pt());
27762776
return;
2777+
} catch (CompletionFailure cf) {
2778+
chk.completionError(that.pos(), cf);
27772779
} catch (Throwable t) {
27782780
//when an unexpected exception happens, avoid attempts to attribute the same tree again
27792781
//as that would likely cause the same exception again.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/DeferredAttr.java

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public class DeferredAttr extends JCTree.Visitor {
9696
final Flow flow;
9797
final Names names;
9898
final TypeEnvs typeEnvs;
99+
final DeferredCompletionFailureHandler dcfh;
99100

100101
public static DeferredAttr instance(Context context) {
101102
DeferredAttr instance = context.get(deferredAttrKey);
@@ -121,6 +122,7 @@ protected DeferredAttr(Context context) {
121122
names = Names.instance(context);
122123
stuckTree = make.Ident(names.empty).setType(Type.stuckType);
123124
typeEnvs = TypeEnvs.instance(context);
125+
dcfh = DeferredCompletionFailureHandler.instance(context);
124126
emptyDeferredAttrContext =
125127
new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, infer.emptyContext, null, null) {
126128
@Override
@@ -481,12 +483,12 @@ JCLambda attribSpeculativeLambda(JCLambda that, Env<AttrContext> env, ResultInfo
481483
*/
482484
JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
483485
return attribSpeculative(tree, env, resultInfo, treeCopier,
484-
(newTree)->new DeferredAttrDiagHandler(log, newTree), AttributionMode.SPECULATIVE, null);
486+
newTree->new DeferredDiagnosticHandler(log), AttributionMode.SPECULATIVE, null);
485487
}
486488

487489
JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, LocalCacheContext localCache) {
488490
return attribSpeculative(tree, env, resultInfo, treeCopier,
489-
(newTree)->new DeferredAttrDiagHandler(log, newTree), AttributionMode.SPECULATIVE, localCache);
491+
newTree->new DeferredDiagnosticHandler(log), AttributionMode.SPECULATIVE, localCache);
490492
}
491493

492494
<Z> JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, TreeCopier<Z> deferredCopier,
@@ -496,12 +498,14 @@ <Z> JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resul
496498
Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
497499
speculativeEnv.info.attributionMode = attributionMode;
498500
Log.DeferredDiagnosticHandler deferredDiagnosticHandler = diagHandlerCreator.apply(newTree);
501+
DeferredCompletionFailureHandler.Handler prevCFHandler = dcfh.setHandler(dcfh.speculativeCodeHandler);
499502
int nwarnings = log.nwarnings;
500503
log.nwarnings = 0;
501504
try {
502505
attr.attribTree(newTree, speculativeEnv, resultInfo);
503506
return newTree;
504507
} finally {
508+
dcfh.setHandler(prevCFHandler);
505509
log.nwarnings += nwarnings;
506510
enter.unenter(env.toplevel, newTree);
507511
log.popDiagnosticHandler(deferredDiagnosticHandler);
@@ -510,35 +514,6 @@ <Z> JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resul
510514
}
511515
}
512516
}
513-
//where
514-
static class DeferredAttrDiagHandler extends Log.DeferredDiagnosticHandler {
515-
516-
static class PosScanner extends TreeScanner {
517-
DiagnosticPosition pos;
518-
boolean found = false;
519-
520-
PosScanner(DiagnosticPosition pos) {
521-
this.pos = pos;
522-
}
523-
524-
@Override
525-
public void scan(JCTree tree) {
526-
if (tree != null &&
527-
tree.pos() == pos) {
528-
found = true;
529-
}
530-
super.scan(tree);
531-
}
532-
}
533-
534-
DeferredAttrDiagHandler(Log log, JCTree newTree) {
535-
super(log, d -> {
536-
PosScanner posScanner = new PosScanner(d.getDiagnosticPosition());
537-
posScanner.scan(newTree);
538-
return posScanner.found;
539-
});
540-
}
541-
}
542517

543518
/**
544519
* A deferred context is created on each method check. A deferred context is
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
73+
public static void main(String[] args) throws Exception {
74+
new NoCompletionFailureSkipOnSpeculativeAttribution().test();
75+
}
76+
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+
}

0 commit comments

Comments
 (0)