Skip to content

Generic mismatch message drops a custom nullable annotation and reports source has String where target requires String #2226

Backlog

Description

@vlsi

With CustomNullableAnnotations naming a type-use annotation, a type-argument mismatch on a node that carries that annotation prints both sides identically. NullAway rejects the call because it reads the configured annotation, but the message cannot show why.

Compiled with -XepOpt:NullAway:OnlyNullMarked=true -XepOpt:NullAway:CustomNullableAnnotations=Test.CanBeNull in JSpecify mode:

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;
import org.jspecify.annotations.NullMarked;

@NullMarked
class Test {
  @Target(ElementType.TYPE_USE)
  @interface CanBeNull {}

  static void take(List<String> l) {}
  static void takeN(List<@CanBeNull String> l) {}
  static void takeMap(Map<String, List<String>> m) {}

  void a(List<@CanBeNull String> l) {
    take(l);
  }

  void b(List<String> l) {
    takeN(l);
  }

  void c(Map<String, List<@CanBeNull String>> m) {
    takeMap(m);
  }
}

At the head of #1828 (d75bba5), NullAway reports:

/Test.java:14: warning: [NullAway] incompatible nullability in type argument for E of List: source has String where target requires String
    take(l);
         ^
    source: List<String>
                 ^^^^^^
    target: List<String>
                 ^^^^^^

/Test.java:17: warning: [NullAway] incompatible nullability in type argument for E of List: a non-wildcard type argument must have the same nullability in source and target; source has String where target requires String
    takeN(l);
          ^
    source: List<String>
                 ^^^^^^
    target: List<String>
                 ^^^^^^

/Test.java:20: warning: [NullAway] incompatible nullability in type argument for V of Map -> type argument for E of List: source has String where target requires String
    takeMap(m);
            ^
    source: Map<String, List<String>>
                             ^^^^^^
    target: Map<String, List<String>>
                             ^^^^^^

Expected: the node that carries the configured annotation prints with it, so that the two sides differ where the check found them to differ, for example:

incompatible nullability in type argument for E of List: source has @CanBeNull String where target requires String
    source: List<@CanBeNull String>
                 ^^^^^^^^^^^^^^^^^
    target: List<String>
                 ^^^^^^

The exact form is open (the annotation under its own simple name, or @Nullable for every annotation the configuration treats as nullable); what matters is that a message never names two identical types as the mismatch.

CaretTests.printedTypesShowOnlyAnAnnotationNamedNullable currently pins that @CanBeNull is dropped where the mismatch is elsewhere in the type, and the Javadoc of GenericsChecks.prettyTypeForError and the class comment of GenericTypePrettyPrintingVisitor state the Nullable-only rule; both change with this.

Analysis, from reading the source (the report above stands without it): GenericTypePrettyPrintingVisitor.appendNullableAnnotationIfPresent prints an annotation only where its simple name is Nullable, while the check decides nullability with GenericsUtils.isAnnotatedAsNullable(type, config), which honors CustomNullableAnnotations.

Found in the review of #1828.

Activity

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