Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8311038

Incorrect exhaustivity computation

    XMLWordPrintable

Details

    • b07

    Backports

      Description

        Consider code like:
        ---
        public class Test {

            record Rec(Object t) {}

            private void test1() {
                Rec r = new Rec("test");
                int res = switch (r) {
                    case Rec(String x): {
                        yield 4;
                    }
                    case Rec(Object x): {
                        yield -3;
                    }
                };

                if (r instanceof Rec(String x)) {
                    System.out.println(x.length());
                }
            }

            private void test2() {
                Rec r = new Rec("test");
                int res = switch (r) {
                    case Rec(String x): {
                        yield x.length();
                    }
                    case Rec(Object x): {
                        yield -3;
                    }
                };
            }

        }
        ---

        this leads to:
        ---
        /home/work/src/jdk/jdk/test/langtools/tools/Test.java:14: error: the switch expression does not cover all possible input values
                int res = switch (r) {
                          ^
        /home/work/src/jdk/jdk/test/langtools/tools/Test.java:30: error: the switch expression does not cover all possible input values
                int res = switch (r) {
                          ^
        2 errors
        ---

        which is obviously wrong, because the switches are exhaustive.

        Also not that code like:
        ---
        public class Test {

            record Rec(Object t) {}

            private void test0() {
                Rec r = new Rec("test");
                int res = switch (r) {
                    case Rec(String x): {
                        yield 4;
                    }
                    case Rec(Object x): {
                        yield -3;
                    }
                };
            }
        }
        ---

        despite having the overall structure as the original code. That is a Symbol completion issue that needs to be handled as well.

        Attachments

          Issue Links

            Activity

              People

                jlahoda Jan Lahoda
                jlahoda Jan Lahoda
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: