8333107: javac fails with an exception when processing broken lambda

Reviewed-by: asotona
This commit is contained in:
Jan Lahoda 2024-05-30 13:53:27 +00:00
parent 921860d41d
commit 4acafb809c
2 changed files with 50 additions and 2 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac/recovery

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, 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
@ -151,10 +151,12 @@ public class AttrRecover {
@Override
public void visitLambda(JCLambda tree) {
//do not touch nested lambdas
result = tree;
}
@Override
public void visitClassDef(JCClassDecl tree) {
//do not touch nested classes
result = tree;
}
}.translate(lambda.body);
if (!voidCompatible && lambda.body.hasTag(Tag.BLOCK)) {

@ -23,7 +23,7 @@
/*
* @test
* @bug 8301580 8322159 8332230
* @bug 8301580 8322159 8333107 8332230
* @summary Verify error recovery w.r.t. Attr
* @library /tools/lib
* @enablePreview
@ -157,4 +157,50 @@ public class AttrRecovery extends TestRunner {
}
}
@Test //JDK-8333107
public void testNestedLambda() throws Exception {
String code = """
public class Dummy {
private void main() {
Stream l = null;
l.map(a -> {
l.map(b -> {
return null;
});
l.map(new FI() {
public String convert(String s) {
return null;
}
});
class Local {}
});
}
public interface Stream {
public void map(FI fi);
}
public interface FI {
public String convert(String s);
}
}
""";
Path curPath = Path.of(".");
List<String> actual = new JavacTask(tb)
.options("-XDrawDiagnostics", "-XDdev",
"-XDshould-stop.at=FLOW")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL)
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of(
"Dummy.java:4:10: compiler.err.cant.apply.symbol: kindname.method, map, Dummy.FI, @15, kindname.interface, Dummy.Stream, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.incompatible.ret.type.in.lambda: (compiler.misc.missing.ret.val: java.lang.String)))",
"1 error"
);
if (!Objects.equals(actual, expected)) {
error("Expected: " + expected + ", but got: " + actual);
}
}
}