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

Wrong type inference when passing null

    XMLWordPrintableJSON

Details

    Description

      I have the following Groovy program.

      @groovy.transform.CompileStatic
      public class Main {
        public static void main(String[] args) {
          final A<Integer> x = new A<>(null);
          baz(x);
        }
      
        public static void baz(A<Integer> x) {}
      
      }
      
      class A<T> {
        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:
      Test.groovy: 4: [Static type checking] - Incompatible generic argument types. Cannot assign A <java.lang.Object> to: A <Integer>
       @ line 4, column 26.
             final A<Integer> x = new A<>(null);
                                  ^
      
      Test.groovy: 5: [Static type checking] - Cannot call Main#baz(A <java.lang.Integer>) with arguments [A <java.lang.Object>]
       @ line 5, column 5.
             baz(x);
             ^
      
      2 errors
      
      

      Expected Behavior

      Compile successfully.

      Comment

      This should be a regression bug because it compiles with the 4.0.0-alpha-2 compiler.

      Affected Version

      This programs fails when compiled with the compiler from the master (commit: f0eea862549529ef4e93fafe337f86dd4ac98751).

      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