Scott Murphy Heiberg created GROOVY-12620:
---------------------------------------------
Summary: 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: 5.0.8, 4.0.33, 6.0.0
Reporter: Scott Murphy Heiberg
{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.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)