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

Inference failure in method reference not suppressed by default #3764

Closed
wmdietlGC opened this issue Oct 13, 2020 · 1 comment
Closed

Inference failure in method reference not suppressed by default #3764

wmdietlGC opened this issue Oct 13, 2020 · 1 comment

Comments

@wmdietlGC
Copy link
Contributor

The following code:

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

class Demo {
  static <T, R, A> void use(@Nullable Collector<? super T, A, R> collector) { }

  static <T, K, V> @Nullable Collector<T, ?, Map<K,V>> calc(Function<? super T, ? extends K> keyFunction,
    Function<? super T, ? extends V> valueFunction,
    BiFunction<V, V, V> mergeFunction) { return null; }

  void foo(Function<Long, Float> f) {
    // No error when using a lambda
    use(calc(f, f, (a, b) -> Math.max(a, b)));
    // Error when using a method reference
    use(calc(f, f, Math::max));
  }
}

gives the following errors with the Nullness Checker:

Demo.java:18: error: [methodref.param.invalid] Incompatible parameter type for a
    use(calc(f, f, Math::max));
                   ^
  Method
    @Initialized @NonNull float max(@Initialized @NonNull float p0, @Initialized @NonNull float p1) in java.lang.Math
  is not a valid method reference for
    /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object apply(@Initialized @NonNull BiFunction</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object> this, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object p0, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object p1) in java.util.function.BiFunction
  found   : @Initialized @NonNull float
  required: /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object
Demo.java:18: error: [methodref.param.invalid] Incompatible parameter type for b
    use(calc(f, f, Math::max));
                   ^
  Method
    @Initialized @NonNull float max(@Initialized @NonNull float p0, @Initialized @NonNull float p1) in java.lang.Math
  is not a valid method reference for
    /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object apply(@Initialized @NonNull BiFunction</*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object> this, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object p0, /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object p1) in java.util.function.BiFunction
  found   : @Initialized @NonNull float
  required: /*INFERENCE FAILED for:*/ ? extends @Initialized @Nullable Object
2 errors

Note how the equivalent, but more verbose, lambda expression works without errors.

I would have expected these errors to be suppressed by default, because the types contain INFERENCE FAILED. Maybe that suppression is implemented for lambdas but not for method references?
Maybe this can be fixed independently of the larger fix for #979?

Originally posted by @wmdietlGC in #979 (comment)

@smillst
Copy link
Member

smillst commented May 21, 2024

This test cases passes not.

@smillst smillst closed this as completed May 21, 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

2 participants