mirror of
https://github.com/JetBrains/JetBrainsRuntime.git
synced 2025-12-06 09:29:38 +01:00
8315452: Erroneous AST missing modifiers for partial input
Reviewed-by: asotona, vromero
Backport-of: 84425a6290
This commit is contained in:
committed by
Vitaly Provodin
parent
2046a7ef84
commit
34d982cc74
@@ -3918,7 +3918,7 @@ public class JavacParser implements Parser {
|
||||
|
||||
boolean firstTypeDecl = true; // have we see a class, enum, or interface declaration yet?
|
||||
boolean isUnnamedClass = false;
|
||||
while (token.kind != EOF) {
|
||||
OUTER: while (token.kind != EOF) {
|
||||
if (token.pos <= endPosTable.errorEndPos) {
|
||||
// error recovery
|
||||
skip(firstTypeDecl, false, false, false);
|
||||
@@ -3933,6 +3933,8 @@ public class JavacParser implements Parser {
|
||||
while (firstTypeDecl && mods == null && token.kind == SEMI) {
|
||||
semiList.append(toP(F.at(token.pos).Skip()));
|
||||
nextToken();
|
||||
if (token.kind == EOF)
|
||||
break OUTER;
|
||||
}
|
||||
if (firstTypeDecl && mods == null && token.kind == IMPORT) {
|
||||
if (!semiList.isEmpty()) {
|
||||
@@ -3999,7 +4001,7 @@ public class JavacParser implements Parser {
|
||||
checkSourceLevel(token.pos, Feature.UNNAMED_CLASSES);
|
||||
defs.appendList(topLevelMethodOrFieldDeclaration(mods));
|
||||
isUnnamedClass = true;
|
||||
} else if (token.kind != EOF) {
|
||||
} else {
|
||||
JCTree def = typeDeclaration(mods, docComment);
|
||||
if (def instanceof JCExpressionStatement statement)
|
||||
def = statement.expr;
|
||||
|
||||
@@ -23,7 +23,8 @@
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8312093
|
||||
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451 8237041 8253584 8246774 8256411 8256149 8259050 8266436 8267221 8271928 8275097 8293897 8295401 8304671 8312093 8315452
|
||||
|
||||
* @summary tests error and diagnostics positions
|
||||
* @author Jan Lahoda
|
||||
* @modules jdk.compiler/com.sun.tools.javac.api
|
||||
@@ -60,6 +61,8 @@ import com.sun.tools.javac.main.Main.Result;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -70,6 +73,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
@@ -88,7 +92,8 @@ import com.sun.source.tree.ModuleTree;
|
||||
import com.sun.source.util.TreePathScanner;
|
||||
import com.sun.tools.javac.api.JavacTaskPool;
|
||||
import com.sun.tools.javac.api.JavacTaskPool.Worker;
|
||||
import java.util.Objects;
|
||||
import com.sun.tools.javac.tree.JCTree.JCErroneous;
|
||||
import com.sun.tools.javac.tree.Pretty;
|
||||
|
||||
public class JavacParserTest extends TestCase {
|
||||
static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
|
||||
@@ -2423,6 +2428,28 @@ public class JavacParserTest extends TestCase {
|
||||
}.scan(cut, null);
|
||||
}
|
||||
|
||||
@Test //JDK-8315452
|
||||
void testPartialTopLevelModifiers() throws IOException {
|
||||
String code = """
|
||||
package test;
|
||||
public
|
||||
""";
|
||||
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();
|
||||
|
||||
String result = toStringWithErrors(cut).replaceAll("\\R", "\n");
|
||||
System.out.println("RESULT\n" + result);
|
||||
assertEquals("incorrect AST",
|
||||
result,
|
||||
"""
|
||||
package test;
|
||||
(ERROR: public )""");
|
||||
}
|
||||
|
||||
void run(String[] args) throws Exception {
|
||||
int passed = 0, failed = 0;
|
||||
final Pattern p = (args != null && args.length > 0)
|
||||
@@ -2452,6 +2479,38 @@ public class JavacParserTest extends TestCase {
|
||||
passed + ", failed = " + failed + " ??????????");
|
||||
}
|
||||
}
|
||||
|
||||
private String toStringWithErrors(Tree tree) {
|
||||
StringWriter s = new StringWriter();
|
||||
try {
|
||||
new PrettyWithErrors(s, false).printExpr((JCTree) tree);
|
||||
} catch (IOException e) {
|
||||
// should never happen, because StringWriter is defined
|
||||
// never to throw any IOExceptions
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
private static final class PrettyWithErrors extends Pretty {
|
||||
|
||||
public PrettyWithErrors(Writer out, boolean sourceOutput) {
|
||||
super(out, sourceOutput);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitErroneous(JCErroneous tree) {
|
||||
try {
|
||||
print("(ERROR: ");
|
||||
print(tree.errs);
|
||||
print(")");
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract class TestCase {
|
||||
|
||||
Reference in New Issue
Block a user