You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@NullMarked (and its negation) includes module and package targets #34
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.
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.
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.
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.
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.
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.
@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.
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
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.
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.
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.]
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.
Reacted by Stephan Schroevers, Chris Povirk, Manu Sridharan, Kevin Bourrillion and Kevin Bierhoff
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
I made an accidental discovery: I could use @NullMarked on a method even before we addedMETHOD to its @Target... because it had MODULE in its @Target?!
$ ~/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.
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.
Reacted by Chris Povirk, Sébastien Deleuze, Kevin Bourrillion, Manu Sridharan, David P. Baker, Werner Dietl and Lázaro Clapp
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.
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
Activity
kevinb9n commentedon Jun 21, 2019
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.
cushon commentedon Jun 21, 2019
To start enumerating those:
package-infodoesn't work well with split packages: apackage-infoon 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 apackage-infoon 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 apackage-info.module-infosolves 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.kevinb9n commentedon Jun 21, 2019
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.
PeteGillin commentedon Jun 25, 2019
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.
kevinb9n commentedon Jun 26, 2019
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.
PeteGillin commentedon Jun 27, 2019
(Credit for highlighting the refactoring issue should go to Neil Fuller.)
kevinb9n commentedon Jun 27, 2019
I will try to pull together a full summary of all the pros/cons for package-level defaults.
kevin1e100 commentedon Jun 29, 2019
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.
donnerpeter commentedon Jul 2, 2019
@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.
abreslav commentedon Jul 8, 2019
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
abreslav commentedon Jul 8, 2019
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"?
I think this should be in sync with how we treat illegally annotated entities. Do we have an issue for that?
kevin1e100 commentedon Jul 8, 2019
sdeleuze commentedon Jul 8, 2019
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.
cpovirk commentedon Jul 8, 2019
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) ]
cpovirk commentedon Jul 8, 2019
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:
Not to mention common but not universal annotations, which could include:
I wonder if this leads us down the road of wanting...
[edit: Oops, I see that that's #35, mentioned above.]
60 remaining items
kevinb9n commentedon Jan 18, 2022
Propose: we will include MODULE and PACKAGE in the targets, and
Please view this issue on github and add a comment or even just an emoji reaction! to express your feedback.
netdpb commentedon Feb 2, 2022
Looks like we have consensus.
[-]@NullMarked (and its negation) to include module and package targets[/-][+]@NullMarked (and its negation) includes module and package targets [working decision: yes][/+]cpovirk commentedon Aug 9, 2022
I made an accidental discovery: I could use
@NullMarkedon a method even before we addedMETHODto its@Target... because it hadMODULEin its@Target?!I do get the expected error with a newer javac:
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
@NullMarkedon methods anyway :)The not-as-fine part is that this same bug also allows
@NullMarkedon 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.netdpb commentedon Apr 9, 2024
The current, working decision, as of 2022-03-20, is that users can use
@NullMarkedon modules and packages, and@NullUnmarkedon 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.
cpovirk commentedon Apr 9, 2024
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!
netdpb commentedon May 21, 2024
Decision:
@NullMarkedcan be applied to modules and packages, and@NullUnmarkedon packages.[-]@NullMarked (and its negation) includes module and package targets [working decision: yes][/-][+]@NullMarked (and its negation) includes module and package targets[/+]