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

STC misses type error when calling a vararg method

    XMLWordPrintableJSON

Details

    Description

      I have the following program

       

      class Foo {}
      
      class X<T> {
        T f;
        X(T f) {
          this.f = f;
        }
      
        T getF() {
          this.f;
        }
      }
      
      class A {
        void foo(int x, X<Foo>... y) {
          Foo foo = y[0].getF(); // ClastCastException at runtime
        }
      }
      
      public class Main {
          public static void main(String[] args) {
            A a = new A();
            a.foo(1, new X<>(1));
          }
      }
      

      Actual Behaviour

      groovyc compiles this program, but we get the following exception at runtime

      Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1' with class 'java.lang.Integer' to class 'Foo'
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:415)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:329)
              at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243)
              at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:318)
              at A.foo(test.groovy:16)
              at Main.main(test.groovy:23)
      
      

      Expected Behaviour

      groovyc should have rejected this program with an error similar to the following

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      test.groovy: 23: [Static type checking] - Cannot call A#foo(int, X<Foo>[]) with arguments [int, X<java.lang.Integer>]
       @ line 23, column 7.
               a.foo(1, new X<>(1));
               ^1 error
      
      

      Tested against master (https://github.com/apache/groovy/commit/2df019d3c93486e8a32d407d364913e7b65e1cba)

       

       

       

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: