Skip to content

"The checker shouldn't produce errors on unannotated files" #77

Closed

Description

@cpovirk

(That is: It might produce warnings, which could fail analysis in "strict mode." But "lenient mode" should succeed.)

This is a principle we've discussed a couple times (link, link). From the second link:

I think this should follow directly from the fact that @NullUnspec as we define it communicates zero semantic nullness information. So if a checker fails for some reason, it cannot be due to information it got from our annotations, because there was no such information.

However: While a file might not be annotated, it might use APIs from other files that are. For example, assuming that Optional is annotated:

[@DefaultNullnessUnspecified]
class Foo {
  void bar() {
    Optional.of(null); // error, presumably?
  }
}

That's "only" implementation code. Still, it's worth keeping in mind that we may have a cleanup to do before we can enable implementation checking universally.

Alternatively, we could consider making the checker skip @DefaultNullnessUnspecified APIs -- or just demote errors to warnings? But probably we'd want to roll out the checking gradually instead.

That all seems fine, hopefully? Just something that I'd overlooked initially.

Activity

  1. cpovirk commented on Oct 31, 2019

    @cpovirk
    CollaboratorAuthor

    We can twist this into a way to create errors with APIs only, with no implementations:

    @DefaultNotNull
    interface A {
      List<String> getStrings();
    }
    
    @DefaultNotNull
    interface B {
      List<@Nullable String> getStrings();
    }
    
    @DefaultNullnessUnspecified
    interface Impl extends A, B {}
    // error? getStrings() can't satisfy both signatures

    I would like to think that that will never come up, but it probably will somewhere, hopefully by accident :)

  2. mernst commented on Nov 2, 2019

    @mernst
    Contributor

    I understand the goal of this project as defining the semantics of annotations, not defining the behavior of a code analysis.

    What a tool does with the annotations is orthogonal to the semantics of the annotations. There are a multitude of different possible tools that might be written. Some are sound, and some are unsound. Some are static, and some are dynamic. This project should not define the behavior of those tools.

  3. cpovirk commented on Nov 2, 2019

    @cpovirk
    CollaboratorAuthor

    Fair; I am, depending on how charitable you want to me, somewhere between "wrong" and "speaking in shorthand" :) If you haven't seen #73 yet, I'm interested in your thoughts if you get the chance.

    The more precise phrasing of this issue is something like:

    • We've previously made a claim like: "When analyzing an unannotated file with a checker that uses only our annotations and rules, the checker could not possibly detect any logically contradictions / type mismatches."
    • However, I have realized that this is not necessarily true because the checker might be reading annotations from other files that are used by the unannotated file.
    • If we think that's a problem, we could define our annotations to have different meanings when read from an unannotated file. However, I'm not sure it's a problem, and I doubt we'd want to change our definitions in that way.
    • Still, we may be interested in my realization: Taking off our spec-designer hats and putting on our tool-author and codebase-maintainer hats, we may want to consider the implications for us as we roll new nullness analyses.
    • And then, putting our spec-designer hats back on: If someday in the future we find ourselves saying, "One of the tradeoffs of such-and-such an option for such-and-such a decision is that it could permit checkers to identify type errors in unannotated code, oh, no!" then we can say "But that's not fundamentally different from the situation we're already in," and that may help us in our decision.
  4. cpovirk commented on Nov 13, 2019

    @cpovirk
    CollaboratorAuthor

    Another case in which we might detect a type mismatch on unannotated code is nullOutCertainValues(List<? extends Foo> c) from #60 (comment): The implementation of that method would write null into the array, which isn't valid whether the parameter type is List<? extends @NotNull Foo> or List<? extends @Nullable Foo>.

    Of course, one way to resolve this particular problem is to permit List<@Nullable ? extends Foo>, precisely as discussed in #60 :) (And then we'd have to be sure that our handling of unannotated code also considers that the lower bound of an unannotated List<? extends Foo> might be @Nullable instead of @NotNull.)

  5. amaembo commented on Jan 31, 2020

    @amaembo
    Collaborator

    I think the sophisticated checker is free to infer annotations, even if not present in the code. E.g.:

    class C {
      void foo(String s) {
        System.out.println(s.trim());
      }
    
      void bar() {
        foo(null);
      }
    }

    Clearly this code contains a problem, and checker may identify it and highlight as a warning or as an error which could be a preference of the checker or configured by checker options. I think, checker is free to provide any errors and warnings that are out of scope of this project. We can formulate this as "There are no mandatory errors/warnings that compliant checker must issue on completely unannotated source base (including all dependencies)".

  6. cpovirk commented on Apr 28, 2020

    @cpovirk
    CollaboratorAuthor

    It occurs to me that checkers can find errors even in code that's entirely reasonable:

    map.get(foo).dereferenceIt();

    With plain jspecify annotations, that is very likely to be an error.

    I still think that's fine. It's just a reminder to us at Google (and perhaps other organizations) that turning on naive null checking everywhere would require a ton of suppressions to avoid breaking builds.

  7. kevin1e100 commented on Apr 28, 2020

    @kevin1e100
    Collaborator
  8. cpovirk commented on Apr 28, 2020

    @cpovirk
    CollaboratorAuthor

    Sorry, yes, I continue to say "error" as shorthand for "type mismatch if the only information available to you is that from jspecify annotations," but I should stop doing that.

    And sorry, this whole issue is more about adoption of tools than it is about our annotations per se. I just wanted to make sure we were aware that rolling out universal nullness checking will produce such "mismatches" in unannotated code, and in fact it's likely to be common. Even if we use a tool that has smarts for Map.get specifically, this same problem is bound to arise frequently (as I believe you've said we've seen with Android SDK upgrades :))

    Maybe I should just close this issue, since I think we all agree on the facts. (Maybe we always did; I just thought I once detected an undercurrent to the contrary.) Given that, the issue is serving mainly as a forum for me to put my foot in my mouth :)

  9. cpovirk commented on Oct 12, 2020

    @cpovirk
    CollaboratorAuthor

    @kevin1e100 , @netdpb , and I had discussed this a bit last week, coming to similar conclusions as above:

    • Only implementation code can break. (Reasoning: Annotations on APIs can't be wrong if the APIs are unannotated :) In contrast, implementations can be broken because they can dereference @Nullable return types, etc.)
    • Fixes for broken implementation code can be made locally (without having to also update your own callers). (At the least, you have the option to suppress the warning, leaving your signature untouched. Other times, you might add a null check or something. Sometimes you will want to change your signature, but you don't need to immediately.)

    But I did just realize that there is an edge case in which APIs could break:

    @NullAware
    interface Super1 {
      List<String> foo();
    }
    
    @NullAware
    interface Super2 {
      List<@Nullable String> foo();
    }
    
    interface Sub extends Super1, Super2 {}

    Tools could defensibly produce an error for Sub, since it inherits incompatible signatures for foo(). This is true even though:

    • It is unannotated code.
    • It doesn't even declare the problematic method directly.

    I would imagine that this case is very rare. And it should come up only when someone adds @Nullable to a type in the supertype -- a change that is already potentially a breaking change and thus one that should be tested accordingly. So this may all be academic.

  10. added
    design
    on Mar 16, 2023
  11. added
    nullness
    on Mar 13, 2024
  12. added this to the 0.3 milestone on Mar 27, 2024
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

    designnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions