Christopher Smith created GROOVY-11414:
------------------------------------------

             Summary: Compiler does not promote/check lambda results when 
required
                 Key: GROOVY-11414
                 URL: https://issues.apache.org/jira/browse/GROOVY-11414
             Project: Groovy
          Issue Type: Bug
          Components: Compiler
    Affects Versions: 4.0.21
            Reporter: Christopher Smith


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.

{code:groovy}
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)
    }
}
{code}

{code}
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);
    }
}
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to