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

Wrong inference inside closure when having a bounded type parameter

    XMLWordPrintableJSON

Details

    Description

      I have the following Groovy program.

      @groovy.transform.CompileStatic
      public class Main {
        public static void main(String[] args) {
          Closure<A<Long>> closure =  { -> {
            final x = (Long) 1;
            new A<>(x);
          }}
          // If I remove 'extends Number' in class A, the following code
          // works as expected.
          final Long y = closure().f;
        }
      }
      
      class A<T extends Number> {
        T f;
        public A(T f) {
          this.f = f;
        }
      }
      

      Actual Behavior

      The program does not compile, and I get the following error.

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      Main.groovy: 10: [Static type checking] - Cannot assign value of type java.lang.Number to variable of type java.lang.Long
       @ line 10, column 20.
             final Long y = closure().f;
                            ^
      
      1 error
      
      

      Expected Behavior

      Compile successfully.

      Affected Version

      I have also tested it with the compiler from Master (commit: e69c874e1a6a3cdeb236bad6a6faa061a30c88ca).

      Attachments

        Activity

          People

            emilles Eric Milles
            schaliasos Stefanos Chaliasos
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 0.5h
                0.5h