typetools / typetools/checker-framework

type inference limitation with streams

Open
#1,630 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

If the explicit type argument to map is removed from the following example it is rejected by the nullness checker. It would be nice if the checker's type inference handled this case without the explicit type.

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.Nullable;

class T {
  @Nullable
  static String toString(Object o) {
    return null;
  }

  public static List<String> f(List<Integer> xs) {
    return xs != null
        ? xs.stream()
            .<@Nullable String>map(T::toString)
            .filter(Objects::nonNull)
            .collect(Collectors.toList())
        : Collections.emptyList();
  }
}

If <@Nullable String> is removed:

T.java:16: error: [methodref.return.invalid] Incompatible return type
            .map(T::toString)
                 ^
  Method
    @Initialized @Nullable String toString(@Initialized @NonNull Object p0) in T
  is not a valid method reference for
    @Initialized @NonNull String apply(@Initialized @NonNull Function<@Initialized @NonNull Integer, @Initialized @NonNull String> this, @Initialized @NonNull Integer p0) in java.util.function.Function
  found   : @Initialized @Nullable String
  required: @Initialized @NonNull String
1 error

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 compiling the reduced Java example with the Checker Framework nullness checker, both with and without the explicit <@Nullable String> type argument on map. Trace the nullness checker's inference for the stream method reference and filter; done means the version without the explicit type argument is accepted without introducing a new diagnostic.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.