Skip to content

Unexpected errors from contract checking for !null -> !null contracts #1440

Closed

Description

@wilkinsona

Using NullAway 0.13.0, we're seeing unexpected errors reported when contract checking is enabled. It seems to be related to !null contracts such as the following three variants of the same method:

@Contract("!null -> !null")
public static @Nullable Integer of(@Nullable String text) {
	if (text != null) {
		return Integer.parseInt(text);
	}
	else {
		return null;
	}
}
@Contract("!null -> !null")
public static @Nullable Integer of(@Nullable String text) {
	if (text != null) {
		return Integer.parseInt(text);
	}
	return null;
}
@Contract("!null -> !null")
public static @Nullable Integer of(@Nullable String text) {
	return (text != null) ? Integer.parseInt(text) : null;
}

FWIW, IntelliJ's null checker reports no problems with any of these.

If it's useful, you can see more examples of some unexpected failures in spring-gradle-plugins/nullability-plugin#10.

Activity

  1. msridhar commented on Jan 21, 2026

    @msridhar
    Contributor

    I looked at this a bit and it seems the contract checking code is quite over-simplified and wrong for this case. I'm surprised we didn't notice before. It will need a bit of work to fix.

  2. msridhar commented on Jan 21, 2026

    @msridhar
    Contributor

    @wilkinsona I have a potential fix for this up in #1441. Is there some way I could test using a snapshot build on the Spring project where you were seeing issues? Was it spring-framework?

  3. deleted a comment from giruuuuj on Jan 21, 2026
  4. deleted a comment from giruuuuj on Jan 21, 2026
  5. wilkinsona commented on Jan 22, 2026

    @wilkinsona
    Author

    Thanks very much, @msridhar. It was Spring Boot's build rather than Spring Framework's. I'm not too sure how easy it'll be for you to try a NullAway snapshot in our build. Let me see if I can build NullAway locally and try it out.

  6. wilkinsona commented on Jan 22, 2026

    @wilkinsona
    Author

    Things are better but not completely fixed. Here are a couple of build scans showing the failures:

    The source that's referenced in these scans is https://github.com/spring-projects/spring-boot/tree/9fde74494d3534d350f77d39d0cfb2420b643fbf.

    One simplified example of something that's still failing with #1441 in place is the following:

    private static final Pattern WHITE_SPACE_PATTERN = Pattern.compile("\\s+");
    
    @Contract("!null -> !null")
    private @Nullable String removeLineBreaks(@Nullable String description) {
    	return (description != null) ? WHITE_SPACE_PATTERN.matcher(description).replaceAll(" ") : null;
    }

    And here's another:

    public class PemContent {
    
    	private final String text;
    
    	private PemContent(String text) {
    		this.text = text;
    	}
    
    	@Contract("!null -> !null")
    	public static @Nullable PemContent of(@Nullable String text) {
    		return (text != null) ? new PemContent(text) : null;
    	}
    
    }
  7. msridhar commented on Jan 22, 2026

    @msridhar
    Contributor

    Thanks so much for testing, @wilkinsona! I think the gap was in handling of returned ternary expressions. I've updated #1441 to fix those now. Any chance you could give it another shot?

  8. wilkinsona commented on Jan 23, 2026

    @wilkinsona
    Author

    I'm now seeing a crash during compilation within the contract checking:

    /Users/aw036093/dev/spring-projects/spring-boot/4.0.x/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/WebApplicationContextRunner.java:88: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
            public static @Nullable Supplier<ConfigurableWebApplicationContext> withMockServletContext(
                                                                                ^
         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.46.0
         BugPattern: NullAway
         Stack Trace:
         org.checkerframework.nullaway.javacutil.BugInCF: Problem with initial store for CFGLambda(
      ()->{
          ConfigurableWebApplicationContext context = contextFactory.get();
          context.setServletContext(new MockServletContext());
          return context;
      }
      ), parameters=[]
            at org.checkerframework.nullaway.dataflow.analysis.ForwardAnalysisImpl.initInitialInputs(ForwardAnalysisImpl.java:351)
            at org.checkerframework.nullaway.dataflow.analysis.AbstractAnalysis.init(AbstractAnalysis.java:554)
            at org.checkerframework.nullaway.dataflow.analysis.ForwardAnalysisImpl.performAnalysis(ForwardAnalysisImpl.java:102)
            at com.uber.nullaway.dataflow.RunOnceForwardAnalysisImpl.performAnalysis(RunOnceForwardAnalysisImpl.java:31)
            at com.uber.nullaway.dataflow.DataFlow.dataflow(DataFlow.java:163)
            at com.uber.nullaway.dataflow.DataFlow.resultFor(DataFlow.java:329)
            at com.uber.nullaway.dataflow.DataFlow.resultForExpr(DataFlow.java:307)
            at com.uber.nullaway.dataflow.DataFlow.expressionDataflow(DataFlow.java:230)
            at com.uber.nullaway.dataflow.AccessPathNullnessAnalysis.getNullnessForContractDataflow(AccessPathNullnessAnalysis.java:143)
            at com.uber.nullaway.handlers.contract.ContractCheckHandler$1.visitReturn(ContractCheckHandler.java:162)
            at com.uber.nullaway.handlers.contract.ContractCheckHandler$1.visitReturn(ContractCheckHandler.java:144)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1780)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:95)
            at jdk.compiler/com.sun.source.util.TreeScanner.scan(TreeScanner.java:110)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitBlock(TreeScanner.java:271)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1148)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:95)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitLambdaExpression(TreeScanner.java:648)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCLambda.accept(JCTree.java:2042)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:95)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitConditionalExpression(TreeScanner.java:471)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCConditional.accept(JCTree.java:1592)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitReturn(TreeScanner.java:546)
            at com.uber.nullaway.handlers.contract.ContractCheckHandler$1.visitReturn(ContractCheckHandler.java:189)
            at com.uber.nullaway.handlers.contract.ContractCheckHandler$1.visitReturn(ContractCheckHandler.java:144)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCReturn.accept(JCTree.java:1780)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at jdk.compiler/com.sun.source.util.TreeScanner.scan(TreeScanner.java:110)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitBlock(TreeScanner.java:271)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:1148)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:95)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitMethod(TreeScanner.java:223)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:992)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:66)
            at com.uber.nullaway.handlers.contract.ContractCheckHandler.onMatchMethod(ContractCheckHandler.java:191)
            at com.uber.nullaway.handlers.CompositeHandler.onMatchMethod(CompositeHandler.java:86)
            at com.uber.nullaway.NullAway.matchMethod(NullAway.java:686)
            at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:539)
            at com.google.errorprone.scanner.ErrorProneScanner.visitMethod(ErrorProneScanner.java:881)
            at com.google.errorprone.scanner.ErrorProneScanner.visitMethod(ErrorProneScanner.java:178)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:992)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at com.google.errorprone.scanner.Scanner.scan(Scanner.java:74)
            at com.google.errorprone.scanner.Scanner.scan(Scanner.java:48)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:95)
            at jdk.compiler/com.sun.source.util.TreeScanner.scan(TreeScanner.java:110)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:118)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitClass(TreeScanner.java:202)
            at com.google.errorprone.scanner.ErrorProneScanner.visitClass(ErrorProneScanner.java:649)
            at com.google.errorprone.scanner.ErrorProneScanner.visitClass(ErrorProneScanner.java:178)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:899)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
            at com.google.errorprone.scanner.Scanner.scan(Scanner.java:74)
            at com.google.errorprone.scanner.Scanner.scan(Scanner.java:48)
            at jdk.compiler/com.sun.source.util.TreeScanner.scan(TreeScanner.java:110)
            at jdk.compiler/com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:118)
            at jdk.compiler/com.sun.source.util.TreeScanner.visitCompilationUnit(TreeScanner.java:151)
            at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:661)
            at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:178)
            at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:627)
            at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:66)
            at com.google.errorprone.scanner.Scanner.scan(Scanner.java:58)
            at com.google.errorprone.scanner.ErrorProneScannerTransformer.apply(ErrorProneScannerTransformer.java:43)
            at com.google.errorprone.ErrorProneAnalyzer.finished(ErrorProneAnalyzer.java:231)
            at jdk.compiler/com.sun.tools.javac.api.MultiTaskListener.finished(MultiTaskListener.java:133)
            at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1423)
            at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1370)
            at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:955)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
            at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
            at org.gradle.internal.compiler.java.IncrementalCompileTask.call(IncrementalCompileTask.java:92)
            at org.gradle.api.internal.tasks.compile.AnnotationProcessingCompileTask.call(AnnotationProcessingCompileTask.java:94)
            at org.gradle.api.internal.tasks.compile.ResourceCleaningCompilationTask.call(ResourceCleaningCompilationTask.java:57)
            at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:83)
            at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:50)
            at org.gradle.api.internal.tasks.compile.daemon.AbstractIsolatedCompilerWorkerExecutor$CompilerWorkAction.execute(AbstractIsolatedCompilerWorkerExecutor.java:79)
            at org.gradle.workers.internal.DefaultWorkerServer.execute(DefaultWorkerServer.java:68)
            at org.gradle.workers.internal.AbstractClassLoaderWorker$1.create(AbstractClassLoaderWorker.java:54)
            at org.gradle.workers.internal.AbstractClassLoaderWorker$1.create(AbstractClassLoaderWorker.java:48)
            at org.gradle.internal.classloader.ClassLoaderUtils.executeInClassloader(ClassLoaderUtils.java:102)
            at org.gradle.workers.internal.AbstractClassLoaderWorker.executeInClassLoader(AbstractClassLoaderWorker.java:48)
            at org.gradle.workers.internal.FlatClassLoaderWorker.run(FlatClassLoaderWorker.java:32)
            at org.gradle.workers.internal.FlatClassLoaderWorker.run(FlatClassLoaderWorker.java:22)
            at org.gradle.workers.internal.WorkerDaemonServer.run(WorkerDaemonServer.java:109)
            at org.gradle.workers.internal.WorkerDaemonServer.run(WorkerDaemonServer.java:78)
            at org.gradle.process.internal.worker.request.WorkerAction.lambda$run$1(WorkerAction.java:150)
            at org.gradle.process.internal.worker.child.WorkerLogEventListener.withWorkerLoggingProtocol(WorkerLogEventListener.java:41)
            at org.gradle.process.internal.worker.request.WorkerAction.lambda$run$2(WorkerAction.java:150)
            at org.gradle.internal.operations.CurrentBuildOperationRef.with(CurrentBuildOperationRef.java:84)
            at org.gradle.process.internal.worker.request.WorkerAction.run(WorkerAction.java:142)
            at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
            at java.base/java.lang.reflect.Method.invoke(Method.java:565)
            at org.gradle.internal.dispatch.MethodInvocation.invokeOn(MethodInvocation.java:77)
            at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:28)
            at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:19)
            at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
            at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
            at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
            at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
            at org.gradle.internal.concurrent.AbstractManagedExecutor$1.run(AbstractManagedExecutor.java:47)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1090)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:614)
            at java.base/java.lang.Thread.run(Thread.java:1474)
      Caused by: java.lang.Throwable
            at org.checkerframework.nullaway.javacutil.BugInCF.<init>(BugInCF.java:26)
            ... 110 more
    

    This is the code that triggered the failure.

  9. msridhar commented on Jan 23, 2026

    @msridhar
    Contributor

    Thanks for the example! I know exactly what's going wrong (returns in nested lambdas). Really appreciate you doing these test runs!

  10. msridhar commented on Jan 23, 2026

    @msridhar
    Contributor

    Ok, @wilkinsona that latest bug is fixed now. Could you give it one more shot? Sorry to ask; with instructions I could try it myself, but you indicated it could be a bit of a pain.

  11. msridhar commented on Jan 23, 2026

    @msridhar
    Contributor

    Hi @wilkinsona I decided to go ahead and merge #1441 since it clearly fixes a few issues and the PR was getting big. If you could still test again with a snapshot off the master branch that'd be great. If it doesn't work, I'd appreciate if you could file a new issue.

  12. added a commit that references this issue on Jan 24, 2026
    fc6d956
  13. marcphilipp commented on Jan 24, 2026

    @marcphilipp

    We're facing the same issue when trying to upgrade to 0.13.0 in the junit-framework build. I tried using the latest version from the master branch but I get the same error: https://ge.junit.org/s/dekvw4r4zkxuq/failures#1

  14. marcphilipp commented on Jan 24, 2026

    @marcphilipp

    I opened a follow-up issue because the problem seems to persist (at least for me):

  15. msridhar commented on Jan 25, 2026

    @msridhar
    Contributor

    @wilkinsona would jt be helpful to you if we cut a release with these fixes? Please let us know. Thanks!

  16. wilkinsona commented on Jan 26, 2026

    @wilkinsona
    Author

    Yes please, @msridhar. I've tested the Spring Boot and Spring Framework builds with 4c619e8 and contract checking enabled. The crash is fixed and I didn't see any false-positive violations either.

  17. msridhar commented on Jan 27, 2026

    @msridhar
    Contributor

    @wilkinsona we just cut release 0.13.1 which includes the fixes for this issue. Thanks so much again with the testing help!

  18. wilkinsona commented on Jan 27, 2026

    @wilkinsona
    Author

    My pleasure, thanks for the super-quick fixes and release.

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