Skip to content

JSpecify mode: NullPointerException when passing a value read through a ? super wildcard receiver #1846

Closed

Description

@vlsi

With JSpecify mode on, NullAway crashes with an unhandled NullPointerException while checking takesObject(node.get()), where node is Node<? super Integer> and Node's type parameter is bounded by Comparable<Integer>.

Versions: NullAway master at de6f2e6 (0.14.2-SNAPSHOT), and the 0.14.1 release, which throws the same exception from the same call. Error Prone 2.50.0, JDK 21.0.9. Flags: -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true -XepOpt:NullAway:JSpecifyExperimental=true -XepOpt:NullAway:OnlyNullMarked=true.

Code sample. I ran it as a test in nullaway/src/test/java/com/uber/nullaway/jspecify/, with the helper WildcardTests uses: makeTestHelperWithArgs(JSpecifyJavacConfig.withJSpecifyModeArgs(Arrays.asList("-XepOpt:NullAway:OnlyNullMarked=true"))).

import org.jspecify.annotations.NullMarked;
@NullMarked
class Test {
  interface Node<T extends Comparable<Integer>> {
    T get();
  }
  static void takesObject(Object value) {}
  static void superBounded(Node<? super Integer> node) {
    takesObject(node.get());
  }
}

Expected: no crash and no NullAway diagnostic, as for the same call with Node<Integer>, Node<?>, or Node<? extends Integer>.

Actual (stack trace cut after the first Error Prone frame):

/Test.java:9: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
    takesObject(node.get());
               ^
     Please report this at https://github.com/google/error-prone/issues/new and include the following, as well as a reproducing code sample (if possible):

     error-prone version: 2.50.0
     BugPattern: NullAway
     Stack Trace:
     java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Type.getAnnotationMirrors()" because "other" is null
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.updateDirectNullabilityAnnotationsForType(TypeSubstitutionUtils.java:574)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitClassType(TypeSubstitutionUtils.java:378)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitClassType(TypeSubstitutionUtils.java:337)
  	at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.accept(Type.java:1050)
  	at jdk.compiler/com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4900)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitTypeLists(TypeSubstitutionUtils.java:622)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitClassType(TypeSubstitutionUtils.java:385)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitClassType(TypeSubstitutionUtils.java:337)
  	at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.accept(Type.java:1050)
  	at jdk.compiler/com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4900)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitClassType(TypeSubstitutionUtils.java:375)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils$RestoreNullnessAnnotationsVisitor.visitClassType(TypeSubstitutionUtils.java:337)
  	at jdk.compiler/com.sun.tools.javac.code.Type$ClassType.accept(Type.java:1050)
  	at jdk.compiler/com.sun.tools.javac.code.Types$DefaultTypeVisitor.visit(Types.java:4900)
  	at com.uber.nullaway.generics.TypeSubstitutionUtils.restoreExplicitNullabilityAnnotations(TypeSubstitutionUtils.java:107)
  	at com.uber.nullaway.generics.GenericsChecks.getTreeType(GenericsChecks.java:863)
  	at com.uber.nullaway.generics.GenericsChecks.getTreeType(GenericsChecks.java:729)
  	at com.uber.nullaway.generics.GenericsChecks.lambda$compareGenericTypeParameterNullabilityForCall$0(GenericsChecks.java:2545)
  	at com.uber.nullaway.InvocationArguments.forEach(InvocationArguments.java:91)
  	at com.uber.nullaway.generics.GenericsChecks.compareGenericTypeParameterNullabilityForCall(GenericsChecks.java:2495)
  	at com.uber.nullaway.NullAway.handleInvocation(NullAway.java:2251)
  	at com.uber.nullaway.NullAway.matchMethodInvocation(NullAway.java:447)
  	at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:541)

What does not crash, in the same run on de6f2e6, each in the same @NullMarked class:

  • the same Node<T extends Comparable<Integer>> with takesObject(node.get()) for Node<Integer>, Node<?>, and Node<? extends Integer>;
  • Node<? super Integer> when the value is assigned (Object value = node.get();) or used as a receiver (node.get().hashCode()) instead of passed as an argument;
  • Node<? super Integer> passed as above when the bound is Number or Object instead of Comparable<Integer>, and Node<? super ArrayList<String>> with the bound List<String>.

#1715 has the same exception message and was closed by #1717. Its trace enters through checkTypeParameterNullnessForFunctionReturnType and visitWildcardType; this one enters through compareGenericTypeParameterNullabilityForCall and still reproduces on master after that fix.

Assisted-by: Claude Code (claude-opus-5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    crashjspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions