Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in CFG construction for case null in switch expression #6173

Closed
msridhar opened this issue Sep 12, 2023 · 2 comments · Fixed by #6192
Closed

Crash in CFG construction for case null in switch expression #6173

msridhar opened this issue Sep 12, 2023 · 2 comments · Fixed by #6192
Assignees

Comments

@msridhar
Copy link
Contributor

msridhar commented Sep 12, 2023

See uber/NullAway#831. Here is the code snippet:

static <T extends Metric<?>> GroupByQuery<T> toGroupByQueryWithExtractor(
      GroupBy groupBy, GroupByResultExtractor<T> extractor) {
    return switch (groupBy) {
      case OWNER -> new GroupByOwnerQuery<>(extractor);
      case CHANNEL -> new GroupByChannelQuery<>(extractor);
      case TOPIC -> new GroupByTopicQuery<>(extractor);
      case TEAM -> new GroupByTeamQuery<>(extractor);
      case null -> throw new IllegalArgumentException("GroupBy parameter is required");
    };
  }

The top of the crash stack is:

  Caused by: java.lang.ClassCastException: class com.sun.tools.javac.tree.JCTree$JCLiteral cannot be cast to class com.sun.source.tree.IdentifierTree (com.sun.tools.javac.tree.JCTree$JCLiteral and com.sun.source.tree.IdentifierTree are in module jdk.compiler of loader 'app')
        at org.checkerframework.nullaway.dataflow.cfg.builder.CFGTranslationPhaseOne$SwitchBuilder.casesAreExhaustive(CFGTranslationPhaseOne.java:2565)
        at org.checkerframework.nullaway.dataflow.cfg.builder.CFGTranslationPhaseOne$SwitchBuilder.build(CFGTranslationPhaseOne.java:2344)
        at org.checkerframework.nullaway.dataflow.cfg.builder.CFGTranslationPhaseOne.visitSwitchExpression17(CFGTranslationPhaseOne.java:585)
        at org.checkerframework.nullaway.dataflow.cfg.builder.CFGTranslationPhaseOne.scan(CFGTranslationPhaseOne.java:548)
        at org.checkerframework.nullaway.dataflow.cfg.builder.CFGTranslationPhaseOne.visitReturn(CFGTranslationPhaseOne.java:3389)

Based on the JCLiteral type I'm guessing the issue is with case null. I'll work on putting together a test case, just wanted to report in case the root cause is obvious.

@msridhar msridhar changed the title Crash in CFG construction for switch expression on JDK 20 Crash in CFG construction for case null in switch expression Sep 12, 2023
@smillst smillst self-assigned this Sep 12, 2023
@smillst
Copy link
Member

smillst commented Sep 12, 2023

(Note using null in a case statement is a preview feature of Java 20, so I just used Java 21 to reproduce.)
Here's a testcase:

import org.checkerframework.checker.nullness.qual.Nullable;

public class Issue6173 {

  static Object toGroupByQueryWithExtractor2(
      @Nullable GroupBy groupBy) {
    return switch (groupBy) {
      case OWNER -> new Object();
      case CHANNEL -> new Object();
      case TOPIC -> new Object();
      case TEAM -> new Object();
      case null -> throw new IllegalArgumentException("GroupBy parameter is required");
    };
  }

  public enum GroupBy {
    OWNER,
    CHANNEL,
    TOPIC,
    TEAM;
  }
}

@smillst
Copy link
Member

smillst commented Sep 12, 2023

We also need to fix the Nullness Checker so that is allows this case, which I'm working on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants