1

Let's consider the following Java program:

import java.util.*;
import java.util.stream.Collectors;

public class Main {

    record Foo(String id, List<Bar> bars) {}

    record Bar(String id) {}

    public static void main(String[] args) {
        Map<String, Map<String, Bar>> barsByFooIdAndBarId = Collections.<Foo>emptyList()
            .stream()
            .collect(
                Collectors.toMap(
                    Foo::id,
                    foo ->
                        Collections.<String, Bar>emptyMap()
                            .entrySet()
                            .stream()
                            .collect(
                                Collectors.toMap(
                                    Map.Entry::getKey,
                                    entry ->
                                        foo
                                            .bars()
                                            .get(0)
                                )
                            )
                )
            );

        Bar bar = barsByFooIdAndBarId
            .computeIfAbsent("fooId", id -> Collections.emptyMap())
            .get("barId");

        System.out.println(bar);
    }
}

What it does is totally irrelevant to us, we're only interested by types here. If you change barsByFooIdAndBarId's type from Map<String, Map<String, Bar>> to var, you should leverage Java type inference and get exactly the same program.

While this works fine with javac and IntelliJ, this fails miserably with the VS Code extension "Language Support for Java(TM) by Red Hat" v1.31.0. The inferred type is Map<String, Map<String, Object>> in that case. As a result, the assignment to Bar bar does not typecheck as Object is not assignable to Bar.

I know that this Java extension is based on Eclipse's Java language server but I haven't been able to interact with the language server directly yet. This issue may come from the language server but I'm not entirely sure. Any ideas?

7
  • Are you asking "why does it fail" in the sense of "what is wrong with it", or in the sense of "why does VS Code's extension have a bug"? If the former, what evidence do you have that something is wrong? Do you suspect that javac and IntelliJ are mistakenly allowing it, and VS Code/Eclipse is right? If the latter, this is a bug report, which is off topic for this site. Commented Jun 26, 2024 at 22:18
  • Well I'm wondering whether that is a bug indeed. I suspect that the language server fails to perform type inference. I'm just looking for some insights because I usually doubt more about myself than the compiler. Commented Jun 26, 2024 at 22:21
  • But two compilers are telling you you're right, and one of them is the one written by the company that develops the language. :-) While I definitely respect and appreciate the instinct to assume it's you and not the tooling, I think in this case there's strong evidence that it's just a bug in the plugin. (I'm not expert enough in the inference rules to be able to add more than you already know, unfortunately.) Commented Jun 26, 2024 at 22:33
  • 3
    javac has clashed with the spec countless times. 'javac does X' is not absolute proof that X is what the spec says should happen. Merely suggestive. Nevertheless, this strikes me as a bug report, yeah. Commented Jun 26, 2024 at 22:40
  • Alright, thanks for the quick replies. I just opened an issue on the extension's repo, then. We will probably need to escalate this to Eclipse's repo in the end. We'll see. Commented Jun 26, 2024 at 22:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.