RFR: 8277105: Inconsistent handling of missing permitted subclasses

Vicente Romero vromero at openjdk.java.net
Thu Nov 18 20:49:41 UTC 2021


On Tue, 16 Nov 2021 20:21:03 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

> 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.

looks good

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

Marked as reviewed by vromero (Reviewer).

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


More information about the compiler-dev mailing list