Skip to content

Give the conditional-expression, method-reference, and override reporters the marked generic mismatch message #2207

Backlog

Description

@vlsi

Problem or use case

#1828 reports a generic nullability mismatch at an assignment, an argument, or a return as a marked diff of the two types. The same mismatch reached through a conditional expression, a method reference, or an override still gets the old message, which prints both types in full and no position:

@NullMarked
class Test {
  void f(boolean b, List<?> l, List<Object> o) {
    Collection<? extends Object> c1 = l;         // marked diff, with note
    Collection<? extends Object> c2 = b ? l : o; // old message
  }
}
Conditional expression must have type Collection<? extends Object> but the sub-expression has type List<?>, which has mismatched nullability of type parameters

Example and expected behavior

c2 = b ? l : o reports, at l, the same message as c1 = l (JSpecify mode on):

incompatible nullability in type argument for E of Collection -> upper bound of wildcard: source has @Nullable Object where target requires Object
    source: List<?>
                 ^
    target: Collection<? extends Object>
                                 ^^^^^^
    note: the unbounded wildcard ? in List<?> has the upper bound of type parameter E of List, which is @Nullable Object (JLS 5.1.10)

Proposed solution

Route GenericsChecks.reportMismatchedTypeForTernaryOperator through reportNullabilityMismatch, as the assignment, argument, and return reporters are, and decide the same for the method-reference and override reporters, which ask the same check.

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