8228451: NPE in Attr.java when -XDshould-stop.ifError=FLOW

Avoiding parsing of compound assignment as a type.

Reviewed-by: jjg, vromero
This commit is contained in:
Jan Lahoda 2020-03-04 13:43:27 +01:00
parent 9d57eefe6e
commit 0c9983887d
2 changed files with 31 additions and 3 deletions

View file

@ -817,7 +817,7 @@ public class JavacParser implements Parser {
JCExpression term() { JCExpression term() {
JCExpression t = term1(); JCExpression t = term1();
if ((mode & EXPR) != 0 && if ((mode & EXPR) != 0 &&
token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0) (token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0))
return termRest(t); return termRest(t);
else else
return t; return t;

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 * @bug 7073631 7159445 7156633 8028235 8065753 8205418 8205913 8228451
* @summary tests error and diagnostics positions * @summary tests error and diagnostics positions
* @author Jan Lahoda * @author Jan Lahoda
* @modules jdk.compiler/com.sun.tools.javac.api * @modules jdk.compiler/com.sun.tools.javac.api
@ -1464,6 +1464,34 @@ public class JavacParserTest extends TestCase {
expectedAST); expectedAST);
} }
@Test
void testCompoundAssignment() throws IOException {
assert tool != null;
String code = "package test; class Test { v += v v;}";
StringWriter output = new StringWriter();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(output, fm, null, List.of("-XDrawDiagnostics"),
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
List<String> actual = List.of(output.toString().split("\r?\n"));
List<String> expected = List.of("Test.java:1:29: compiler.err.expected: token.identifier");
assertEquals("The expected and actual errors do not match, actual errors: " + actual,
actual,
expected);
String actualAST = cut.toString().replaceAll("\\R", "\n");
String expectedAST = "package test;\n" +
"\n" +
"class Test {\n" +
" v <error>;\n" +
" v v;\n" +
"}";
assertEquals("The expected and actual AST do not match, actual AST: " + actualAST,
actualAST,
expectedAST);
}
void run(String[] args) throws Exception { void run(String[] args) throws Exception {
int passed = 0, failed = 0; int passed = 0, failed = 0;
final Pattern p = (args != null && args.length > 0) final Pattern p = (args != null && args.length > 0)