RFR: 8367530: The exhaustiveness errors could be improved [v8]

Maurizio Cimadamore mcimadamore at openjdk.org
Fri Nov 14 13:34:08 UTC 2025


On Thu, 13 Nov 2025 18:56:40 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> Consider code like:
>> 
>> package test;
>> public class Test {
>>     private int test(Root r) {
>>         return switch (r) {
>>             case Root(R2(R1 _), R2(R1 _)) -> 0;
>>             case Root(R2(R1 _), R2(R2 _)) -> 0;
>>             case Root(R2(R2 _), R2(R1 _)) -> 0;
>>         };
>>     }
>>     sealed interface Base {}
>>     record R1() implements Base {}
>>     record R2(Base b1) implements Base {}
>>     record Root(R2 b2, R2 b3) {}
>> }
>> ``` 
>> 
>> This is missing a case for `Root(R2(R2 _), R2(R2 _))`. javac will produce an error correctly, but the error is not very helpful:
>> 
>> $ javac test/Test.java
>> .../test/Test.java:4: error: the switch expression does not cover all possible input values
>>         return switch (r) {
>>                ^
>> 1 error
>> 
>> 
>> The goal of this PR is to improve the error, at least in some cases to something along these lines:
>> 
>> $ javac test/Test.java 
>> .../test/Test.java:4: error: the switch expression does not cover all possible input values
>>         return switch (r) {
>>                ^
>>   missing patterns: 
>>     test.Test.Root(test.Test.R2(test.Test.R2 _), test.Test.R2(test.Test.R2 _))
>> 1 error
>> 
>> 
>> The (very simplified) way it works in a recursive (or induction) way:
>> - start with defining the missing pattern as the binding pattern for the selector type. This would certainly exhaust the switch.
>> - for a current missing pattern, try to enhance it:
>>     - if the current type is a sealed type, try to expand to its (direct) permitted subtypes. Remove those that are not needed.
>>     - if the current (binding pattern) type is a record type, expand it to a record type, generate all possible combinations of its component types based on sealed hierarchies. Remove those that are not needed.
>> 
>> This approach relies heavily on our ability to compute exhaustiveness, which is evaluated repeatedly in the process.
>> 
>> There are some cases where the algorithm does not produce ideal results (see the tests), but overall seems much better than what we have now.
>> 
>> Another significant limitation is the speed of the process. Evaluating exhaustiveness is not a fast process, and this algorithm evaluates exhaustiveness repeatedly, potentially for many combinations of patterns (esp. for record patterns). So part of the proposal here is to have a time deadline for the computation. The default is 5s, and can be changed by `-XDexhaustivityTimeout=<timeout-in-ms>`.
>> 
>> There's also an open possibility for select tools to...
>
> Jan Lahoda has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Fixing trailing whitespaces.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ExhaustivenessComputer.java line 161:

> 159:                                 return enum2Constants.get(enumType).stream().map(c -> enumType.toString() + "." + c.name);
> 160:                             } else {
> 161:                                 return Stream.of(pd.toString());

As discussed offline, eager string generation will render the diagnostic arguments completely opaque to the formatter. This would mean that no `where` clauses will be generated, and unambiguous qualifiers will not be omitted.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/ExhaustivenessComputer.java line 832:

> 830:                                                     Set.of(defaultPattern));
> 831:         } catch (TimeoutException ex) {
> 832:             return ex.missingPatterns != null ? ex.missingPatterns : Set.of();

Instead of a timeout, I wonder if you could instead cut the recursion at a specific threshold. It seems to me that recursing more will provide more precision _at the nested level_, so it's a trade off between when do we want to stop. 

Overload resolution provides some kind of precedent:


error: incompatible types: String cannot be converted to int
      m("Hello");
        ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error


(We "compress" the diagnostic whenever we can somehow figure out if an overload is "better" than the others). Then if you provide the option, you get the full thing:


error: no suitable method found for m(String)
      m("Hello");
      ^
    method Test.m() is not applicable
      (actual and formal argument lists differ in length)
    method Test.m(int) is not applicable
      (argument mismatch; String cannot be converted to int)
    method Test.m(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Test.m(int,int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Test.m(int,int,int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Test.m(int,int,int,int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Test.m(int,int,int,int,int,int) is not applicable
      (actual and formal argument lists differ in length)


But, also, maybe putting an upper bound on the recursion, no matter what, might be a good idea?

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

PR Review Comment: https://git.openjdk.org/jdk/pull/27256#discussion_r2527519653
PR Review Comment: https://git.openjdk.org/jdk/pull/27256#discussion_r2527511175


More information about the compiler-dev mailing list