redhat-developer / redhat-developer/vscode-java

Java type inference fails with this program

Abierto
#3,688 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

compile upstream
Lenguaje dominante
TypeScript
Estrellas
2.3k
Forks
546
Merge medio
20 h 1 min
PR fusionados (30 d)
11

Descripción

Environment
  • Operating System: macOS Sonoma
  • JDK version: 21
  • Visual Studio Code version: 1.90.2
  • Java extension version: 1.31.0
Steps To Reproduce

Create a file Main.java, paste the following content into it and then open it with VS Code and the Java extension:

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) {
        var 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);
    }
}
Current Result

The program doesn't compile. barsByFooIdAndBarId's type is inferred as Map<String, Map<String, Object>> instead of Map<String, Map<String, Bar>>. As a result, the assignment to Bar bar is invalid because Object is not assignable to Bar. However, if you replace var with Map<String, Map<String, Bar>> directly, then the program will be just fine. You can even run it and it will print null.

I know that this doesn't happen with javac nor IntelliJ because I noticed this at work and my colleagues using IntelliJ don't have the issue and manual compilation with Maven is also fine.

I suspect that the issue comes from the Eclipse Java language server which the Java extension is based on. But I cannot confirm this for now as I'm still trying to build and run the language server locally. And this is harder than I thought.

Expected Result

The program compiles and runs with VS Code and the Java extension. I understand that this may not be easy to solve. I would already appreciate some insights on the issue. If you think that I should open an issue on Eclipse's side, please let me know!

Additional Informations

Last but not least, thank you for your amazing work! 💪 I'm so happy to be able to write Java in VS Code thanks to the extension. I was able to move away from IntelliJ which I genuinely dislike. So thank you so much for the dedication in making this possible! ❤️

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduzca el fallo de inferencia con el programa Main.java proporcionado en VS Code 1.90.2 usando la Java extension 1.31.0 y JDK 21. Empiece investigando el Eclipse Java language server y, a continuación, verifique que barsByFooIdAndBarId se infiere como Map<String, Map<String, Bar>> y que el programa compila y se ejecuta como se espera.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
java
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
32/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.