[ 
https://issues.apache.org/jira/browse/GROOVY-12620?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18139874#comment-18139874
 ] 

Scott Murphy Heiberg commented on GROOVY-12620:
-----------------------------------------------

Master now accepts these: the check GROOVY-12616 gave a value returned to a 
primitive functional interface type allows a constant that fits a byte or 
short. A constant that does not fit is reported as "Possible loss of 
precision", and char is not range checked (GROOVY-12648). 
https://github.com/apache/groovy/pull/3146 now only adds the tests.

> STC rejects a closure or lambda returning an int constant for a byte, short 
> or char SAM return type
> ---------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12620
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12620
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> interface S { short get() }
> interface B { byte get() }
> interface C { char get() }
> @groovy.transform.TypeChecked // or @CompileStatic
> void test() {
>     S s = () -> 1
>     B b = { -> 2 }
>     C c = () -> 65
>     assert s.get() == (short) 1 && b.get() == (byte) 2 && c.get() == (char) 65
> }
> test()
> {code}
> Actual:
> {noformat}
> [Static type checking] - Cannot return value of type int for lambda expecting 
> short
> [Static type checking] - Cannot return value of type int for closure 
> expecting byte
> [Static type checking] - Cannot return value of type int for lambda expecting 
> char
> {noformat}
> Expected: the script compiles and runs. Java accepts these lambdas, because 
> JLS 5.2 allows narrowing an int constant that fits the target type in an 
> assignment context, and a lambda result expression is in an assignment 
> context. Groovy already accepts a short variable or a short method return 
> initialised from such a constant, and dynamic Groovy runs the script. A 
> constant that does not fit, such as 70000 for short, or a non-constant int 
> must still be rejected.
> The closure/lambda return check in StaticTypeCheckingVisitor.checkReturnType 
> accepts only a return type that is compatible after wrapping, so int is never 
> accepted where byte, short or char is expected.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3146



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to