Case label does not refine a boxed switch selector

Open
#8,392 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
testing-qa

Research direction

Start with CFGTranslationPhaseOne.buildSelector and CFAbstractTransfer.visitCase, then trace how boxed switch selectors are represented in the control-flow graph. Run the Value Checker command from the issue with the supplied SwitchCaseRefinement.java input and compare boxed and primitive cases. Done means both assignments pass and case-constant refinements reach the boxed selector for checkers that use this transfer.

Written by the indexing model from the issue text.

Description

A case label refines a primitive switch selector, but not a boxed one.

Input
import org.checkerframework.common.value.qual.IntVal;

public class SwitchCaseRefinement {
  void boxed(Integer i) {
    switch (i) {
      case 1:
        @IntVal(1) Integer j = i;
        break;
      default:
        break;
    }
  }

  void primitive(int i) {
    switch (i) {
      case 1:
        @IntVal(1) int j = i;
        break;
      default:
        break;
    }
  }
}
Command
javac -processor org.checkerframework.common.value.ValueChecker SwitchCaseRefinement.java
Actual output
SwitchCaseRefinement.java:7: error: [assignment] incompatible types in assignment.
        @IntVal(1) Integer j = i;
                               ^
  found   : Integer
  required: @IntVal(1L) Integer
1 error
Expected output

No error. In the body of case 1, i is 1, just as in method primitive.

Cause

CFGTranslationPhaseOne.buildSelector unboxes a boxed selector, so the expression of the selector assignment is the artificial i.intValue() call rather than i. CFAbstractTransfer.visitCase refines assign.getExpression() and assign.getTarget(), so the refinement goes to the i.intValue() call and to the synthetic selector variable, but not to i. The same applies to every checker that refines on equality with a case constant.

A fix could refine the expression that the unboxing converts, as well as the unboxed expression.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
3d 6h
Merged PRs (30d)
84

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from typetools/checker-framework

All issues in typetools/checker-framework

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.