typetools / typetools/checker-framework
Stream with wildcard and apparently incompatible filter type
@smillst is already working on this.
Since Feb 26, 2022.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
Hello,
I am currently try to resolve some of the problems CheckerFrameworks complains about after I annotated my code base for null checking.
I am using the Gradle plugin (v 0.6.8) to invoke the CheckerFramework (v 3.21.2).
I have encountered this pattern in my code base which compiles fine, but is flagged by CheckerFramework:
List<? extends @NonNull String> list = new ArrayList<>(Arrays.asList("bla"));
Stream<? extends @NonNull String> stream = list.stream(); // This is what I got as starting point, String is an example type
Predicate<@Nullable Object> filter = (obj) -> true;
Predicate<? super @NonNull String> filter2 = filter; // This is the type of predicate I try to use
long count = stream
.filter(filter2) // Error.
.count();
Using filter instead of filter2 works. But why is filter2 not accepted here by CF as argument for Stream#filter?
CF error:
[argument] incompatible argument for parameter arg0 of filter.
.filter(filter2) // Error
^
found : @UnknownKeyFor Predicate<capture#282[ extends @UnknownKeyFor Object super @UnknownKeyFor String]>
required: @UnknownKeyFor Predicate<?[ extends @UnknownKeyFor Object super capture#950[ extends @UnknownKeyFor String super @KeyForBottom Void]]>
Without the wildcard in the Stream, the error is gone, but this requires an unchecked cast:
@SuppressWarnings("unchecked")
Stream<@NonNull String> stream = (Stream<@NonNull String>) list.stream();
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.
Assessment
This issue has not been assessed yet.