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
This commit is contained in:
Jan Lahoda 2019-09-05 12:39:48 +02:00
parent 5c4be9cc8a
commit 2fa3eddd3c
4 changed files with 132 additions and 31 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac/T8177068

@ -86,6 +86,26 @@ public class DeferredCompletionFailureHandler {
}
};
public final Handler speculativeCodeHandler = new Handler() {
private final Map<ClassSymbol, FlipSymbolDescription> class2Flip = new HashMap<>();
public void install() {
}
public void handleAPICompletionFailure(CompletionFailure cf) {
throw cf;
}
public void classSymbolCompleteFailed(ClassSymbol sym, Completer origCompleter) {
class2Flip.put(sym, new FlipSymbolDescription(sym, new DeferredCompleter(origCompleter)));
}
public void classSymbolRemoved(ClassSymbol sym) {
class2Flip.remove(sym);
}
public void uninstall() {
class2Flip.values().forEach(f -> f.flip());
class2Flip.clear();
}
};
public final Handler javacCodeHandler = new Handler() {
public void install() {
}

@ -2774,6 +2774,8 @@ public class Attr extends JCTree.Visitor {
resultInfo.checkContext.report(that, cause);
result = that.type = types.createErrorType(pt());
return;
} catch (CompletionFailure cf) {
chk.completionError(that.pos(), cf);
} catch (Throwable t) {
//when an unexpected exception happens, avoid attempts to attribute the same tree again
//as that would likely cause the same exception again.

@ -96,6 +96,7 @@ public class DeferredAttr extends JCTree.Visitor {
final Flow flow;
final Names names;
final TypeEnvs typeEnvs;
final DeferredCompletionFailureHandler dcfh;
public static DeferredAttr instance(Context context) {
DeferredAttr instance = context.get(deferredAttrKey);
@ -121,6 +122,7 @@ public class DeferredAttr extends JCTree.Visitor {
names = Names.instance(context);
stuckTree = make.Ident(names.empty).setType(Type.stuckType);
typeEnvs = TypeEnvs.instance(context);
dcfh = DeferredCompletionFailureHandler.instance(context);
emptyDeferredAttrContext =
new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, infer.emptyContext, null, null) {
@Override
@ -481,12 +483,12 @@ public class DeferredAttr extends JCTree.Visitor {
*/
JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
return attribSpeculative(tree, env, resultInfo, treeCopier,
(newTree)->new DeferredAttrDiagHandler(log, newTree), AttributionMode.SPECULATIVE, null);
newTree->new DeferredDiagnosticHandler(log), AttributionMode.SPECULATIVE, null);
}
JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, LocalCacheContext localCache) {
return attribSpeculative(tree, env, resultInfo, treeCopier,
(newTree)->new DeferredAttrDiagHandler(log, newTree), AttributionMode.SPECULATIVE, localCache);
newTree->new DeferredDiagnosticHandler(log), AttributionMode.SPECULATIVE, localCache);
}
<Z> JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, TreeCopier<Z> deferredCopier,
@ -496,12 +498,14 @@ public class DeferredAttr extends JCTree.Visitor {
Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
speculativeEnv.info.attributionMode = attributionMode;
Log.DeferredDiagnosticHandler deferredDiagnosticHandler = diagHandlerCreator.apply(newTree);
DeferredCompletionFailureHandler.Handler prevCFHandler = dcfh.setHandler(dcfh.speculativeCodeHandler);
int nwarnings = log.nwarnings;
log.nwarnings = 0;
try {
attr.attribTree(newTree, speculativeEnv, resultInfo);
return newTree;
} finally {
dcfh.setHandler(prevCFHandler);
log.nwarnings += nwarnings;
enter.unenter(env.toplevel, newTree);
log.popDiagnosticHandler(deferredDiagnosticHandler);
@ -510,35 +514,6 @@ public class DeferredAttr extends JCTree.Visitor {
}
}
}
//where
static class DeferredAttrDiagHandler extends Log.DeferredDiagnosticHandler {
static class PosScanner extends TreeScanner {
DiagnosticPosition pos;
boolean found = false;
PosScanner(DiagnosticPosition pos) {
this.pos = pos;
}
@Override
public void scan(JCTree tree) {
if (tree != null &&
tree.pos() == pos) {
found = true;
}
super.scan(tree);
}
}
DeferredAttrDiagHandler(Log log, JCTree newTree) {
super(log, d -> {
PosScanner posScanner = new PosScanner(d.getDiagnosticPosition());
posScanner.scan(newTree);
return posScanner.found;
});
}
}
/**
* A deferred context is created on each method check. A deferred context is

@ -0,0 +1,104 @@
/*
* Copyright (c) 2019, 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 8177068
* @summary CompletionFailures occurring during speculative attribution should
* not be lost forever.
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.util
* @build toolbox.ToolBox
* @run main NoCompletionFailureSkipOnSpeculativeAttribution
*/
import java.io.File;
import java.nio.file.Paths;
import java.util.List;
import com.sun.tools.javac.util.Assert;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.ToolBox;
public class NoCompletionFailureSkipOnSpeculativeAttribution {
private static final String TSrc =
"import one.A;\n" +
"class T {\n" +
" {\n" +
" System.err.println(two.C.D.g());\n" +
" }\n" +
"}";
private static final String CSrc =
"package two;\n" +
"public class C {\n" +
" public static class D {\n" +
" public static int g() {\n" +
" return 1;\n" +
" }\n" +
" }\n" +
"}";
private static final String ASrc =
"package one;\n" +
"public class A {\n" +
" public A(two.C.D x) {}\n" +
"}";
public static void main(String[] args) throws Exception {
new NoCompletionFailureSkipOnSpeculativeAttribution().test();
}
public void test() throws Exception {
ToolBox tb = new ToolBox();
tb.writeJavaFiles(Paths.get("."), ASrc, CSrc, TSrc);
new JavacTask(tb)
.classpath(".")
.files("T.java")
.run();
tb.deleteFiles("two/C.class", "two/C$D.class");
List<String> output = new JavacTask(tb)
.sourcepath(File.pathSeparator)
.options("-XDrawDiagnostics", "-XDshould-stop.ifError=FLOW")
.classpath(".")
.files("T.java")
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expectedOutput = List.of(
"T.java:4:29: compiler.err.cant.access: two.C.D, (compiler.misc.class.file.not.found: two.C$D)",
"1 error"
);
Assert.check(output.equals(expectedOutput));
}
}