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

Passing a *nested* switch expression as a parameter causes an NPE during compile

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Build:
      b31
    • Introduced In Version:
      17
    • Resolved In Build:
      b19
    • CPU:
      generic
    • OS:
      generic

      Backports

        Description

        ADDITIONAL SYSTEM INFORMATION :
        openjdk 17 2021-09-14
        OpenJDK Runtime Environment (build 17+35-2724)
        OpenJDK 64-Bit Server VM (build 17+35-2724, mixed mode, sharing)

        javac 17

        A DESCRIPTION OF THE PROBLEM :
        Non-nested switch expressions passed as parameters are successfully compiled but when there is a nested inner switch expression (within the outer expression passed as the parameter) then compilation results in a NullPointerException.

        REGRESSION : Last worked in version 16.0.2

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Attempt to compile source code which passes a nested switch expression as a parameter to a method. Eg: ".../jdk-17/bin/javac.exe NestedSwitchExpressionCompileError.java" (see example source code below)


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Expecting a successful compilation instead of a NullPointerException
        ACTUAL -
        > .../jdk-17/bin/javac.exe NestedSwitchExpressionCompileError.java

        An exception has occurred in the compiler (17). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program, the following diagnostic, and the parameters passed to the Java compiler in your report. Thank you.
        java.lang.NullPointerException: Cannot read field "tsym" because "selector.type" is null
                at jdk.compiler/com.sun.tools.javac.tree.TreeInfo.isErrorEnumSwitch(TreeInfo.java:1344)
                at jdk.compiler/com.sun.tools.javac.comp.Flow$AliveAnalyzer.visitSwitchExpression(Flow.java:728)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1380)
                at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
                at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
                at jdk.compiler/com.sun.tools.javac.comp.Flow$AliveAnalyzer.visitYield(Flow.java:874)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCYield.accept(JCTree.java:1658)
                at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
                at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
                at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
                at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.visitCase(TreeScanner.java:181)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCase.accept(JCTree.java:1335)
                at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
                at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:444)
                at jdk.compiler/com.sun.tools.javac.comp.Flow$AliveAnalyzer.analyzeTree(Flow.java:945)
                at jdk.compiler/com.sun.tools.javac.comp.Flow.aliveAfter(Flow.java:273)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.handleSwitch(Attr.java:1800)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.visitSwitchExpression(Attr.java:1610)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1380)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:674)
                at jdk.compiler/com.sun.tools.javac.comp.DeferredAttr.attribSpeculative(DeferredAttr.java:500)
                at jdk.compiler/com.sun.tools.javac.comp.DeferredAttr.attribSpeculative(DeferredAttr.java:480)
                at jdk.compiler/com.sun.tools.javac.comp.DeferredAttr.attribSpeculative(DeferredAttr.java:467)
                at jdk.compiler/com.sun.tools.javac.comp.ArgumentAttr.lambda$processArg$0(ArgumentAttr.java:220)
                at jdk.compiler/com.sun.tools.javac.comp.ArgumentAttr.processArg(ArgumentAttr.java:242)
                at jdk.compiler/com.sun.tools.javac.comp.ArgumentAttr.processArg(ArgumentAttr.java:219)
                at jdk.compiler/com.sun.tools.javac.comp.ArgumentAttr.visitSwitchExpression(ArgumentAttr.java:260)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCSwitchExpression.accept(JCTree.java:1380)
                at jdk.compiler/com.sun.tools.javac.comp.ArgumentAttr.attribArg(ArgumentAttr.java:197)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:672)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribArgs(Attr.java:775)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.visitApply(Attr.java:2554)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1797)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:674)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:727)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.visitExec(Attr.java:2272)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1584)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:674)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStat(Attr.java:748)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStats(Attr.java:767)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.visitBlock(Attr.java:1445)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1091)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:674)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStat(Attr.java:748)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.visitMethodDef(Attr.java:1235)
                at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:921)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribTree(Attr.java:674)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribStat(Attr.java:748)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClassBody(Attr.java:5474)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5365)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attribClass(Attr.java:5196)
                at jdk.compiler/com.sun.tools.javac.comp.Attr.attrib(Attr.java:5141)
                at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.attribute(JavaCompiler.java:1317)
                at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:946)
                at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:317)
                at jdk.compiler/com.sun.tools.javac.main.Main.compile(Main.java:176)
                at jdk.compiler/com.sun.tools.javac.Main.compile(Main.java:64)
                at jdk.compiler/com.sun.tools.javac.Main.main(Main.java:50)

        ---------- BEGIN SOURCE ----------
        class NestedSwitchExpressionCompileError {
        /* Based on JEP 361 example (https://openjdk.java.net/jeps/361#Switch-expressions) */
        static void howMany(int i, int j) {
        System.out.println(
        switch (i) {
        case 1 ->
        switch (j) {
        case 1 -> "one and one";
        default -> "one and many";
        };
        case 2 -> "two";
        default -> "many";
        }
        );
        }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Extract the result of the switch expression into a variable, then pass the variable as the parameter to the method call instead.
        ----------

        class NestedSwitchExpressionCompileWorkaround{
        static void howMany(int i, int j) {
        String result = switch (i) {
        case 1 ->
        switch (j) {
        case 1 -> "one and one";
        default -> "one and many";
        };
        case 2 -> "two";
        default -> "many";
        }
        System.out.println(result);
        }
        }

        FREQUENCY : always


          Attachments

            Issue Links

              Activity

                People

                Assignee:
                jlahoda Jan Lahoda
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: