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

STC instantiates parameterized function with a wrong type leading to unsoundness

    XMLWordPrintableJSON

Details

    Description

      I have the following program

      class Test {
        static <T> T bar(T x, T y) { return x; }
      
        static String m() { return ""; }
      
        static void main(String[] args) {
           Integer x = bar(m(), 1); // STC misses the error.
        }
      }
      

      Actual behaviour

      groovyc incorrectly accepts the above program, although it contains a type error at line7. This leads to the following exception at runtime:

      Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '' with class 'java.lang.String' to class 'java.lang.Integer'
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:179)
              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 Test.main(test.groovy:7)
      

      Expected behaviour

      The compiler should have rejected the program.

      Tested against master.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: