8351260: java.lang.AssertionError: Unexpected type tree: (ERROR) = (ERROR)

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2025-09-08 06:33:30 +00:00
parent b0ca9bf61e
commit f9dc640ef0
3 changed files with 92 additions and 2 deletions

View File

@ -1104,6 +1104,11 @@ public class JavacParser implements Parser {
syntaxError(result.pos, Errors.RestrictedTypeNotAllowedHere(restrictedTypeName));
}
if ((lastmode & TYPE) == 0) {
//if the mode was switched to expression while expecting type, wrap with Erroneous:
result = F.Erroneous(List.of(result));
}
return result;
}
@ -1431,6 +1436,7 @@ public class JavacParser implements Parser {
protected JCExpression term3() {
int pos = token.pos;
JCExpression t;
int startMode = mode;
List<JCExpression> typeArgs = typeArgumentsOpt(EXPR);
switch (token.kind) {
case QUES:
@ -1760,6 +1766,9 @@ public class JavacParser implements Parser {
}
// Not reachable.
default:
if (typeArgs != null && (startMode & TYPE) != 0) {
return F.at(pos).TypeApply(F.Erroneous(), typeArgs);
}
return illegal();
}
return term3Rest(t, typeArgs);

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204 8315452 8337976 8324859 8344706
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8310326 8312093 8312204 8315452 8337976 8324859 8344706 8351260
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@ -3013,6 +3013,69 @@ public class JavacParserTest extends TestCase {
}""");
}
@Test //JDK-8351260
void testVeryBrokenTypeWithAnnotations() throws IOException {
String code = """
package tests;
class ListUtilsTest {
void test(List<@AlphaChars <@StringLength(int value = 5)String> s){
}
}
""";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
List.of("--enable-preview", "--source", SOURCE_VERSION),
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
List<String> codes = new LinkedList<>();
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
}
assertEquals("testVeryBrokenTypeWithAnnotations: " + codes,
List.of("3:32:compiler.err.illegal.start.of.type",
"3:51:compiler.err.dot.class.expected",
"3:57:compiler.err.expected2",
"3:60:compiler.err.expected2",
"3:61:compiler.err.expected2",
"3:67:compiler.err.not.stmt",
"3:70:compiler.err.expected",
"5:2:compiler.err.premature.eof"),
codes);
String result = toStringWithErrors(cut).replaceAll("\\R", "\n");
System.out.println("RESULT\n" + result);
assertEquals("incorrect AST",
result,
"""
package tests;
\n\
class ListUtilsTest {
\n\
void test(List<@AlphaChars (ERROR: (ERROR)<@StringLength(int) value, (ERROR)> = 5), (ERROR: )> <error>) {
(ERROR: String > s);
{
}
}
}""");
}
@Test //JDK-8351260
void testVeryBrokenTypeWithAnnotationsMinimal() throws IOException {
String code = """
B<@C<@D(e f=
""";
DiagnosticCollector<JavaFileObject> coll =
new DiagnosticCollector<>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
List.of("--enable-preview", "--source", SOURCE_VERSION),
null, Arrays.asList(new MyFileObject(code)));
//no exceptions:
ct.parse().iterator().next();
}
void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8301580 8322159 8333107 8332230 8338678
* @bug 8301580 8322159 8333107 8332230 8338678 8351260
* @summary Verify error recovery w.r.t. Attr
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
@ -322,4 +322,22 @@ public class AttrRecovery extends TestRunner {
}
}
@Test //JDK-8351260
public void testVeryBrokenAnnotation() throws Exception {
String code = """
class ListUtilsTest {
void test(List<@AlphaChars <@StringLength(int value = 5)String> s){
}
}
""";
Path curPath = Path.of(".");
//should not fail with an exception:
new JavacTask(tb)
.options("-XDrawDiagnostics",
"-XDshould-stop.at=FLOW")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL)
.writeAll();
}
}