# HG changeset patch # User sdama # Date 1542731347 -19800 # Node ID 7a8af2f1f0c59b38ad0d92e84cc2ec186b523609 # Parent f7309a1491d9e3edaf2e90c1da40cc65cf353a85 8210742: compound var declaration type is not uniform for all variables Summary: make implicit type for all variables in compound declaration as null for which type inference happens at later phase Reviewed-by: mcimadamore Contributed-by: srinivas.dama@oracle.com diff -r f7309a1491d9 -r 7a8af2f1f0c5 src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Nov 20 13:12:48 2018 +0000 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Nov 20 21:59:07 2018 +0530 @@ -3180,13 +3180,13 @@ if (elemType.hasTag(IDENT)) { Name typeName = ((JCIdent)elemType).name; if (isRestrictedLocalVarTypeName(typeName, pos, !compound && localDecl)) { - if (compound) { - //error - 'var' in compound local var decl - reportSyntaxError(pos, Errors.VarNotAllowedCompound); - } else if (type.hasTag(TYPEARRAY)) { + if (type.hasTag(TYPEARRAY) && !compound) { //error - 'var' and arrays reportSyntaxError(pos, Errors.VarNotAllowedArray); } else { + if(compound) + //error - 'var' in compound local var decl + reportSyntaxError(pos, Errors.VarNotAllowedCompound); startPos = TreeInfo.getStartPos(mods); if (startPos == Position.NOPOS) startPos = TreeInfo.getStartPos(type); diff -r f7309a1491d9 -r 7a8af2f1f0c5 test/langtools/tools/javac/parser/JavacParserTest.java --- a/test/langtools/tools/javac/parser/JavacParserTest.java Tue Nov 20 13:12:48 2018 +0000 +++ b/test/langtools/tools/javac/parser/JavacParserTest.java Tue Nov 20 21:59:07 2018 +0530 @@ -1041,6 +1041,28 @@ assertEquals("the error message is not correct, actual: " + actualErrors, expectedErrors, actualErrors); } + @Test //JDK-821742 + void testCompDeclVarType() throws IOException { + String code = "package test; public class Test {" + + "private void test() {" + + "var v1 = 10,v2 = 12;" + + "} private Test() {}}"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, + null, null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + ct.enter(); + ct.analyze(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(0); + VariableTree stmt1 = (VariableTree) method.getBody().getStatements().get(0); + VariableTree stmt2 = (VariableTree) method.getBody().getStatements().get(1); + Tree v1Type = stmt1.getType(); + Tree v2Type = stmt2.getType(); + assertEquals("Implicit type for v1 is not correct: ", Kind.PRIMITIVE_TYPE, v1Type.getKind()); + assertEquals("Implicit type for v2 is not correct: ", Kind.PRIMITIVE_TYPE, v2Type.getKind()); + } + @Test void testCaseBodyStatements() throws IOException { String code = "class C {" +