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
"The checker shouldn't produce errors on unannotated files" #77
(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:
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.
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.
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.
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.)
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)".
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.
.... hence why there are those that are lobbying to let `Map.get` return
unspecified nullness :). [ this is one reason for doing that anyway ]
With plain jspecify annotations, that is very likely to be an error.
I believe the intention was to leave it up to checkers to decide what to
do? I didn't think we would specify this to be an error for sure, or am I
missing something?
Nonetheless the point is certainly valid that there *will* be places where
annotations would suggest errors in unannotated code, and this will present
challenges for tools. One response, of course, being not to do anything by
default until the file itself is annotated.
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 :)
@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:
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.
Activity
cpovirk commentedon Oct 31, 2019
We can twist this into a way to create errors with APIs only, with no implementations:
I would like to think that that will never come up, but it probably will somewhere, hopefully by accident :)
mernst commentedon Nov 2, 2019
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.
cpovirk commentedon Nov 2, 2019
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:
cpovirk commentedon Nov 13, 2019
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 writenullinto the array, which isn't valid whether the parameter type isList<? extends @NotNull Foo>orList<? 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 unannotatedList<? extends Foo>might be@Nullableinstead of@NotNull.)amaembo commentedon Jan 31, 2020
I think the sophisticated checker is free to infer annotations, even if not present in the code. E.g.:
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)".
cpovirk commentedon Apr 28, 2020
It occurs to me that checkers can find errors even in code that's entirely reasonable:
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.
kevin1e100 commentedon Apr 28, 2020
cpovirk commentedon Apr 28, 2020
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.getspecifically, 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 :)
cpovirk commentedon Oct 12, 2020
@kevin1e100 , @netdpb , and I had discussed this a bit last week, coming to similar conclusions as above:
@Nullablereturn types, etc.)But I did just realize that there is an edge case in which APIs could break:
Tools could defensibly produce an error for
Sub, since it inherits incompatible signatures forfoo(). This is true even though:I would imagine that this case is very rare. And it should come up only when someone adds
@Nullableto 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.