[
https://issues.apache.org/jira/browse/GROOVY-12831?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150566#comment-18150566
]
ASF GitHub Bot commented on GROOVY-12831:
-----------------------------------------
paulk-asert opened a new pull request, #3357:
URL: https://github.com/apache/groovy/pull/3357
…d await needs its operand on the same line
A method call, property or index after await(x) was taken into the operand,
so await(task).toString() awaited task.toString(), although await(task) + 1
added to the awaited value. The parenthesised form is now a primary, so a
following path element applies to the awaited value. Without parentheses await
still takes the whole expression.
The grammar also allowed newlines between await and its operand, so a
variable, field or parameter named await at the end of a line took the next
line as its operand. await must now be followed by its operand, or by (, on the
same line. async may still have its closure on the next line.
> await(x) followed by a method call, property or index awaits the whole chain
> instead of x
> -----------------------------------------------------------------------------------------
>
> Key: GROOVY-12831
> URL: https://issues.apache.org/jira/browse/GROOVY-12831
> Project: Groovy
> Issue Type: Bug
> Components: parser-antlr4
> Affects Versions: 6.0.0
> Reporter: Paul King
> Priority: Major
>
> A method call, property access or index written after \{{await(x)}} is taken
> into the operand of \{{await}}, so \{{await(task).toString()}} is read as
> \{{await((task).toString())}}. The same code with a binary operator,
> \{{await(task) + 1}}, is read as \{{(await task) + 1}}.
> {code:groovy}
> def t = async \{ 7 }
> assert await(t) + 1 == 8 // ok
> assert (await(t)).plus(1) == 8 // ok
> assert await(t).plus(1) == 8 // MissingMethodException: No
> signature of method: plus for class: GroovyPromise
> assert await(t).toString() == '7' // IllegalArgumentException: No
> Awaitable adapter found for type: java.lang.String
> def l = async \{ [3, 1, 2] }
> assert await(l).sort() == [1, 2, 3] // MissingMethodException: No
> signature of method: sort for class: GroovyPromise
> assert await(l)[0] == 3 // MissingMethodException
> {code}
> The behaviour is the same at script level, in a method and inside an
> \{{async}} closure, on 6.0.0, the current 6.0.x branch and master.
> h3. Cause
> {\{awaitExprAlt}} in \{{GroovyParser.g4}} offers two forms:
> {code}
> AWAIT NL* ( LPAREN expression (COMMA NL* expression)* RPAREN
> | expression (COMMA NL* expression)*
> )
> {code}
> When a binary operator follows \{{await(t)}}, both forms parse and the first
> wins. When \{{.}}, \{{?.}}, \{{[}} or an argument list follows, the first
> form cannot continue, because a path element cannot follow an expression at
> that level, so the second form takes \{{(t).toString()}} as a full
> expression. GROOVY-12715 copied the same alternatives into
> \{{castOperandExpression}}, so \{{(String) await(t).toString()}} behaves the
> same way.
> {\{core-grammar.adoc}} documents that \{{await a + b}} is \{{await (a + b)}},
> and that is not in question here. The problem is the parenthesised form:
> \{{await(t)}} looks like, and mostly behaves like, a call, so a member access
> after it is expected to apply to the awaited result.
> The only existing test using this shape,
> \{{ChannelCompositionTest.testSplit}} (\{{await(bigTask).sort()}}), is
> \{{@Disabled}}.
> h3. Proposed fix
> Make the parenthesised form bind like a method call: a path element
> (\{{.m()}}, \{{?.p}}, \{{[i]}}, a call or closure argument) after
> \{{await(...)}} applies to the awaited value. The unparenthesised form keeps
> taking a full expression, so \{{await a.b()}} is still \{{await (a.b())}}.
> Apply the same change to the \{{castOperandExpression}} alternative.
> h3. Compatibility
> Code that relies on the current reading, such as \{{await(x).m()}} meaning
> "await the result of \{{x.m()}}", changes meaning. That reading contradicts
> the binary-operator case and is unlikely to be intended; \{{await x.m()}}
> keeps that meaning. Async/await is incubating.
> h3. Tests
> * \{{await(t).m()}}, \{{await(t)?.p}}, \{{await(t)[0]}} and \{{await(t).m {
> ... }}} apply to the awaited value, at script level, in a method and inside
> an \{{async}} closure.
> * The multi-argument form \{{await(a, b)}} is unchanged.
> * \{{await a.m()}} is still \{{await (a.m())}}.
> * \{{(T) await(t).m()}} under the cast rule.
> * Re-enable or rewrite \{{ChannelCompositionTest.testSplit}}'s assertions so
> the shape is covered.
> * \{{await}} with its operand on the same line, and \{{await(}} with
> arguments on following lines, are unchanged.
> ```
> Both propose 6.0.1. Draft 1 matters more: I'd expect
> `await(task).someMethod()` to be common. Draft 2 could slip to a later
> release if you prefer. Once you've filed them, send me the keys and I'll
> record them in the task.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)