Skip to content

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Sep 12, 2025

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 delay the more detailed computation to some later time, although that would need to be tried and evaluated.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Integration blocker

 ⚠️ Dependency #27253 must be integrated first

Issue

  • JDK-8367530: The exhaustiveness errors could be improved (Enhancement - P2)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27256/head:pull/27256
$ git checkout pull/27256

Update a local copy of the PR:
$ git checkout pull/27256
$ git pull https://git.openjdk.org/jdk.git pull/27256/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27256

View PR using the GUI difftool:
$ git pr show -t 27256

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27256.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 12, 2025

👋 Welcome back jlahoda! A progress list of the required criteria for merging this PR into pr/27253 will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Sep 12, 2025

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk
Copy link

openjdk bot commented Sep 12, 2025

⚠️ @lahodaj This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

@openjdk
Copy link

openjdk bot commented Sep 12, 2025

@lahodaj The following label will be automatically applied to this pull request:

  • compiler

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the compiler compiler-dev@openjdk.org label Sep 12, 2025
@lahodaj lahodaj marked this pull request as ready for review October 9, 2025 15:38
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 9, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 9, 2025

Webrevs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler compiler-dev@openjdk.org rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

1 participant