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

Incorrect desugaring of null-allowed nested patterns

XMLWordPrintable

    • b11

        Consider code like:
        ---
        public class NullPatterns {

            public static void main(String[] args) {
                switch (new R(null)) {
                    case R(Object o) when o == null -> {} //correct
                    case R(Object o) -> throw new AssertionError("Incorrect case!");
                };
            }

            record R(Object o) {}

        }
        ---

        Running this leads to incorrect outcome:
        ---
        $ java --enable-preview --source 21 NullPatterns.java
        Note: NullPatterns.java uses preview features of Java SE 21.
        Note: Recompile with -Xlint:preview for details.
        Exception in thread "main" java.lang.AssertionError: Incorrect case!
                at NullPatterns.main(NullPatterns.java:6)
        --

              jlahoda Jan Lahoda
              jlahoda Jan Lahoda
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: