Skip to content

Commit

Permalink
8333107: javac fails with an exception when processing broken lambda
Browse files Browse the repository at this point in the history
Reviewed-by: asotona
  • Loading branch information
lahodaj committed May 30, 2024
1 parent 921860d commit 4acafb8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -151,10 +151,12 @@ public <T extends JCTree> T translate(T t) {
@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)) {
Expand Down
48 changes: 47 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 8322159 8332230
* @bug 8301580 8322159 8333107 8332230
* @summary Verify error recovery w.r.t. Attr
* @library /tools/lib
* @enablePreview
Expand Down Expand Up @@ -157,4 +157,50 @@ public void t() {
}
}

@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);
}
}

}

0 comments on commit 4acafb8

Please sign in to comment.