Paul King created GROOVY-12772:
----------------------------------
Summary: STC: a closure's result is not checked against a type
parameter of the calling method that the target gives a generic call
Key: GROOVY-12772
URL: https://issues.apache.org/jira/browse/GROOVY-12772
Project: Groovy
Issue Type: Bug
Components: Static Type Checker
Affects Versions: 6.0.0, 5.1.3, 4.0.33
Reporter: Paul King
Under {{@TypeChecked}} and {{@CompileStatic}}, the result of a closure passed
to a generic method is not checked when the assignment target gives the
method's type parameter a type variable of the calling method:
{code:groovy}
@groovy.transform.CompileStatic
class C {
static <T> List<T> cast(List<String> strings) {
List<T> list = strings.collect { String s -> s }
list
}
static main(args) {
List<Integer> ints = cast(['a'])
println ints[0].intValue()
}
}
{code}
This compiles with 4.0.33, 5.1.3, 6.0.0 and master, and then fails:
{noformat}
java.lang.ClassCastException: class java.lang.String cannot be cast to class
java.lang.Integer
{noformat}
(a {{MissingMethodException}} for {{intValue}} under {{@TypeChecked}}).
javac rejects the equivalent, {{List<T> l = s.stream().map((String x) ->
x).collect(Collectors.toList())}}, with "inference variable T has incompatible
bounds".
*Expected:* a compile error. {{collect}}'s result type parameter takes {{T}}
from the target, and the closure returns a {{String}}, which is not a {{T}}.
A {{@NotYetImplemented}} test,
{{GenericsSTCTest#testClosureResultIsCheckedAgainstEnclosingTypeParameter}},
records this in PR #3293.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)