Paul King created GROOVY-12531:
----------------------------------
Summary: MonadicChecker: type DO generator variables from
@ClosureParams on Closure-typed binds
Key: GROOVY-12531
URL: https://issues.apache.org/jira/browse/GROOVY-12531
Project: Groovy
Issue Type: Sub-task
Reporter: Paul King
Fix For: 7.x
Since GROOVY-12486, a DO generator variable is typed from the input of the
carrier's bind function. A Closure-typed bind names no input, so the checker
falls back to the carrier's first type argument. Static type checking already
reads @ClosureParams for a direct call, so DO and a direct call disagree:
{code:groovy}
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.FromString
class GRes<E, A> {
A value
GRes flatMap(@ClosureParams(value = FromString, options = 'A') Closure c) {
(GRes) c(value) }
GRes map(@ClosureParams(value = FromString, options = 'A') Closure c) { new
GRes(value: c(value)) }
}
def g = new GRes<String, Integer>(value: 6)
g.flatMap { x -> int n = x; new GRes<String, Integer>(value: n + 1) } //
compiles: x is Integer
DO(x in g) { int n = x; new GRes<String, Integer>(value: n + 1) }
// [Static type checking] - Cannot assign value of type java.lang.String to
variable of type int
{code}
Proposed fix: when the bind parameter is a Closure, read its @ClosureParams,
instantiate the hint through the compilation unit's transform loader, and call
getClosureSignatures as static type checking does. Use the result only if there
is exactly one single-parameter signature, and resolve a placeholder such as A
against the receiver's type arguments. Otherwise keep the existing fallback.
Ignore hints that derive types from the method's arguments
(PickAnyArgumentHint, i.e. the FirstParam/SecondParam/ThirdParam family, and
MapEntryOrKeyValue). On a carrier's instance-method bind the only argument is
the closure, so they would type the variable as Closure.
A prototype exists with tests for: FromString naming the last type parameter,
SimpleType, an ignored FirstParam hint, and a precompiled carrier whose hint is
read from a class file. Before merging:
* add a test for a malformed FromString option or a hint that throws
* document the precedence of bound-type sources in the DO documentation
* note that conflictResolutionStrategy is not consulted: an ambiguous hint
falls back
{code}
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)