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

Type inference failure when using lambda function reference #780

Open
lpgong opened this issue Jun 6, 2024 · 0 comments
Open

Type inference failure when using lambda function reference #780

lpgong opened this issue Jun 6, 2024 · 0 comments

Comments

@lpgong
Copy link

lpgong commented Jun 6, 2024

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)

@lpgong lpgong changed the title Type inference failure with lambda function reference Type inference failure when using lambda function reference Jun 6, 2024
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