Skip to content

Invalid override error in JSpecify mode #1139

Closed

Description

@msridhar

I think this might be another example of the same:

@NullMarked
class Foo {
    static abstract class MyClass<T extends @Nullable Object>  {
        abstract T doThing(T value);
    }

    static void repro() {
      new MyClass<@Nullable Object>() {
        @Override
        @Nullable Object doThing(@Nullable Object value) {
            return value;
        }
      }.doThing(null);
    }
}
Foo.java:14: warning: [NullAway] method returns @Nullable, but superclass method Foo.MyClass.doThing(T) returns @NonNull
        @Nullable Object doThing(@Nullable Object value) {
                         ^
    (see http://t.uber.com/nullaway )

Originally posted by @agrieve in #1126

Activity

  1. msridhar commented on Jan 31, 2025

    @msridhar
    ContributorAuthor

    @agrieve I can't reproduce this error with JSpecify mode enabled with NullAway 0.12.3. It does reproduce without JSpecify mode enabled. Is that what you are seeing?

  2. agrieve commented on Feb 5, 2025

    @agrieve
    Contributor

    Ah, so the error goes away if I use jspecify's @nullable, but when I use Chrome's @Nullable, the error shows.

    Our @Nullable is here.

    Here's the list of compiler flags we're using. We don't set any flags related to which @Nullable to use, as we assume so long as it's called "@nullable" it should be treated the same.

    ../../third_party/jdk/current/bin/javac -g --release 17 \
        -encoding UTF-8 -sourcepath : -Xlint:-dep-ann -Xlint:-removal -J-XX:+PerfDisableSharedMem -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED -XDcompilePolicy=simple '-Xplugin:ErrorProne -XepOpt:NullAway:AnnotatedPackages= -XepOpt:NullAway:CustomContractAnnotations=org.chromium.build.annotations.Contract -XepOpt:NullAway:CheckContracts=true -XepOpt:NullAway:CastToNonNullMethod=org.chromium.build.NullUtil.assumeNonNull -XepOpt:NullAway:AssertsEnabled=true -XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true -XepOpt:Nullaway:AcknowledgeAndroidRecent=true -XepOpt:NullAway:JSpecifyMode=true -XepOpt:NullAway:KnownInitializers=android.app.Application.onCreate,android.app.Activity.onCreate,android.app.Service.onCreate,android.app.backup.BackupAgent.onCreate,android.content.ContentProvider.attachInfo,android.content.ContentProvider.onCreate,android.content.ContentWrapper.attachBaseContext -XepAllErrorsAsWarnings -XepDisableWarningsInGeneratedCode -Xep:InlineMeInliner:OFF -Xep:InlineMeSuggester:OFF -Xep:HidingField:OFF -Xep:AlreadyChecked:OFF -Xep:DirectInvocationOnMock:OFF -Xep:MockNotUsedInProduction:OFF -Xep:JdkObsolete:OFF -Xep:ReturnValueIgnored:OFF -Xep:StaticAssignmentInConstructor:OFF -Xep:InvalidBlockTag:OFF -Xep:InvalidParam:OFF -Xep:InvalidLink:OFF -Xep:InvalidInlineTag:OFF -Xep:MalformedInlineTag:OFF -Xep:MissingSummary:OFF -Xep:UnescapedEntity:OFF -Xep:UnrecognisedJavadocTag:OFF -Xep:MutablePublicArray:OFF -Xep:NonCanonicalType:OFF -Xep:DoNotClaimAnnotations:OFF -Xep:JavaUtilDate:OFF -Xep:IdentityHashMapUsage:OFF -Xep:StaticMockMember:OFF -Xep:StaticAssignmentOfThrowable:OFF -Xep:CatchAndPrintStackTrace:OFF -Xep:TypeParameterUnusedInFormals:OFF -Xep:DefaultCharset:OFF -Xep:FutureReturnValueIgnored:OFF -Xep:ThreadJoinLoop:OFF -Xep:StringSplitter:OFF -Xep:ClassNewInstance:OFF -Xep:ThreadLocalUsage:OFF -Xep:EqualsHashCode:OFF -Xep:OverrideThrowableToString:OFF -Xep:UnsafeReflectiveConstructionCast:OFF -Xep:MixedMutabilityReturnType:OFF -Xep:EqualsGetClass:OFF -Xep:UndefinedEquals:OFF -Xep:SameNameButDifferent:OFF -Xep:UnnecessaryLambda:OFF -Xep:EmptyCatch:OFF -Xep:BadImport:OFF -Xep:UseCorrectAssertInTests:OFF -Xep:RefersToDaggerCodegen:OFF -Xep:RemoveUnusedImports:OFF -Xep:UnicodeEscape:OFF -Xep:NonApiType:OFF -Xep:StringCharset:OFF -Xep:StringCaseLocaleUsage:OFF -Xep:RedundantControlFlow:OFF -Xep:BinderIdentityRestoredDangerously:WARN -Xep:EmptyIf:WARN -Xep:EqualsBrokenForNull:WARN -Xep:InvalidThrows:WARN -Xep:LongLiteralLowerCaseSuffix:WARN -Xep:MultiVariableDeclaration:WARN -Xep:RedundantOverride:WARN -Xep:StaticQualifiedUsingExpression:WARN -Xep:TimeUnitMismatch:WARN -Xep:UnnecessaryStaticImport:WARN -Xep:UseBinds:WARN -Xep:WildcardImport:WARN -Xep:NoStreams:WARN' -XDshould-stop.ifError=FLOW -proc:none -processorpath lib.java/tools/android/errorprone_plugin/errorprone_plugin.jar:lib.java/build/android/build_java.jar:lib.java/third_party/android_build_tools/error_prone/error_prone_java.jar:lib.java/third_party/android_build_tools/error_prone_javac/error_prone_javac_java.jar:../../third_party/android_build_tools/nullaway/cipd/nullaway.jar \                                                                  -classpath ../../third_party/android_deps/cipd/libs/org_jspecify_jspecify/jspecify-1.0.0.jar:obj/build/android/build_java.turbine.jar:. \
        "$@"
    
  3. msridhar commented on Feb 5, 2025

    @msridhar
    ContributorAuthor

    Yeah, you're right, good catch. There are some places where we previously hardcoded support for the JSpecify @Nullable but I don't see any reason to do that anymore; we should just allow any TYPE_USE @Nullable. Will fix.

  4. msridhar commented on Feb 6, 2025

    @msridhar
    ContributorAuthor

    @agrieve so it turns out for annoying implementation reasons that it'll be tough to make things work completely if we don't know of some type-use @Nullable annotation that will be in the classpath. Previously we were assuming the JSpecify @Nullable annotation would be in the classpath, but that's not true in your case I think. Honestly, I'm kind of surprised that nothing else has broken for you in JSpecify mode before this.

    My question is: do you have a strong reason not to have a dependence on JSpecify? If so, I'm curious what it is. If we can assume the JSpecify annotation will be in the classpath that will make things simpler for us.

  5. agrieve commented on Feb 6, 2025

    @agrieve
    Contributor

    Sounds like it's hard to detect that our custom @Nullable is TYPE_USE? Would be really slick if nullaway could automatically use "jspecify mode" for TYPE_USE annotations, and non-jspecify mode otherwise.

    My rationale for using a custom annotation:

    • NullAway has other annotations as well (@Contract, @EnsuresNonNull, etc), and it's convenient for build target to have to add only a single dependency for nullable annotations instead of having to add two.
    • It makes it easier to package stand-alone libraries if we use our own annotation:
      • Not bundling them: Now users of the library need to also add a dep onto jspecify
      • Bundling them: Now users end up with conflicts if they also try and use jspecify
      • Renaming their java package when creating the .jar: adds complexity
      • Using our own annotations package seems by far the simplest IMO.

    The "managing dependencies" problem is not exactly unique, and systems like Maven have a way to solve it, but for only two small annotations, it hardly seems worth the overhead (and we've got more than just Maven we need to be compatible with).

  6. msridhar commented on Feb 6, 2025

    @msridhar
    ContributorAuthor

    Sounds like it's hard to detect that our custom @Nullable is TYPE_USE? Would be really slick if nullaway could automatically use "jspecify mode" for TYPE_USE annotations, and non-jspecify mode otherwise.

    The issue is that in places we need to modify javac Type objects to include annotations (e.g., here), and when we do that, I think we need to choose some annotation that is in the classpath. I can tinker and see if it's possible to use some dummy internal annotation provided by NullAway instead.

    My rationale for using a custom annotation:

    • NullAway has other annotations as well (@Contract, @EnsuresNonNull, etc), and it's convenient for build target to have to add only a single dependency for nullable annotations instead of having to add two.

    • It makes it easier to package stand-alone libraries if we use our own annotation:

      • Not bundling them: Now users of the library need to also add a dep onto jspecify
      • Bundling them: Now users end up with conflicts if they also try and use jspecify
      • Renaming their java package when creating the .jar: adds complexity
      • Using our own annotations package seems by far the simplest IMO.

    The "managing dependencies" problem is not exactly unique, and systems like Maven have a way to solve it, but for only two small annotations, it hardly seems worth the overhead (and we've got more than just Maven we need to be compatible with).

    While I totally get it in general (and NullAway itself really tries to avoid adding new deps for exactly these reasons), for JSpecify in particular I think it is extremely unlikely that users will ever end up with conflicts if you bundle its current annotations. @cpovirk could perhaps comment further, but I cannot imagine a scenario in which JSpecify introduces an incompatible version of these annotations in a future version (though new annotations may be added). So maybe bundling (without shadowing) is an ok solution in this case?

  7. agrieve commented on Feb 6, 2025

    @agrieve
    Contributor

    I can repro this warning even when jspecify is on the classpath. If having it on the classpath would fix the issue, that would be an easy thing to ensure (I can just add it when running the checks).

    Most android apps use R8 to do whole-program optimization, and it gets upset when given duplicate classes, even if they are the same.

  8. msridhar commented on Feb 6, 2025

    @msridhar
    ContributorAuthor

    Ok, got it. So I'll try to fix this issue, with the assumption that the JSpecify @Nullable annotation will be on the classpath. I'll also make NullAway fail in JSpecify mode if the JSpecify @Nullable annotation is not on the classpath; perhaps we will find a workaround in the future.

  9. agrieve commented on Feb 6, 2025

    @agrieve
    Contributor

    Sounds great!

  10. cpovirk commented on Feb 6, 2025

    @cpovirk

    The issue is that in places we need to modify javac Type objects to include annotations (e.g., here), and when we do that, I think we need to choose some annotation that is in the classpath. I can tinker and see if it's possible to use some dummy internal annotation provided by NullAway instead.

    Just as an aside, we have seen a similar problem with our JSpecify reference checker. In that case, we need an annotation on the classpath for the Checker Framework's purposes. I think that's different from your case, in which you're trying to produce instances of javac types more just because you ought to be able to use the javac types (and have successfully done so elsewhere) but can't in case of the weird javac behavior for some cases? I wish that I understood the Checker Framework dataflow model better to understand whether you might eventually need annotations on the classpath for it, too (perhaps for conversions between type variables and their bounds?). But hopefully it will be fine to require the JSpecify annotations on the classpath during nullness checking.

    In the reference checker, we ended up having users put a special jar of package-private annotations on the classpath for us to use. I don't love that approach, since a static analyzer would ideally live entirely on the processorpath. I will say that I've wondered if we could just use, say, @Override and @FunctionalInterface as our internal @Nullable and @NonNull: javac probably doesn't have any reason to validate that they're actually type annotations. (Now, I think the Checker Framework has some logic somewhere that is conditional on annotation @Target values, so that might not go well. But I don't think we'd be using the trick in that area of the code.)

  11. msridhar commented on Feb 7, 2025

    @msridhar
    ContributorAuthor

    In the end, I found a trick so that we don't even need the JSpecify @Nullable annotation on the classpath, at least for now. (Basically, we just use whatever @Nullable annotation type we found in the code: https://github.com/uber/NullAway/pull/1142/files#diff-ff27cbf7ec2f2f43a39e36ffc3a820771afadf439b803a5b1167d347ae316dceR75) As @cpovirk notes, as we add more JSpecify support we may run into a need to have those annotations on the classpath at some point. But for now, we have escaped 🙂

  12. agrieve commented on Feb 7, 2025

    @agrieve
    Contributor

    That's great! Thank you!

  13. added a commit that references this issue on Feb 7, 2025
    50cb4ab
  14. added a commit that references this issue on Mar 6, 2025
  15. added a commit that references this issue on Mar 7, 2025
  16. added a commit that references this issue on Mar 12, 2025
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