Skip to content

Enhance generic method inference to compute mappings from type variables to full types? #1585

Open

Description

@msridhar

Right now, our generic method inference computes only the top-level nullability for each type variable of a generic method. Our assumption was that we could rely on javac to correctly compute the type arguments otherwise. But, as #1473 and #1574 show, sometimes this is not the case, and it makes sense: computing the nullability-enhanced type of an actual parameter (particularly for its type arguments) requires extra logic that javac doesn't include. While our hacks seem to hold up for now, eventually, we probably want to enhance generic method inference to compute full mappings from type variables to (nullability-enhanced) types to fix this properly, relying on javac types where appropriate but not always. This could be fixed alongside #1291 which will also require a re-working of inference.

Activity

  1. added
    jspecify
    on May 25, 2026
  2. msridhar commented on Oct 6, 2026

    @msridhar
    ContributorAuthor

    Another self-contained example motivating a proper reimplementation of generic method inference:

    import org.jspecify.annotations.NullMarked;
    import org.jspecify.annotations.Nullable;
    
    @NullMarked
    public final class Example {
      public static final class Box<T extends @Nullable Object> {
        public <R extends @Nullable Object> Box<R> flatMap(
            Mapper<? super T, ? extends Box<R>> mapper) {
          throw new UnsupportedOperationException();
        }
      }
    
      @FunctionalInterface
      public interface Mapper<P extends @Nullable Object, R extends @Nullable Object> {
        R apply(P value);
      }
    
      static void acceptsNested(Box<Box<@Nullable String>> box) {}
    
      static void reproduce(Box<Box<Box<@Nullable String>>> nested) {
        var flat = nested.flatMap(box -> box);
        acceptsNested(flat);
      }
    }

    NullAway reports:

    Box<Box<@Nullable String>> cannot be converted to Box<Box<String>>
        var flat = nested.flatMap(box -> box);
                                         ^
    
    Box<Box<String>> cannot be converted to Box<Box<@Nullable String>>
        acceptsNested(flat);
                      ^
    

    The correct substitution is R = Box<@Nullable String>, giving flat type Box<Box<@Nullable String>>. Here R itself is non-null; the nullable annotation is inside its substituted type. Recording only the top-level nullability of R does not preserve that information. This is another case where inference needs to compute and propagate a full nullability-enhanced type substitution.

  3. msridhar commented on Oct 9, 2026

    @msridhar
    ContributorAuthor

    Fixing this should address #1942

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

    jspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions