JShell - Incorrect type inference in lists of records implementing interfaces

XMLWordPrintable

    • Type: Bug
    • Resolution: Fixed
    • Priority: P3
    • 23
    • Affects Version/s: 17, 21, 22
    • Component/s: tools
    • Environment:

      Observed in 17, 21 and 22 build 22-ea+27-2262.

    • b05

        A list of records that implement a common Shape interface is inferred as a List<Record &Shape>.
        When its elements are passed to a Consumer<Shape> in JShell, the following error is displayed:
        Error:
        | incompatible types: java.util.function.Consumer<Shape> cannot be converted to java.util.function.Consumer<? super java.lang.Record>
        This error is not there in a classical main method.
        The following code shows this bug.
        ```
        interface Shape {}
        record Square(int edge) implements Shape {}
        record Circle(int radius) implements Shape {}
        Consumer<Shape> printShape = System.out::println;
        Square square = new Square(1);
        Circle circle = new Circle(1);
        var shapes = List.of(square, circle);
        shapes.forEach(printShape);
        ```

              Assignee:
              Jan Lahoda
              Reporter:
              Jose PAUMARD
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: