Skip to content

Commit f9dc640

Browse files
committed
8351260: java.lang.AssertionError: Unexpected type tree: (ERROR) = (ERROR)
Reviewed-by: vromero
1 parent b0ca9bf commit f9dc640

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,11 @@ public JCExpression unannotatedType(boolean allowVar, int newmode) {
11041104
syntaxError(result.pos, Errors.RestrictedTypeNotAllowedHere(restrictedTypeName));
11051105
}
11061106

1107+
if ((lastmode & TYPE) == 0) {
1108+
//if the mode was switched to expression while expecting type, wrap with Erroneous:
1109+
result = F.Erroneous(List.of(result));
1110+
}
1111+
11071112
return result;
11081113
}
11091114

@@ -1431,6 +1436,7 @@ private Token[] newOpStack() {
14311436
protected JCExpression term3() {
14321437
int pos = token.pos;
14331438
JCExpression t;
1439+
int startMode = mode;
14341440
List<JCExpression> typeArgs = typeArgumentsOpt(EXPR);
14351441
switch (token.kind) {
14361442
case QUES:
@@ -1760,6 +1766,9 @@ protected JCExpression term3() {
17601766
}
17611767
// Not reachable.
17621768
default:
1769+
if (typeArgs != null && (startMode & TYPE) != 0) {
1770+
return F.at(pos).TypeApply(F.Erroneous(), typeArgs);
1771+
}
17631772
return illegal();
17641773
}
17651774
return term3Rest(t, typeArgs);

test/langtools/tools/javac/parser/JavacParserTest.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @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
26+
* @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
2727
* @summary tests error and diagnostics positions
2828
* @author Jan Lahoda
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -3013,6 +3013,69 @@ public static boolean test2() {
30133013
}""");
30143014
}
30153015

3016+
@Test //JDK-8351260
3017+
void testVeryBrokenTypeWithAnnotations() throws IOException {
3018+
String code = """
3019+
package tests;
3020+
class ListUtilsTest {
3021+
void test(List<@AlphaChars <@StringLength(int value = 5)String> s){
3022+
}
3023+
}
3024+
""";
3025+
DiagnosticCollector<JavaFileObject> coll =
3026+
new DiagnosticCollector<>();
3027+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
3028+
List.of("--enable-preview", "--source", SOURCE_VERSION),
3029+
null, Arrays.asList(new MyFileObject(code)));
3030+
CompilationUnitTree cut = ct.parse().iterator().next();
3031+
3032+
List<String> codes = new LinkedList<>();
3033+
3034+
for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
3035+
codes.add(d.getLineNumber() + ":" + d.getColumnNumber() + ":" + d.getCode());
3036+
}
3037+
3038+
assertEquals("testVeryBrokenTypeWithAnnotations: " + codes,
3039+
List.of("3:32:compiler.err.illegal.start.of.type",
3040+
"3:51:compiler.err.dot.class.expected",
3041+
"3:57:compiler.err.expected2",
3042+
"3:60:compiler.err.expected2",
3043+
"3:61:compiler.err.expected2",
3044+
"3:67:compiler.err.not.stmt",
3045+
"3:70:compiler.err.expected",
3046+
"5:2:compiler.err.premature.eof"),
3047+
codes);
3048+
String result = toStringWithErrors(cut).replaceAll("\\R", "\n");
3049+
System.out.println("RESULT\n" + result);
3050+
assertEquals("incorrect AST",
3051+
result,
3052+
"""
3053+
package tests;
3054+
\n\
3055+
class ListUtilsTest {
3056+
\n\
3057+
void test(List<@AlphaChars (ERROR: (ERROR)<@StringLength(int) value, (ERROR)> = 5), (ERROR: )> <error>) {
3058+
(ERROR: String > s);
3059+
{
3060+
}
3061+
}
3062+
}""");
3063+
}
3064+
3065+
@Test //JDK-8351260
3066+
void testVeryBrokenTypeWithAnnotationsMinimal() throws IOException {
3067+
String code = """
3068+
B<@C<@D(e f=
3069+
""";
3070+
DiagnosticCollector<JavaFileObject> coll =
3071+
new DiagnosticCollector<>();
3072+
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll,
3073+
List.of("--enable-preview", "--source", SOURCE_VERSION),
3074+
null, Arrays.asList(new MyFileObject(code)));
3075+
//no exceptions:
3076+
ct.parse().iterator().next();
3077+
}
3078+
30163079
void run(String[] args) throws Exception {
30173080
int passed = 0, failed = 0;
30183081
final Pattern p = (args != null && args.length > 0)

test/langtools/tools/javac/recovery/AttrRecovery.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8301580 8322159 8333107 8332230 8338678
26+
* @bug 8301580 8322159 8333107 8332230 8338678 8351260
2727
* @summary Verify error recovery w.r.t. Attr
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -322,4 +322,22 @@ String type2String(TypeMirror type) {
322322
}
323323
}
324324

325+
@Test //JDK-8351260
326+
public void testVeryBrokenAnnotation() throws Exception {
327+
String code = """
328+
class ListUtilsTest {
329+
void test(List<@AlphaChars <@StringLength(int value = 5)String> s){
330+
}
331+
}
332+
""";
333+
Path curPath = Path.of(".");
334+
//should not fail with an exception:
335+
new JavacTask(tb)
336+
.options("-XDrawDiagnostics",
337+
"-XDshould-stop.at=FLOW")
338+
.sources(code)
339+
.outdir(curPath)
340+
.run(Expect.FAIL)
341+
.writeAll();
342+
}
325343
}

0 commit comments

Comments
 (0)