Bad error message: `WarnOnGenericInferenceFailure` flags `Collector<_, ?, _>` arguments passed to `@NullMarked` methods such as Reactor's `Flux#collect`

Aperta
#2,124 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
20/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
java
Ambito
devtools

Direzione di ricerca

Start with the reproducer Repro1 and the WarnOnGenericInferenceFailure option, then compare with Stream#collect, which is not flagged. The open question is whether this is intended under the JSpecify unbounded-wildcard rule, so a maintainer decision comes before any code change. Done means that decision is recorded and, if the warning stays, its message names the wildcard rather than only @Nullable.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

(This bug report was generated by Claude Sonnet 5.5 with "high" effort. See also https://github.com/PicnicSupermarket/error-prone-support/pull/2299#issuecomment-6590316084.)

With JSpecifyExperimental, passing a collector with an unbounded-wildcard accumulator type (Collectors.toList(), Guava's toImmutableList(), ...) to a @NullMarked generic method whose accumulator type parameter A has no @Nullable bound reports an inference failure. No @Nullable appears in the source, and no @NullUnmarked code is involved: a real-world example is reactor.core.publisher.Flux#collect(Collector), which is declared <R, A> Mono<R> collect(Collector<? super T, A, ? extends R>) in a @NullMarked package (Reactor 3.8.8). This accounts for 9 of the 15 inference failures we see in our code base.

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import org.jspecify.annotations.NullMarked;

@NullMarked
class Repro1 {
  static class Pipeline<T> {
    <R, A> R collect(Collector<? super T, A, ? extends R> collector) {
      throw new RuntimeException();
    }
  }

  List<String> test(Pipeline<String> pipeline) {
    return pipeline.collect(Collectors.toList());
  }
}

Options: -XepOpt:NullAway:OnlyNullMarked=true -XepOpt:NullAway:JSpecifyMode=true -XepOpt:NullAway:JSpecifyExperimental=true

Repro1.java:15: warning: [NullAway] inference failure: type variable A is constrained to be @Nullable, but its upper bound requires it to be @NonNull
    return pipeline.collect(Collectors.toList());
                           ^

Reproduced with NullAway 0.14.3 and with master (002c08d2), Error Prone 2.51.0, JDK 25. No diagnostic is reported with -XepOpt:NullAway:WarnOnGenericInferenceFailure=false.

Observations:

  • Stream#collect is not flagged for the same collectors.
  • The JDK-free form interface Box<A extends @Nullable Object>, static Box<?> box() and static <A> void take(Box<A> box) reproduces too (take(box())), while Box<? extends Object> does not.
  • This may be working as specified: my reading of the JSpecify specification (section "Bound of an 'unbounded' wildcard") is that in null-marked code ? has an upper bound Object with a UNION_NULL nullness operator, irrespective of the declared bound, so its capture is not within a non-null bound. If so, Flux#collect arguably ought to declare A extends @Nullable Object. But as it flags every Flux#collect(Collectors.xyz()) call, I wonder whether this is intended to surface as a user-facing warning, or whether inference should accept the capture of an unbounded wildcard here.
  • The message mentions @Nullable, though nothing nullable is in sight; it does not mention the wildcard.
Lingua principale
Java
Stelle
4.1k
Fork
381
Merge medio
1g 2h
PR unite (30g)
106

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di uber/NullAway

Tutte le issue di uber/NullAway

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.