[
https://issues.apache.org/jira/browse/GROOVY-12616?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Scott Murphy Heiberg updated GROOVY-12616:
------------------------------------------
Description:
{code:groovy}
import java.util.function.ToLongFunction
@groovy.transform.CompileStatic
class C {
static void main(args) {
ToLongFunction<String> f = (String s) -> s.length()
assert f.applyAsLong('abc') == 3L
}
}
{code}
Actual: {{[Static type checking] - Cannot return value of type int for lambda
expecting long}}. The same happens under @TypeChecked, with a closure (passed
for a ToLongFunction parameter, or a closure returning 42 assigned to a
LongSupplier), for ToDoubleFunction from int, long or float, for char to int,
and for Integer to long.
Expected: compiles and runs, as in Java (JLS 5.2 allows a widening primitive
conversion, optionally after unboxing, in an assignment context) and as with
the method reference {{C::len}} or method pointer {{C.&len}}. Narrowing (long
to int) should still be rejected.
Cause: {{StaticTypeCheckingVisitor.checkReturnType}} accepts a closure or
lambda return value only when the wrapper of the SAM return type is compatible
with the wrapper of the value type, and Long is not compatible with Integer.
Seen in 4.0.33, 5.0.8 and 6.0.0.
PR: https://github.com/apache/groovy/pull/3142
was:
{code:groovy}
import java.util.function.ToLongFunction
@groovy.transform.CompileStatic
class C {
static void main(args) {
ToLongFunction<String> f = (String s) -> s.length()
assert f.applyAsLong('abc') == 3L
}
}
{code}
Actual: {{[Static type checking] - Cannot return value of type int for lambda
expecting long}}. The same happens under @TypeChecked, with a closure (passed
for a ToLongFunction parameter, or a closure returning 42 assigned to a
LongSupplier), for ToDoubleFunction from int, long or float, for char to int,
and for Integer to long.
Expected: compiles and runs, as in Java (JLS 5.2 allows a widening primitive
conversion, optionally after unboxing, in an assignment context) and as with
the method reference {{C::len}} or method pointer {{C.&len}}. Narrowing (long
to int) should still be rejected.
Cause: {{StaticTypeCheckingVisitor.checkReturnType}} accepts a closure or
lambda return value only when the wrapper of the SAM return type is compatible
with the wrapper of the value type, and Long is not compatible with Integer.
Seen in 4.0.33, 5.0.8 and 6.0.0.
> STC rejects a closure or lambda that returns a primitive needing only a
> widening conversion to the SAM's primitive return type
> ------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12616
> URL: https://issues.apache.org/jira/browse/GROOVY-12616
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> import java.util.function.ToLongFunction
> @groovy.transform.CompileStatic
> class C {
> static void main(args) {
> ToLongFunction<String> f = (String s) -> s.length()
> assert f.applyAsLong('abc') == 3L
> }
> }
> {code}
> Actual: {{[Static type checking] - Cannot return value of type int for lambda
> expecting long}}. The same happens under @TypeChecked, with a closure (passed
> for a ToLongFunction parameter, or a closure returning 42 assigned to a
> LongSupplier), for ToDoubleFunction from int, long or float, for char to int,
> and for Integer to long.
> Expected: compiles and runs, as in Java (JLS 5.2 allows a widening primitive
> conversion, optionally after unboxing, in an assignment context) and as with
> the method reference {{C::len}} or method pointer {{C.&len}}. Narrowing (long
> to int) should still be rejected.
> Cause: {{StaticTypeCheckingVisitor.checkReturnType}} accepts a closure or
> lambda return value only when the wrapper of the SAM return type is
> compatible with the wrapper of the value type, and Long is not compatible
> with Integer.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3142
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)