8205913: Inconsistent source code model

Ensuring variable declarations have non-null modifiers and names.

Reviewed-by: mcimadamore
This commit is contained in:
Jan Lahoda 2018-06-28 10:05:43 +02:00
parent 2183a72fe3
commit 152488eeee
3 changed files with 39 additions and 5 deletions

View File

@ -2784,7 +2784,7 @@ public class JavacParser implements Parser {
return variableDeclarators(modifiersOpt(), t, stats, true).toList();
} else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
log.error(DiagnosticFlag.SYNTAX, pos, Errors.BadInitializer("for-loop"));
return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null));
return List.of((JCStatement)F.at(pos).VarDef(modifiersOpt(), names.error, t, null));
} else {
return moreStatementExpressions(pos, t, stats).toList();
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2018, 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
@ -198,7 +198,9 @@ public class ToolTabSnippetTest extends UITesting {
//no crash: 8188072
inputSink.write(INTERRUPT + "for (int:" + TAB);
waitOutput(out, PROMPT + "for \\(int:" + BELL);
waitOutput(out, PROMPT + "for \\(int:\n" +
getMessage("jshell.console.completion.all.completions.number", "[0-9]+") +
REDRAW_PROMPT + "for \\(int:");
});
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2018, 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
@ -23,7 +23,7 @@
/*
* @test
* @bug 7073631 7159445 7156633 8028235 8065753
* @bug 7073631 7159445 7156633 8028235 8065753 8205913
* @summary tests error and diagnostics positions
* @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api
@ -49,7 +49,9 @@ import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.source.tree.WhileLoopTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTaskImpl;
@ -1005,6 +1007,36 @@ public class JavacParserTest extends TestCase {
assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors);
}
@Test //JDK-8205913
void testForInit() throws IOException {
String code = "class T { void t() { for (n : ns) { } } }";
String expectedErrors = "Test.java:1:27: compiler.err.bad.initializer: for-loop\n";
StringWriter out = new StringWriter();
JavacTask ct = (JavacTask) tool.getTask(out, fm, null,
Arrays.asList("-XDrawDiagnostics"), null, Arrays.asList(new MyFileObject(code)));
Iterable<? extends CompilationUnitTree> cuts = ct.parse();
boolean[] foundVar = new boolean[1];
new TreePathScanner<Void, Void>() {
@Override public Void visitVariable(VariableTree vt, Void p) {
assertNotNull(vt.getModifiers());
assertNotNull(vt.getType());
assertNotNull(vt.getName());
assertEquals("name should be <error>", "<error>", vt.getName().toString());
foundVar[0] = true;
return super.visitVariable(vt, p);
}
}.scan(cuts, null);
if (!foundVar[0]) {
fail("haven't found a variable");
}
String actualErrors = normalize(out.toString());
assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors);
}
void run(String[] args) throws Exception {
int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0)