[ 
https://issues.apache.org/jira/browse/GROOVY-12620?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Scott Murphy Heiberg updated GROOVY-12620:
------------------------------------------
    Description: 
{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

  was:
{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.


> 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