Skip to content

Support nullable intersection bounds consistently #1971

Closed

Description

@subhramit

Note: This issue was framed with the help of an agent.
Refs. #1932
Follow-up to #1965

Support nullable intersection bounds consistently

Background

Intersection-bound changes were removed from #1965 to keep that PR focused on call-specific inference variables.

This issue tracks the deferred behavior fixes and test coverage identified by @vlsi.

1. Make inference and explicit type-argument checks agree

Under JSpecify’s “Null-inclusive under every parameterization” rule, an intersection permits null only when every explicit component permits null.

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
  interface A {}
  interface B {}
  static class Both implements A, B {}

  static class Box<T extends @Nullable A & @Nullable B> {}

  static <T extends @Nullable A & @Nullable B> T id(T value) {
    return value;
  }

  void inferred(@Nullable Both value) {
    id(value); // Should be accepted.
  }

  void explicitWitness(@Nullable Both value) {
    Test.<@Nullable Both>id(value); // Should be accepted.
  }

  void explicitClassArgument(Box<@Nullable Both> value) {} // Should be accepted.

  void nullableResult(@Nullable Both value) {
    id(value).hashCode(); // Should report a nullable dereference.
  }
}

The existing checks do not consistently recognize nullability represented on intersection components. In particular, these paths need to agree:

  • GenericsUtils.upperBoundIsNullable
  • GenericsChecks.checkGenericMethodCallTypeArguments
  • GenericsChecks.getTypeParamsWithNullableUpperBound
  • GenericsChecks.substitutedMethodTypeVarUpperBoundIsNullable

Cover all-nullable and mixed-nullness bounds, component order, implicit versus explicit Object, and overriding method bounds. A mixed bound such as @Nullable A & B must still reject nullable instantiations.

The x2, x6, and x8 cases in JSpecify’s IntersectionSupertype.java provide conformance examples.

2. Preserve intersection structure when restoring inferred annotations

With a library model permitting nullable instantiations of T, the following exercises a separate inference problem inside a lambda:

package com.uber.lib.unannotated;

import java.util.List;
import java.util.function.Function;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class NestedAnnots<T extends NestedAnnots.A & NestedAnnots.B> {
  interface A {}
  interface B {}

  // Requires the test library model making T's upper bound nullable.
  NestedAnnots(T value, Function<List<T>, Integer> function) {}

  static <U extends @Nullable Object> List<U> append(List<U> values, U value) {
    return values;
  }

  static void test() {
    new NestedAnnots<>(null, values -> append(values, null).size());
  }
}

The fixture uses the existing nullable-bound model for com.uber.lib.unannotated.NestedAnnots.

#1965 retains declaration provenance on fresh symbols, addressing the lost-model lookup identified in the original report. However, successfully inferred nullability can subsequently be applied to a javac intersection type:

  • On JDK 17, metadata cloning throws Cannot add metadata to an intersection type.
  • On newer javac versions, metadata cloning can replace the intersection with an ordinary ClassType, losing its structural representation.

The JDK 17 crash also occurs on the pre-#1965 base; it is not solely a regression introduced by call-specific inference.

Acceptance criteria and coverage

  • Accept nullable instantiations for all-nullable source-written intersection bounds; reject them for mixed bounds.
  • Keep inferred results, explicit method arguments, class arguments, and override checks consistent.
  • Preserve intersection components, nested annotations, and compiler-owned types during annotation application, restoration, and removal.
  • Do not treat the implicit Object component of an interface-only intersection as an additional non-null constraint.
  • Preserve explicit non-null computed bounds against nullable declaration fallback, while allowing explicit non-null declaration bounds to narrow nullable computed bounds.
  • Verify that nullable reads still warn and non-null reads remain accepted.
  • Validate on JDK 17 and newer supported JDKs.
  • Add distinguishing tests for the deferred helper branches and consumers, including the mutation cases listed in the coverage review, or remove branches that are unnecessary.

Related review discussions

Activity

  1. added
    jspecify
    on Oct 22, 2026
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

    crashfalse positivejspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions