Skip to content

JSpecify: a wildcard type argument of an unannotated generic type is rejected in @NullMarked code #1778

Closed

Description

@vlsi

In @NullMarked code, reading a generic member off a wildcard-typed value fails when the generic type itself is unannotated. The type variable of the unannotated type gets a @Nullable Object upper bound, and the wildcard then no longer converts to the wildcard of an annotated type.

This is the shape left over from #1732. #1764 fixed the annotated cases there, such as Collection<?>.removeAll, and this one still reports.

Reproducer

// No @NullMarked on this file, its package, or its module.
package example.unannotated;

public interface Box<V> {
  Class<V> valueType();
}
package example.annotated;

import example.unannotated.Box;
import org.jspecify.annotations.NullMarked;

@NullMarked
public class Use {
  public Class<?> valueType(Box<?> box) {
    return box.valueType();
  }
}
Use.java:8: warning: [NullAway] incompatible types: Class<capture of ?> cannot be converted to Class<?> (target wildcard upper bound is Object; source wildcard upper bound is @Nullable Object; source wildcard is the type argument for type variable V of Box)

Box is unannotated, so I expect its V to carry unspecified nullness and the read to pass. Two changes silence it: annotating Box with @NullMarked, or naming the type argument as Box<String>. In annotated code that leaves a raw type or a suppression as the only ways out.

Where it comes from

Apache Calcite reads region.getAttributes().getValueConstraint() off Apache Geode's Region<?, ?>, and geode-core ships unannotated bytecode. The workaround today is a raw Class plus @SuppressWarnings("rawtypes"): GeodeUtils.autodetectRelTypeFromRegion.

Versions

Reproduces on 0.14.0 and on main at 915b287. Error Prone 2.50.0, JSpecify 1.0.1, JDK 21, with JSpecifyMode, JSpecifyExperimental, HandleWildcardGenerics, JSpecifyJDKModels, and OnlyNullMarked all enabled.

One case I assume is intended

ImmutableMap.copyOf(map) on a Map<String, ?> reports inference failure: type variable V is constrained to be @Nullable, but its upper bound requires it to be @NonNull, because Map's V has a @Nullable Object bound while Guava's ImmutableMap requires a non-null one. A Map<String, ?> can hold nulls and copyOf would throw on them, so I read that one as a true positive. Say so if it is not, and I will file it separately.

Activity

  1. msridhar commented on Aug 29, 2026

    @msridhar
    Contributor

    I think this error is valid. The key is that in null-marked code, an unbounded wildcard has an implicit upper bound of @Nullable Object:

    https://jspecify.dev/docs/spec/#unbounded-wildcard

    For the capture of this wildcard, that bound gets combined with the Object UNSPECIFIED upper bound of V, yielding @Nullable Object as its upper bound. But, in the JSpecify JDK model, the T type variable of Class has a @NonNull upper bound:

    https://github.com/jspecify/jdk/blob/397fbb20a940e9ae679b268358e6f74396a8938c/src/java.base/share/classes/java/lang/Class.java#L218-L220

    So we have an incompatibility.

    @vlsi do you agree?

  2. msridhar commented on Aug 29, 2026

    @msridhar
    Contributor

    For your original code, can you write Region<? extends Object, ? extends Object> region as a workaround?

  3. vlsi commented on Aug 29, 2026

    @vlsi
    ContributorAuthor

    Agreed. The raw Class was hiding a real incompatibility rather than working around a defect, and the same reading explains the Guava case I put at the bottom of the report.

    Region<? extends Object, ? extends Object> works, and on 0.14.0, so nothing here waits on a release. Both sites are clean with the raw type and both suppressions gone: GeodeUtils.autodetectRelTypeFromRegion and DataContexts, where the bound also moves up to the public DataContexts.of factory. It is the same Java type either way, so callers compile unchanged.

    Please close this one. Thanks for the quick read.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions