RFR: 8300543 Compiler Implementation for Pattern Matching for switch [v5]

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Apr 24 08:59:03 UTC 2023


On Fri, 21 Apr 2023 16:25:04 GMT, Jan Lahoda <jlahoda at openjdk.org> wrote:

>> This is the first draft of a patch for JEP 440 and JEP 441. Changes included:
>> 
>>  - the pattern matching for switch and record patterns features are made final, together with updates to tests.
>>  - parenthesized patterns are removed.
>>  - qualified enum constants are supported for case labels.
>> 
>> This change herein also includes removal record patterns in for each loop, which may be split into a separate PR in the future.
>
> Jan Lahoda has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Adding test.
>  - Removing redundant continue, as noted on the review.

I believe the exhaustiveness algorithm needs rules for union types, both in javac and in the JLS:


class Foo extends Exception { }
    class Bar extends Exception { }

    void m() {
        try {
            g();
        } catch (Foo | Bar ex) {
            String s = switch (ex) {
                case Foo f -> "Foo";
                case Bar f -> "Bar";
            };
        }
    }

    void g() throws Foo, Bar { }


I would expect the above switch to be exhaustive, since it covers all possible components of the union type. In other words, the union type should act as a sealed hierarchy which permits Foo and Bar - and the two cases should be merged together (e.g. `case Foo` + `case Bar` should cover `Foo | Bar`).  For the records, I get same error on JDK 20.

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

PR Comment: https://git.openjdk.org/jdk/pull/13074#issuecomment-1519655297


More information about the core-libs-dev mailing list