typetools / typetools/checker-framework
Index Checker unable to infer length properties of CharSequence length() method
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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