You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2021/10/01 15:30:00 UTC

[jira] [Updated] (GROOVY-10277) STC fails to catch type error on the return type of lambdas

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

Thodoris Sotiropoulos updated GROOVY-10277:
-------------------------------------------
    Component/s: Static Type Checker
    Description: 
I have the following program

{code:java}
import java.util.function.Supplier;


class Main {
  static void main(String[] args) {
    bar({ -> true}); // should not type-check
    bar({ -> true} as Supplier<Long>); // should not type-check.
  }

  static Long bar(Supplier<Long> x) {
    x.get()
  }
}
{code}

h3. Actual behaviour

The compiler mistakenly compiles this program, but produces a CCE at runtime.

{code}
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.lang.Long'
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:182)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:294)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:251)
        at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
        at Main.bar(test.groovy:10)
        at Main.main(test.groovy:6)

{code}

h3. Expected behaviour

The compiler should reject this program.

Tested against master.
Also note that this bug exists since Groovy v3.0.8.


> STC fails to catch type error on the return type of lambdas
> -----------------------------------------------------------
>
>                 Key: GROOVY-10277
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10277
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Priority: Major
>
> I have the following program
> {code:java}
> import java.util.function.Supplier;
> class Main {
>   static void main(String[] args) {
>     bar({ -> true}); // should not type-check
>     bar({ -> true} as Supplier<Long>); // should not type-check.
>   }
>   static Long bar(Supplier<Long> x) {
>     x.get()
>   }
> }
> {code}
> h3. Actual behaviour
> The compiler mistakenly compiles this program, but produces a CCE at runtime.
> {code}
> Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.lang.Long'
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:182)
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:294)
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:251)
>         at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
>         at Main.bar(test.groovy:10)
>         at Main.main(test.groovy:6)
> {code}
> h3. Expected behaviour
> The compiler should reject this program.
> Tested against master.
> Also note that this bug exists since Groovy v3.0.8.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)