Skip to content

@NullMarked (and its negation) includes module and package targets #34

Closed

Description

@kevinb9n

Filing this so we can punt on it. [edit: haha no]

Trying to accommodate package- or module-level defaults is fraught with issues, so for now, we are going to require an annotation like @DefaultNotNull to appear on every class.

We know people aren't going to like this. So this issue is about continuing to try to figure something out eventually.

Activity

  1. kevinb9n commented on Jun 21, 2019

    @kevinb9n
    CollaboratorAuthor

    Note that if we solve #35 then we can at least keep it down to just ONE annotation people put on every class, like @MyPreferredDefaults.

  2. cushon commented on Jun 21, 2019

    @cushon
    Collaborator

    fraught with issues

    To start enumerating those:

    • package-info doesn't work well with split packages: a package-info on the classpath applies to all classes in that package on the classpath, regardless of which jar those classes were loaded from. So it's easy to forget to include a package-info on a classpath if a package is split across multiple jars, or to accidentally change the defaults for a library that shares a package with another library that defines a package-info.

    • module-info solves the split package problem (packages and modules have to be 1:1), but it will be a long time before module adoption is widespread enough for a solution that depends on modules to be sufficient.

  3. kevinb9n commented on Jun 21, 2019

    @kevinb9n
    CollaboratorAuthor

    It is also very uncomfortable that a package-info present at compile time may be different or missing when a dependent is compiled, and perceived nullabilities of symbols can change. We want class files to be more self-contained than that.

  4. PeteGillin commented on Jun 25, 2019

    @PeteGillin
    Collaborator

    There's also the refactoring issue: if you move a class to a package with a different package-level default then you have to remember to add a class-level default to preserve the semantics.

  5. kevinb9n commented on Jun 26, 2019

    @kevinb9n
    CollaboratorAuthor

    Very good one. It's bad enough when moving a class causes or removes warnings in that class itself, but when it changes the meaning of things in that class as seen by other classes, that's pretty bad.

  6. PeteGillin commented on Jun 27, 2019

    @PeteGillin
    Collaborator

    (Credit for highlighting the refactoring issue should go to Neil Fuller.)

  7. kevinb9n commented on Jun 27, 2019

    @kevinb9n
    CollaboratorAuthor

    I will try to pull together a full summary of all the pros/cons for package-level defaults.

  8. kevin1e100 commented on Jun 29, 2019

    @kevin1e100
    Collaborator

    I'd like to propose allowing @WantDefaultForEveryClass (or something) on modules and packages, with the following syntactic rule: if this annotation is present then a defaulting annotation must be present on all top-level classes in its scope. This rule I believe addresses concerns around split packages, lost package-info.class files, and refactoring, simply because the intended default is evident in every class, and package-info and module-info files can therefore be ignored in bytecode. I also feel this is nice for readers, as they never have to look outside of a compilation unit to determine if a default is in effect.

    A possible variant here is to let people just use one of the defaulting annotations on packages, but require the same or a different defaulting annotation to be placed on very top-level class in scope. I like the economy of avoiding an extra annotation but there could be confusion when the rule about top-level classes isn't followed/enforced.

  9. donnerpeter commented on Jul 2, 2019

    @donnerpeter
    Collaborator

    @kevin1e100 thanks, an interesting approach! It indeed solves the issue of a source file being self-contained.

    Will we require all checkers to check this syntactic rule? Then we still must specify what the scope is exactly, and package-splitting issue will remain.

  10. abreslav commented on Jul 8, 2019

    @abreslav

    I like the economy of avoiding an extra annotation but there could be confusion when the rule about top-level classes isn't followed/enforced.

    I think our job is to enforce it :) Adding more annotations is not a total show-stopper, but where we can make it clear for the users what's expected of them through making checkers complain, there's no risk of misunderstanding

  11. abreslav commented on Jul 8, 2019

    @abreslav

    Another question that's connected to enforcing: what do we do if the checker encounters a class that violates the "want default for every class rule"?

    • Do we complain with an error and then consider it legacy?
    • Or complain and just not check it until it has a default?

    I think this should be in sync with how we treat illegally annotated entities. Do we have an issue for that?

  12. kevin1e100 commented on Jul 8, 2019

    @kevin1e100
    Collaborator
  13. sdeleuze commented on Jul 8, 2019

    @sdeleuze
    Collaborator

    As discussed today, I think it is important to support package-level defaults since it is the only realistic way to set defaults (like the very popular non-null one) with a scope which is wide enough to avoid cluttering the codebase with class level defaults. Ease of use is important even at the prototype stage, and lack of such feature would strongly impact adoption IMO. Defining that only at module level is not an option given the very low adoption.

    But it is likely doable to avoid issues by introducing some constraints related to modules which are now widely adopted best practices even by libraries that does not use modules yet. In practice, most of the JVM ecosystem tries to be a good citizen and avoid providing classes with the same package in 2 different JARs. This seems to be a pattern much more widely seen than the module support itself, so I tend to think we could provide that as a rule to follow in the specification.

    For the issues related to tests, I think this should not be a showstopper since the scope of the specification is limited to public API. While tests have of course a public API, it is not part of the application or library public API so could maybe be excluded from the scope of the annotations.

  14. cpovirk commented on Jul 8, 2019

    @cpovirk
    Collaborator

    One concern about package defaults that I thought I remembered seeing was from someone working on incremental compilation -- presumably an IDE, perhaps even IntelliJ? The concern was over recognizing that files in a package should be recompiled when its package-info is changed. I haven't managed to dig up the reference to this, though.

    [ edit: maybe #38 (comment) ]

  15. cpovirk commented on Jul 8, 2019

    @cpovirk
    Collaborator

    If we were to decide against support for package-level defaults, presumably that decision would apply not just to nullness but to all annotations. That might lead to a world in which libraries like Guava end up starting every class like this:

    @DefaultNotNull
    @MustUseResult

    Not to mention common but not universal annotations, which could include:

    @Immutable
    @GwtCompatible
    @PublishParameterNames

    I wonder if this leads us down the road of wanting...

    @GuavaDefaultAnnotations // defined to imply our preferred set :(

    [edit: Oops, I see that that's #35, mentioned above.]

  16. 60 remaining items

  17. kevinb9n commented on Jan 18, 2022

    @kevinb9n
    CollaboratorAuthor

    Propose: we will include MODULE and PACKAGE in the targets, and

    • When users use modules, these should have relatively well-defined behaviors (and see Spec/artifact issues specific to Java modules #222 for any issues that may arise with this)
    • When they don't, many problems can result from using package annotations in this way, but we believe these problems are addressable in various ways (not necessarily filed yet), and don't add up to an argument to actually not support package annotations.

    Please view this issue on github and add a comment or even just an emoji reaction! to express your feedback.

  18. netdpb commented on Feb 2, 2022

    @netdpb
    Collaborator

    Looks like we have consensus.

  19. changed the title [-]@NullMarked (and its negation) to include module and package targets[/-] [+]@NullMarked (and its negation) includes module and package targets [working decision: yes][/+] on Mar 21, 2022
  20. cpovirk commented on Aug 9, 2022

    @cpovirk
    Collaborator

    I made an accidental discovery: I could use @NullMarked on a method even before we added METHOD to its @Target... because it had MODULE in its @Target?!

    $ tail -n +1 *.java
    ==> Anno.java <==
    import static java.lang.annotation.ElementType.MODULE;
    
    import java.lang.annotation.Target;
    
    @Target(MODULE)
    @interface Anno {}
    
    ==> Foo.java <==
    class Foo {
      @Anno
      void go() {}
    }
    
    $ javac Foo.java Anno.java
    # no errors?!
    
    $ javac -version
    javac 11.0.13

    I do get the expected error with a newer javac:

    $ ~/jdk-19-ea+26/bin/javac Foo.java Anno.java
    Foo.java:2: error: annotation interface not applicable to this kind of declaration
      @Anno
      ^
    1 error
    

    I didn't immediately find a bug about this upstream. [edit: But it looks like it was fixed in JDK 16.]

    Anyway, that's fine because now we allow @NullMarked on methods anyway :)

    The not-as-fine part is that this same bug also allows @NullMarked on fields, parameters, and local variables (and probably packages, but I didn't check), which we don't want. (It doesn't allow it in type-use-only locations, at least.)

    I don't think that rises to the level that should make us actually avoid @Target(MODULE) (even though this isn't the first problem we've seen with @Target(MODULE) :)). But it does give tool authors more reason to write code to explicitly disregard annotations in locations where they aren't expected—and to issue errors when they see source code with such annotations.

  21. added
    design
    and removed on Nov 30, 2022
  22. modified the milestones: 1.0, 0.3 on Dec 2, 2022
  23. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    The current, working decision, as of 2022-03-20, is that users can use @NullMarked on modules and packages, and @NullUnmarked on packages, even though there are pitfalls. The pitfalls are common to all annotations used at those levels, however, not specific to JSpecify's.

    The argument for changing this decision is that the pitfalls are interesting, including the fact that package-level annotations apply to split packages, including tests, and the fact that module-level annotations apply only when the artifact is installed in the module-path by clients, and not in the classpath.

    However, because the pitfalls are not JSpecify-specific, we should allow the obvious application at these levels.

    This must be decided before version 1.0 of the jar.

    I propose the current decision be finalized for 1.0. If you agree, please add a thumbs-up emoji (👍) to this comment. If you disagree, please add a thumbs-down emoji (👎) to this comment and briefly explain your disagreement. Please only add a thumbs-down if you feel you can make a strong case why this decision will be materially worse for users or tool providers than an alternative.

    Results: Seven 👍; no other votemoji.

  24. cpovirk commented on Apr 9, 2024

    @cpovirk
    Collaborator

    I agree (and I have given my thumbs up), but I feel obligated call attention to one more pitfall that I don't think came up on this particular thread:

    Some users expect for annotations on a package to apply to subpackages, or at least they wonder if that might be the behavior. I expect us to finalize our working decision that annotations do not apply to subpackages (#8). Regardless of what we decide, some users will have different expectations than that. I think that's unfortunate but not a reason to pull back from supporting package-level annotations at all.

    Please still see the proposal and vote above!

  25. netdpb commented on May 21, 2024

    @netdpb
    Collaborator

    Decision: @NullMarked can be applied to modules and packages, and @NullUnmarked on packages.

  26. changed the title [-]@NullMarked (and its negation) includes module and package targets [working decision: yes][/-] [+]@NullMarked (and its negation) includes module and package targets[/+] on May 21, 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