8205913: Inconsistent source code model
authorjlahoda
Thu, 28 Jun 2018 10:05:43 +0200
changeset 50868 addda6247cb0
parent 50867 e84038f37713
child 50869 be1020446dd5
8205913: Inconsistent source code model Summary: Ensuring variable declarations have non-null modifiers and names. Reviewed-by: mcimadamore
src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
test/langtools/jdk/jshell/ToolTabSnippetTest.java
test/langtools/tools/javac/parser/JavacParserTest.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Jun 28 10:05:39 2018 +0200
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Thu Jun 28 10:05:43 2018 +0200
@@ -2784,7 +2784,7 @@
                 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();
             }
--- a/test/langtools/jdk/jshell/ToolTabSnippetTest.java	Thu Jun 28 10:05:39 2018 +0200
+++ b/test/langtools/jdk/jshell/ToolTabSnippetTest.java	Thu Jun 28 10:05:43 2018 +0200
@@ -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 @@
 
             //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:");
         });
     }
 
--- a/test/langtools/tools/javac/parser/JavacParserTest.java	Thu Jun 28 10:05:39 2018 +0200
+++ b/test/langtools/tools/javac/parser/JavacParserTest.java	Thu Jun 28 10:05:43 2018 +0200
@@ -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.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 @@
         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)