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

Mismatched type parameter nullability #1091

Closed
ben-manes opened this issue Dec 14, 2024 · 6 comments · Fixed by #1115 or #1143
Closed

Mismatched type parameter nullability #1091

ben-manes opened this issue Dec 14, 2024 · 6 comments · Fixed by #1115 or #1143
Labels
jspecify Related to support for jspecify standard (see jspecify.dev)

Comments

@ben-manes
Copy link

Upgrading from 0.12.1 to 0.12.2 and I am confused by these new warnings.

> Task :caffeine:compileJava
/Users/ben/projects/caffeine/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java:3180: warning: [NullAway] Cannot pass parameter of type Function<@org.jspecify.annotations.Nullable V, @org.jspecify.annotations.Nullable V>, as formal parameter has type Function<V, V>, which has mismatched type parameter nullability
            .map(node -> nodeToCacheEntry(node, transformer))
                                                ^
    (see http://t.uber.com/nullaway )
  Did you mean '@SuppressWarnings("NullAway") <T> T snapshot(Iterable<Node<K, V>> iterable, Function<@Nullable V, @Nullable V> transformer,'?
/Users/ben/projects/caffeine/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java:4014: warning: [NullAway] Cannot pass parameter of type Function<@org.jspecify.annotations.Nullable V, @org.jspecify.annotations.Nullable V>, as formal parameter has type Function<V, V>, which has mismatched type parameter nullability
      return (node == null) ? null : cache.nodeToCacheEntry(node, transformer);
                                                                  ^
    (see http://t.uber.com/nullaway )
  Did you mean '@SuppressWarnings("NullAway") @Override public @Nullable CacheEntry<K, V> getEntryIfPresentQuietly(K key) {'?

The snapshot method and nodeToCacheEntry method both use the same types for the transformer parameter.

  <T> T snapshot(Iterable<Node<K, V>> iterable, Function<@Nullable V, @Nullable V> transformer,
      Function<Stream<CacheEntry<K, V>>, T> mappingFunction)

  @Nullable CacheEntry<K, V> nodeToCacheEntry(
      Node<K, V> node, Function<@Nullable V, @Nullable V> transformer)

The other warning is similar. It appears to be a regression.

Note that you might have to use --rerun, --rerun-tasks or --no-build-cache to force recompilations.

@msridhar msridhar added the jspecify Related to support for jspecify standard (see jspecify.dev) label Dec 14, 2024
@msridhar
Copy link
Collaborator

Thanks for the report; I can reproduce. I know what is wrong, but I'd like to be careful with the fix, which will take a bit of work. Will try to investigate more deeply soon.

@ben-manes
Copy link
Author

Here's a wildcard case,

/Users/ben/projects/caffeine/caffeine/src/test/java/com/github/benmanes/caffeine/cache/AsyncCacheTest.java:141: 
warning: [NullAway] Cannot pass parameter of type Function<Int, @Nullable Int>, as formal parameter has type Function<? super Int, ? extends @Nullable Int>, which has mismatched type parameter nullability
    var valueFuture = cache.get(key, k -> null);
                                     ^
    (see http://t.uber.com/nullaway )
  Did you mean '@SuppressWarnings("NullAway") var valueFuture = cache.get(key, k -> null);'?

when I tried to annotate as a nullable return value because it disliked that the Map computation had no result.

@CacheSpec
@Test(dataProvider = "caches")
public void getFunc_absent_null(AsyncCache<Int, @Nullable Int> cache, CacheContext context) {
  Int key = context.absentKey();
  var valueFuture = cache.get(key, k -> null);
  assertThat(context).stats().hits(0).misses(1).success(0).failures(1);

  assertThat(valueFuture).isDone();
  assertThat(cache).doesNotContainKey(key);
}

@msridhar
Copy link
Collaborator

This should not have been closed

@msridhar
Copy link
Collaborator

@ben-manes FYI I confirmed that with #1143 the warnings that you reported above go away.

@ben-manes
Copy link
Author

Thanks! I hope someday ErrorProne adds support for warning of unused suppressions so I don't have to remember when upgrading.

@msridhar
Copy link
Collaborator

Thanks! I hope someday ErrorProne adds support for warning of unused suppressions so I don't have to remember when upgrading.

FYI inspired by your comment I put up a draft PR for this: google/error-prone#4828 It's a feature I've wanted for a long time as well. We'll see if we can make it work :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
jspecify Related to support for jspecify standard (see jspecify.dev)
Projects
None yet
2 participants