RFR: 8277105: Inconsistent handling of missing permitted subclasses

Jan Lahoda jlahoda at openjdk.java.net
Tue Nov 16 20:30:01 UTC 2021


Consider a sealed interface, when one (or more) of the permitted subtypes is not available.:

public abstract sealed class Lib permits Impl1, Impl2 {}
final class Impl1 extends Lib {}
final class Impl2 extends Lib {}


When compiling against this `Lib`, consider that `Impl1` is missing. Then there is an inconsistency between cast and pattern switch in reported errors:

public class Test1 {
    public void test(Lib lib) {
        Runnable r = (Runnable) lib;
    }
}

$ javac -d out -classpath out --enable-preview -source 17 test/Test1.java
test/Test1.java:3: error: cannot access Impl1
        Runnable r = (Runnable) lib;
                     ^
  class file for Impl1 not found
1 error



public class Test2 {
    public void test(Lib lib) {
        switch (lib) {
            case Impl2 i -> {}
        }
    }
}

$ javac -d out -classpath out --enable-preview -source 17 test/Test2.java
test/Test2.java:3: error: the switch statement does not cover all possible input values
        switch (lib) {
        ^
Note: test/Test2.java uses preview features of Java SE 17.
Note: Recompile with -Xlint:preview for details.
1 error


The pattern matching switch tries to recover from the missing permitted subtype, but that is not right - it is generally not possible to determine reliably if a cast is valid, or a switch is exhaustive, when a permitted subtype is missing. So the "not found" error should be reported also for the pattern switch case.

The `CompletionFailure` still needs to be handled somehow in `Flow`, as there's nothing else that would catch the exception, and the compilation would fail with an exception if not handled.

-------------

Commit messages:
 - Fixing header years.
 - Removing debug info.
 - 8277105: Inconsistent handling of missing permitted subclasses

Changes: https://git.openjdk.java.net/jdk/pull/6418/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=6418&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8277105
  Stats: 315 lines in 3 files changed: 201 ins; 101 del; 13 mod
  Patch: https://git.openjdk.java.net/jdk/pull/6418.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/6418/head:pull/6418

PR: https://git.openjdk.java.net/jdk/pull/6418


More information about the compiler-dev mailing list