Paul King created GROOVY-12832:
----------------------------------
Summary: a variable named await at the end of a line takes the
next line as an await operand
Key: GROOVY-12832
URL: https://issues.apache.org/jira/browse/GROOVY-12832
Project: Groovy
Issue Type: Bug
Components: parser-antlr4
Affects Versions: 6.0.0
Reporter: Paul King
{\{async}} and \{{await}} are contextual keywords, and
\{{core-async-await.adoc}} says they can still be used as variable or method
names in existing code. But the grammar allows newlines between \{{await}} and
its operand, so a variable, field or parameter named \{{await}} at the end of a
line is read as the keyword, and the following line becomes its operand:
{code:groovy}
def await = 5
def r = await
r // MissingPropertyException: No such property: r (parsed as: def
r = await r)
{code}
{code:groovy}
def await = 5
def x
x = await
println 'next' // MissingPropertyException: No such property: println
{code}
{code:groovy}
class A {
int await = 5
def go() {
def r = await
r // MissingPropertyException: No such property: r for class: A
}
}
{code}
All three return 5 on 5.1.3. They fail the same way on 6.0.0, the current 6.0.x
branch and master. Nothing is reported at compile time; the next statement just
disappears into the \{{await}}.
h3. Cause
{\{awaitExprAlt}} (and since GROOVY-12715 the same alternative in
\{{castOperandExpression}}) begins \{{AWAIT NL*}}, and \{{asyncClosureExprAlt}}
begins \{{ASYNC NL*}}. Both have been there since async/await was added under
GROOVY-9381. \{{new}} allows newlines in the same way, but \{{new}} is never a
name, so it cannot swallow a statement; \{{return}} and \{{throw}} do not
continue onto the next line.
No test or documentation example breaks a line after \{{await}} or \{{async}}.
h3. Proposed fix
Remove \{{NL*}} after \{{AWAIT}}, in both rules, so \{{await}} at the end of a
line ends the expression and is read as a name. \{{ASYNC NL*}} could stay: it
only matches when a closure or lambda follows, and a variable named \{{async}}
followed by a closure on the next line is rare. Removing it too would make
\{{async}} consistent with a method call, where a closure on the next line is
not an argument, but it would break \{{async}} with its closure on the next
line, which works on 6.0.0.
h3. Compatibility
An await written with its operand on the next line, such as
{code:groovy}
def x = await
task
{code}
would no longer parse as an await. Writing \{{await task}} on one line, or
\{{await(}} with the argument on the following lines, keeps working.
Async/await is incubating.
h3. Tests
* A variable, field and parameter named \{{await}} at the end of a line,
followed by another statement.
* \{{async}} with its closure on the next line, either way the \{{ASYNC NL*}}
question is decided.
* \{{await}} with its operand on the same line, and \{{await(}} with arguments
on following lines, are unchanged.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)