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

Static analysis does not work properly when coercing Closure to FunctionInterface

    XMLWordPrintableJSON

Details

    Description

      The following code fails when executing in static compilation mode.

              import groovy.transform.CompileStatic
              import java.util.stream.Collectors
              import java.util.stream.Stream
              
              @CompileStatic     // if we comment this line, the code works in dynamic mode.
              public class Test1 {
                  public static void main(String[] args) {
                      p();
                  }
                  
                  public static void p() {
                      assert 13 == Stream.of(1, 2, 3).reduce(7, {r, e -> r + e});
                  }
              }
      

      Here is the error message:

      [Static type checking] - Cannot find matching method java.util.stream.Stream#reduce(int, groovy.lang.Closure). Please check if the declared type is correct and if the method exists.
       at line: 12, column: 30
      
      [Static type checking] - Cannot find matching method java.lang.Object#plus(java.lang.Object). Please check if the declared type is correct and if the method exists.
       at line: 12, column: 68
      

      Similarly, the following code fails in static compilation mode too:

              import groovy.transform.CompileStatic
              import java.util.stream.Collectors
              import java.util.stream.Stream
              
              @CompileStatic  // if we comment this line, the code works in dynamic mode.
              public class Test1 {
                  public static void main(String[] args) {
                      p();
                  }
                  
                  public static void p() {
                      def list = [1, 2, 3]
                      list.replaceAll { e -> e + 10 }
                      assert [11, 12, 13] == list
                  }
              }
      

      Here is the error message:

      [Static type checking] - Cannot find matching method java.util.List#replaceAll(groovy.lang.Closure). Please check if the declared type is correct and if the method exists.
       at line: 13, column: 17
      
      [Static type checking] - Cannot find matching method java.lang.Object#plus(int). Please check if the declared type is correct and if the method exists.
       at line: 13, column: 40
      
      
      

      Attachments

        Activity

          People

            daniel_sun Daniel Sun
            daniel_sun Daniel Sun
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: