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

Compiler does not promote/check lambda results when required

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 4.0.21
    • 4.0.22
    • Compiler
    • None

    Description

      Given this code, the Groovy compiler compiles it and happily inserts a java.lang.Integer into a Map<String, Long. The Java compiler produces a compile-time error for the same code, complaining that 1 is not a Long. The result is the same for static and dynamic compilation: heap pollution.

      class Repro {
          static Map<String, Long> map = new HashMap()
      
          static void main(String... args) {
              println map.computeIfAbsent('key', (__) -> 1)
              println map.key.class
              println map.compute('key', (__, Long existing) -> (existing ?: 0L) + 2)
          }
      }
      
      import java.util.HashMap;
      import java.util.Map;
      
      class ReproJava {
          static Map<String, Long> map = new HashMap<>();
      
          static void main(String... args) {
              map.computeIfAbsent("key", (__) -> 1);
              map.compute("key", (__, existing) -> (existing != null ? existing : 0L) + 2);
          }
      }
      

      Attachments

        Activity

          People

            emilles Eric Milles
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: