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

Fail to infer correct types for fluent method calls

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 4.0.2
    • None
    • Static Type Checker
    • None

    Description

      The following code can not run when STC is enabled:

      import java.util.stream.Collectors;
      
      @groovy.transform.CompileStatic
      def m() {
          Map<String, Integer> map = new HashMap<>();
          map.put("a", 1);
          map.put("b", 2);
      
          final Map<String, Integer> result =
                  map.entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (v1, v2) -> v1));
      
          return result;
      }
      
      assert [a:1, b:2] == m();
      

      Here are the error messages:

      3 compilation errors:
      
      [Static type checking] - Cannot find matching method java.lang.Object#getKey(). Please check if the declared type is correct and if the method exists.
       at line: 10, column: 67
      
      [Static type checking] - Cannot find matching method java.lang.Object#getValue(). Please check if the declared type is correct and if the method exists.
       at line: 10, column: 84
      
      [Static type checking] - Incompatible generic argument types. Cannot assign java.util.Map<java.lang.Object, java.lang.Object> to: java.util.Map<java.lang.String, java.lang.Integer>
       at line: 10, column: 13
      
      

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: