GROOVY-7958: Incorrect parsing of comma-separated variable declaration as single statement after if/while/for (closes #437)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f29962c5 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f29962c5 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f29962c5 Branch: refs/heads/parrot Commit: f29962c54229ce26eb7adb5a53fa7874afa7813c Parents: 7775ea4 Author: paulk <[email protected]> Authored: Wed Oct 5 18:05:43 2016 +1000 Committer: paulk <[email protected]> Committed: Thu Oct 6 08:51:59 2016 +1000 ---------------------------------------------------------------------- src/main/org/codehaus/groovy/antlr/groovy.g | 6 ++++- src/test/groovy/bugs/Groovy7958Bug.groovy | 31 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/f29962c5/src/main/org/codehaus/groovy/antlr/groovy.g ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/antlr/groovy.g b/src/main/org/codehaus/groovy/antlr/groovy.g index b973a5c..105059a 100644 --- a/src/main/org/codehaus/groovy/antlr/groovy.g +++ b/src/main/org/codehaus/groovy/antlr/groovy.g @@ -1975,9 +1975,13 @@ forInClause /** In Java, "if", "while", and "for" statements can take random, non-braced statements as their bodies. * Support this practice, even though it isn't very Groovy. */ -compatibleBodyStatement +compatibleBodyStatement {Token first = LT(1);} : (LCURLY)=> compoundStatement + // comma sep decl case converted to multiple statements so must be wrapped in SLIST when single statement occurs after if/while/for + | (declarationStart (varInitializer)? COMMA)=> + de:declaration + {#compatibleBodyStatement = #(create(SLIST,"CBSLIST",first,LT(1)),de);} | statement[EOF] ; http://git-wip-us.apache.org/repos/asf/groovy/blob/f29962c5/src/test/groovy/bugs/Groovy7958Bug.groovy ---------------------------------------------------------------------- diff --git a/src/test/groovy/bugs/Groovy7958Bug.groovy b/src/test/groovy/bugs/Groovy7958Bug.groovy new file mode 100644 index 0000000..1198c39 --- /dev/null +++ b/src/test/groovy/bugs/Groovy7958Bug.groovy @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.bugs + +class Groovy7958Bug extends GroovyTestCase { + void testCommaSepVariableDeclarationAfterIf() { + assertScript """ + int xNext = 0, yNext = 0 + if (false) int x = xNext++, y = yNext++ + assert xNext == 0 && yNext == 0 + if (true) int x = xNext++, y = yNext++ + assert xNext == 1 && yNext == 1 + """ + } +}
