typetools / typetools/checker-framework

Index Checker unable to infer length properties of CharSequence length() method

Open
#2,561 2 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 following code:-

class ex{
	public static void pr(CharSequence ch) {
		char[] newCh = new char[ch.length()];
		for (int i = 0; i < newCh.length; i++)
			newCh[i] = ch.charAt(i);
	}
}

Command:-

javac -processor index ex.java

Output:-

ex.java:7: error: [argument.type.incompatible] incompatible types in argument.
        newCh[i] = ch.charAt(i);
                             ^
  found   : @LTLengthOf(value="newCh", offset="0") int
  required: @LTLengthOf(value="ch", offset={}) int
1 error

If I change code to:-

class ex{
	public static void pr(CharSequence ch) {
		String b=ch.toString();
		char[] newCh = new char[b.length()];
		for (int i = 0; i < newCh.length; i++)
			newCh[i] = ch.charAt(i);
	}
}

then it works fine.

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

Start by reproducing the example with javac -processor index and compare it with the ch.toString() variant. Investigate how the Index Checker handles CharSequence.length() and CharSequence.charAt() in this loop. Done means the original example type-checks without the incompatible-length error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.