eisop / checker-framework

Pluggable type-checking for Java
https://eisop.github.io/
Other
15 stars 16 forks source link

Type inference failure when using lambda function reference #780

Open lpgong opened 1 month ago

lpgong commented 1 month ago

Tested with latest release 3.42.0-eisop3.

commands ./checker/bin/javac -processor nullness Test.java

inputs Test.java

import java.util.Map;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.IntStream;
import org.checkerframework.checker.nullness.qual.Nullable;

public abstract class Test {
  public Map<String, Object> test(List<String> ids) {
    return IntStream.range(0, ids.size())
               .boxed()
               .collect(collector(ids::get, i -> new Object()));
  }

  protected abstract <T extends @Nullable Object, K, V> 
      Collector<T, ?, Map<K, V>> collector(
          Function<? super T, ? extends K> keyFunction,
          Function<? super T, ? extends V> valueFunction);
}

outputs

Note: The Checker Framework is tested with JDK 8, 11, 17, and 21. You are using version 22.
Test.java:12: error: [methodref.param.invalid] Incompatible parameter type for arg0
               .collect(collector(ids::get, i -> new Object()));
                                  ^
  found   : @NonNull int
  required: /*INFERENCE FAILED for:*/ ? extends @Nullable Object
  Consequence: method in @NonNull List<@NonNull String>
    @NonNull String get(@NonNull List<@NonNull String> this, @NonNull int p0)
  is not a valid method reference for method in @NonNull Function</*INFERENCE FAILED for:*/ ? extends @Nullable Object, @NonNull String>
    @NonNull String apply(@NonNull Function</*INFERENCE FAILED for:*/ ? extends @Nullable Object, @NonNull String> this, /*INFERENCE FAILED for:*/ ? extends @Nullable Object p0)
1 error

notes Can be fixed by replacing ids::get with i -> ids.get(i)