Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10277

STC fails to catch type error on the return type of lambdas

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-rc-1
    • Static Type Checker
    • None

    Description

      I have the following program

      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()
        }
      }
      

      Actual behaviour

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

      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)
      
      

      Expected behaviour

      The compiler should reject this program.

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

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              theosot Thodoris Sotiropoulos
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: