Skip to content

JSpecify JDK models: an unbounded wildcard in a bytecode signature rejects a nullable type argument #1732

Closed

Description

@vlsi

Version

NullAway 0.14.0, and current master (21f02dd). Error Prone 2.50.0, JDK 21.

Flags: -XepOpt:NullAway:JSpecifyMode=true -XepOpt:NullAway:JSpecifyJDKModels=true -XDaddTypeAnnotationsToSymbol=true.

Summary

A Collection<?> parameter declared in a @NullMarked class read from bytecode rejects an argument of type Collection<@Nullable String>. The same Collection<?> written in annotated source accepts it.

incompatible types: Collection<@Nullable String> cannot be converted to Collection<?>

An unbounded wildcard should accept any type argument, and it does everywhere except in a signature that came from bytecode.

Reproducer

package foo;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
  static boolean containsAll(List<String> l, Collection<@Nullable String> o) {
    // BUG: Diagnostic contains: incompatible types: Collection<@Nullable String> cannot be converted to Collection<?>
    return l.containsAll(o);
  }

  // a JDK static method with the same parameter shape and no receiver: both arguments report
  static boolean disjoint(Collection<@Nullable String> a, Collection<@Nullable String> b) {
    // BUG: Diagnostic contains: incompatible types: Collection<@Nullable String> cannot be converted to Collection<?>
    return Collections.disjoint(a, b);
  }

  // the same parameter type, declared in source: clean
  static boolean ownStatic(Collection<?> c) {
    return c.isEmpty();
  }

  static boolean callOwnStatic(Collection<@Nullable String> o) {
    return ownStatic(o);
  }

  // assignment to a source-declared Collection<?>: clean
  static Collection<?> assign(Collection<@Nullable String> o) {
    return o;
  }
}

removeAll and retainAll behave like containsAll.

What does and does not report

Call Reported
list.containsAll(Collection<@Nullable String>) yes
list.removeAll(Set<@Nullable String>) yes
list.retainAll(Collection<@Nullable String>) yes
Collections.disjoint(Collection<@Nullable String>, ...) — static, no receiver yes
list.contains(@Nullable String) — single-element form no
list.removeAll(Collection<String>) — non-null argument elements no
source-declared f(Collection<?>) called with Collection<@Nullable String> no
source-declared f(Collection<? extends @Nullable Object>) no
Collection<?> c = someCollectionOfNullable; no

The receiver's element type does not matter: List<@Nullable String>.removeAll(Collection<@Nullable String>) reports, and List<@Nullable String>.removeAll(Collection<String>) does not. Only the argument's element type decides, and Collections.disjoint shows that no receiver need be involved at all.

What appears to be going on

jspecify-jdk.astubx carries no method entry for containsAll, removeAll, retainAll, or contains on Collection, List, or Set. Dumping the parsed model, what it does record for java.util.Collection is that the class is @NullMarked and that its type parameter E has a @Nullable upper bound.

So the parameter type is read from the bytecode signature, and the unbounded ? in it ends up behaving like ? extends Object with a non-null bound rather than an unbounded wildcard. Writing the identical signature in source gives the correct behavior, which is what makes the two paths disagree.

This looks adjacent to #1717, which recently fixed restoring nullness annotations from an unbounded wildcard to the captured type.

Expected

Collection<@Nullable String> is a Collection<?>. A signature read from bytecode should treat an unbounded wildcard the same way a signature read from source does.

Workaround

@SuppressWarnings("NullAway") on the calling method.

Where we hit it

SqlNodeList delegates all three bulk methods to a List<@Nullable SqlNode>, and SubstitutionVisitor removes a set of parents of which the root's is null. Found while replacing the Checker Framework with NullAway in Apache Calcite (apache/calcite#5213).

Activity

  1. retrodaredevil commented on Aug 26, 2026

    @retrodaredevil

    I'm facing the same issue. Here's two ways I reproduced it:

    import org.jspecify.annotations.NullMarked;
    import reactor.core.publisher.Flux;
    
    import java.util.stream.Collectors;
    
    @NullMarked
    public class NullAwayCollectorInferenceTest {
    	public static void test() {
    		long count = Flux.just("value")
    				.collect(Collectors.counting())
    				.blockOptional()
    				.orElseThrow();
    		if (count != 1L) {
    			throw new AssertionError(count);
    		}
    	}
    }
    

    or without Reactor Core APIs:

    import org.jspecify.annotations.NullMarked;
    
    import java.util.stream.Collector;
    import java.util.stream.Collectors;
    
    @NullMarked
    public class NullAwayCollectorInferenceTest {
    	public static void test() {
    		long count = collect("value", Collectors.counting());
    		if (count != 1L) {
    			throw new AssertionError(count);
    		}
    	}
    
    	private static <T, A, R> R collect(T value, Collector<? super T, A, ? extends R> collector) {
    		A accumulator = collector.supplier().get();
    		collector.accumulator().accept(accumulator, value);
    		return collector.finisher().apply(accumulator);
    	}
    }
    
  2. msridhar commented on Aug 27, 2026

    @msridhar
    Contributor

    Still working on this one, but as an FYI, I think this is fixed if you compile using JDK 25. Working on a JDK 17/21 fix.

  3. vlsi commented on Aug 28, 2026

    @vlsi
    ContributorAuthor

    You are right, one should use the latest compiler. Don't spend to much time on adding workarounds for older compilers.

    For now, Calcite uses "current JAVA_HOME" for compilation which to me is just wrong. It should always use JDK 25 for javac and target the release via --release.

  4. msridhar commented on Aug 28, 2026

    @msridhar
    Contributor

    You are right, one should use the latest compiler. Don't spend to much time on adding workarounds for older compilers.

    Well, we do officially support JDK 17 and 21 (with -XDaddTypeAnnotationsToSymbol=true). So we should fix this one.

    For now, Calcite uses "current JAVA_HOME" for compilation which to me is just wrong. It should always use JDK 25 for javac and target the release via --release.

    Agreed! Or even JDK 26. javac gets some bug fixes and improvements in every version, and --release works well in most cases.

  5. vlsi commented on Aug 28, 2026

    @vlsi
    ContributorAuthor

    Or even JDK 26

    AFAIK JDK 26 does not support --release 8 while Calcite and pgjdbc officially support Java 8 :)

  6. msridhar commented on Aug 28, 2026

    @msridhar
    Contributor

    AFAIK JDK 26 does not support --release 8 while Calcite and pgjdbc officially support Java 8 :)

    Looks like it's supported but causes warnings:

    $ javac -version
    javac 26.0.2.1
    $ javac --release 8 Test.java
    warning: [options] source value 8 is obsolete and will be removed in a future release
    warning: [options] target value 8 is obsolete and will be removed in a future release
    warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
    3 warnings
    
  7. 24 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions