The introduction of Sequenced collections is not a source compatible change

Remi Forax forax at univ-mlv.fr
Sat Apr 29 10:46:21 UTC 2023


I've several repositories that now fails to compile with the latest jdk21, which introduces sequence collections.

The introduction of a common supertype to existing collections is *not* a source compatible change because of type inference.

Here is a simplified example:

  public static void m(List<Supplier<? extends Map<String, String>>> factories) {
  }

  public static void main(String[] args) {
    Supplier<LinkedHashMap<String,String>> supplier1 = LinkedHashMap::new;
    Supplier<SortedMap<String,String>> supplier2 = TreeMap::new;
    var factories = List.of(supplier1, supplier2);
    m(factories);
  }


This example compiles fine with Java 20 but report an error with Java 21:
  SequencedCollectionBug.java:28: error: method m in class SequencedCollectionBug cannot be applied to given types;
    m(factories);
    ^
  required: List<Supplier<? extends Map<String,String>>>
  found:    List<Supplier<? extends SequencedMap<String,String>>>
  reason: argument mismatch; List<Supplier<? extends SequencedMap<String,String>>> cannot be converted to List<Supplier<? extends Map<String,String>>>



Apart from the example above, most of the failures I see are in the unit tests provided to the students, because we are using a lot of 'var' in them so they work whatever the name of the types chosen by the students. 

Discussing with a colleague, we also believe that this bug is not limited to Java, existing Kotlin codes will also fail to compile due to this bug.

Regards,
Rémi


More information about the core-libs-dev mailing list