Skip to content

Commit

Permalink
8322159: ThisEscapeAnalyzer crashes for erroneous code
Browse files Browse the repository at this point in the history
Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jan 2, 2024
1 parent d4fb308 commit 7455b1b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public void visitMethodDef(JCMethodDecl tree) {
public void visitApply(JCMethodInvocation invoke) {

// Get method symbol
MethodSymbol sym = (MethodSymbol)TreeInfo.symbolFor(invoke.meth);
Symbol sym = TreeInfo.symbolFor(invoke.meth);

// Recurse on method expression
scan(invoke.meth);
Expand All @@ -530,7 +530,7 @@ public void visitApply(JCMethodInvocation invoke) {
invoke(invoke, sym, invoke.args, receiverRefs);
}

private void invoke(JCTree site, MethodSymbol sym, List<JCExpression> args, RefSet<?> receiverRefs) {
private void invoke(JCTree site, Symbol sym, List<JCExpression> args, RefSet<?> receiverRefs) {

// Skip if ignoring warnings for a constructor invoked via 'this()'
if (suppressed.contains(sym))
Expand Down Expand Up @@ -810,6 +810,10 @@ public void visitSelect(JCFieldAccess tree) {

@Override
public void visitReference(JCMemberReference tree) {
if (tree.type.isErroneous()) {
//error recovery - ignore erroneous member references
return ;
}

// Scan target expression and extract 'this' references, if any
scan(tree.expr);
Expand Down
39 changes: 38 additions & 1 deletion test/langtools/tools/javac/recovery/AttrRecovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8301580
* @bug 8301580 8322159
* @summary Verify error recovery w.r.t. Attr
* @library /tools/lib
* @enablePreview
Expand Down Expand Up @@ -87,4 +87,41 @@ class C {
}
}

@Test
public void testX() throws Exception {
String code = """
public class C {
public C() {
Undefined.method();
undefined1();
Runnable r = this::undefined2;
overridable(this); //to verify ThisEscapeAnalyzer has been run
}
public void overridable(C c) {}
}
""";
Path curPath = Path.of(".");
List<String> actual = new JavacTask(tb)
.options("-XDrawDiagnostics", "-XDdev",
"-XDshould-stop.at=FLOW", "-Xlint:this-escape")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL)
.writeAll()
.getOutputLines(OutputKind.DIRECT);

List<String> expected = List.of(
"C.java:3:9: compiler.err.cant.resolve.location: kindname.variable, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
"C.java:4:9: compiler.err.cant.resolve.location.args: kindname.method, undefined1, , , (compiler.misc.location: kindname.class, C, null)",
"C.java:5:22: compiler.err.invalid.mref: kindname.method, (compiler.misc.cant.resolve.location.args: kindname.method, undefined2, , , (compiler.misc.location: kindname.class, C, null))",
"C.java:6:20: compiler.warn.possible.this.escape",
"3 errors",
"1 warning"
);

if (!Objects.equals(actual, expected)) {
error("Expected: " + expected + ", but got: " + actual);
}
}

}

1 comment on commit 7455b1b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.