typetools / typetools/checker-framework

IndexChecker false warning in length - index situations

Open
#2,524 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

False Positive (false warning or imprecision) Index
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Consider the snippet:

import org.checkerframework.checker.index.qual.IndexOrHigh;
import org.checkerframework.checker.index.qual.NonNegative;

public class Testing {
    public @NonNegative int method(int[] array, @IndexOrHigh("#1") int var)  {
        return array.length - var;
    }
}

Running javac -processor org.checkerframework.checker.index Testing.java, you will get the following error:

Error:(6, 29) java: [return.type.incompatible] incompatible types in return.
  type of expression: @LowerBoundUnknown int
  method return type: @NonNegative int

If var is a valid index for array, or equal to array.length, then the value returned is always 0 or greater.

Contributor guide

Open the contributing guide

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.

Research direction

Reproduce the warning with the Testing.java snippet and the javac command using the org.checkerframework.checker.index processor. Start by tracing the IndexChecker handling of @IndexOrHigh values in the length - index expression. Done means the shown method type-checks without the incompatible-return warning while preserving the expected non-negative result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.