Repository navigation
Description
We say that this is illegal. But we might need to say more about it someday.
In our docs about specific design issues, we have grown fond of saying:
For any syntax that is legal Java, we don't have the option to simply declare it to be illegal. Since we will have to deal with code that was compiled outside the purview of compliant checkers anyway, we still need to decide how to deal with it.
That's particularly relevant for @Nullable @NonNull Foo: For the other cases in which we declare say an annotation is placed "illegal," checkers can just ignore the annotation, since it's appearing in a bogus place. But here, the annotations are in a valid place: There are just too many of them :)
It's even possible to imagine that this case will arise in practice -- beyond just from human error (which has produced @Nullable @NonNull a few times in Google's codebase :)). Consider a code generator that is given the following type...
interface Foo<T> {
@Nullable T get();
}...and asked to generate an implementation of Foo<@NonNull T>. It might well apply naive substitution rules and generate:
@Nullable @NonNull T get() { ... }Maybe it "shouldn't" (and I haven't tried to find if any tools actually do this). But the only alternatives are:
- to mostly give up on type annotations (which is what most tools do, like AutoValue)
- to have special handling for our nullability annotations
A couple possibilities:
First, maybe @NonNull @Nullable should be treated as @NullnessUnspecified. This raises a couple questions:
-
Should checkers allow writing
@NonNull @Nullablein code they're checking? I would say no. (But I was briefly tempted to say that we could omit@NullnessUnspecifiedentirely by telling users to write@NonNull @Nullableinstead :)) -
Is it really sensible to interpret
@NonNull @Nullableas@NullnessUnspecified, particularly on type-variables usages, where@NonNullis weird (Annotating type parameters and type variables without using default annotations #89Handling of type-use annotations on type variables #91)?
The other possibility -- my favorite, as usual :) -- is that we pick the "most nullable" specifier. So we'd treat @Nullable @NonNull like plain @Nullable. (Maybe this feels a little more natural in the case of treating @Nullable @NullnessUnspecified as @Nullable?)
Activity
mernst commentedon Apr 4, 2020
I agree with our current position that incompatible annotations at a location should be illegal.
@NonNulland@Nullableshould have clear definitions, and those definitions are inconsistent. Adding special rules for when they are both present will muddy the waters and will make it difficult or impossible for checking tools to do their job.There are other situations where we declare legal Java syntax to be illegal. One example is when a method override does not satisfy behavioral subtyping. So we should feel free to do so here.
I don't think we want to lose an important opportunity to find problems in client code, just to handle some tools that might do naive substitution. I think it is better to notify users of the problem. This will enable users to work around the problems or at least inform them of the potential problem. This may also nudge the code generation tools to become more sophisticated.
(Related: #4.)
I have concerns about this proposal.
An alternative proposal is to leave
@Nullable @Nonnullundefined, and permit each checking tool to issue a warning or treat it unsoundly, at their preference.cpovirk commentedon Apr 6, 2020
Thanks. There are a lot of things that deserve a response here, but to focus on one:
I think we all agree that checkers can (and probably should, though we don't need to mandate it) treat a source file with
@Nullable @NonNull Fooas an error.The remaining concern is that people might write that in code that they compile with a stock compiler. Then our tools might see it in class files.
I suspect that most of us aren't going to want for tools to issue outright errors for problems in libraries used by the code that it's checking, so we need to decide what they should do.
Our assumption so far has been that we should define the semantics for anything that we might find in class files, even if our tools would have refused to produce those class files. I am not personally that invested in that assumption: I could probably tolerate leaving cases that are "perverse enough" as undefined, including this one. But it would be a shift in our approach.
@kevinb9n , who I believe wrote our assumption that we need to specify the semantics of any valid Java class files.
kevinb9n commentedon Apr 13, 2020
I suppose we could consider the idea that some annotationings are SO bogus that we would even refuse to compile a file that depended on a file that did that. And maybe
@Nullable @NonNullwould safely be in that category. But surely not everything should be. We hadn't really talked about this idea before, instead just saying "we'll have to do something well-defined (when compiling the dependent code) no matter what".kevinb9n commentedon Apr 13, 2020
If we DO want to have some forgiving treatment of
@Nullable @NonNullthen I'd suggest we can do a thought experiment by analogy: what if a Java variable was by some mysterious means given two types: bothNumberANDInteger?This is a bogus scenario, but if we had to choose, wouldn't we simply consider that to be of type
Integer, since it satisfies both? I think we would do that (whether in an assigning-from or assigning-to context). That would suggest letting@NonNulltake precedence over@Nullable.cpovirk commentedon Apr 13, 2020
Interesting! I see the argument there. At the same time, I have bought pretty deeply into the idea of annotations as only ever "adding nullness" (hence many of my reservations around
@NonNull). The behavior of@Nullable @NonNull Foois probably yet another thing that hinges on that idea.cpovirk commentedon Apr 13, 2020
(I have been meaning to get out of the habit of saying things like "additive nullness" and into the habit of saying "Kotlin-style nullness." I mean the same thing, but I think the latter sounds friendlier :))
kevin1e100 commentedon Apr 13, 2020
kevinb9n commentedon May 8, 2020
#101 is a proposed mechanism for making this combination illegal.
kevinb9n commentedon May 8, 2020
I have definitely moved away from "if it's legal Java we have to make a best effort" to "it's okay to define some things as beyond the pale".
@Nullable @NonNullbeing a poster child for the latter.kevinb9n commentedon May 20, 2020
I'm thinking that any time spent trying to decide the right graceful way to handle this baffling scenario would be time wasted.
We should specify that
@Nullable @NonNullis an illegal combination of annotations and at least strongly encourage checkers to refuse to analyze code that even depends on such a monstrosity.kevinb9n commentedon Sep 1, 2022
Despite the "time wasted" pronouncement above, we reconsidered this in #242.