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

Fixed-size instances of mutable collections #197

Open
kelloggm opened this issue Dec 5, 2017 · 1 comment
Open

Fixed-size instances of mutable collections #197

kelloggm opened this issue Dec 5, 2017 · 1 comment

Comments

@kelloggm
Copy link
Owner

kelloggm commented Dec 5, 2017

A collection value may be of fixed size even though it has a mutable type. (Or, the value may be of fixed size in the scope in which it is analyzed, such as within a method call to which it is passed as an argument.)

For instance, a method might take a CharSequence and create an index into it by comparing it to CharSequence#length:

void test(CharSequence seq) {
    if (seq.length() > 0) {
        @IndexFor("seq") int index = seq.length() - 1;
    }
}

Currently, the Index Checker forbids this code. This is because CharSequences can be mutable, so the return type of CharSequence#length does not have an upper bound annotation. In this specific case, seq is immutable, so the type of seq.length() could be @IndexOrHigh("seq").

A proposal to remedy this is to add a @FixedSize or @Readonly annotation that can be applied to collections. So, the method above would be annotated:

void test(@FixedSize CharSequence seq) {
    if (seq.length() > 0) {
        @IndexFor("seq") int index = seq.length() - 1;
    }
}

On its own, this annotation does nothing. The annotation is trusted.

In CharSequence, we would then annotate the length method so that if the collection is @FixedSize, length returns @IndexOrHigh, using something like @EnsuresQualifierIf (although the actual syntax would likely have to be different):

interface CharSequence {
    @NonNegative @IfFixedSize(LTEqLengthOf.class) int length();
}

Then, we can implicitly add the @FixedSize annotation to known fixed-size implementations of CharSequence, such as String.

@kelloggm
Copy link
Owner Author

kelloggm commented Dec 5, 2017

This is distinct from #154 in that this issue refers to specific fixed-size instances of collections that are generally mutable. Issue #154 refers to classes that are always immutable.

smillst pushed a commit to typetools/checker-framework that referenced this issue Jan 4, 2018
…1716)

As a partial fix for kelloggm#154 and kelloggm#197, this skips warnings on uses of base classes of sequences which contain methods that change the length and have both mutable and immutable implementations.

In order to do that, `SourceChecker` had to be modified to allow overriding `shouldSkipUses`, so that we can ignore these classes in addition to classes matching the pattern specified by the user.

The base classes are:
- `java.util.List`
- `java.util.AbstractList`

Index jdk chages:
- add upper bound annotations to index parameters in `java.util.List` and `java.util.AbstractList`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant