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
As we note periodically, we haven't gotten much into implementation code at all. Part of the possible work there is to extend our existing rules for determining the types of elements—things like catch parameters. Another possible part of the work is to specify how dataflow works, but my guess is that we're not going to find that to be a priority when tools generally have their own implementations, such as the Checker Framework's.
And another possible part of the work is to specify how type inference works. The Checker Framework has an implementation, and I'm pretty sure that Eclipse and IntelliJ have their own, too. But it seems likely that at least some of those could be improved and that some tools will be writing their own from scratch.
Type inference is possibly the scariest part of the JLS :) There are parts of that that won't apply to nullness. Notably, we don't have to perform overload resolution, only check whether the selected overload is safe. But I'm sure there's still a lot to be done.
I will make a random note here that (as I may have mentioned in the spec at one point but then deleted?) type inference is probably another case in which all tools need to perform some "all-worlds" checks. Or in fact, I wonder if we need an additional variant of all-worlds checks, one that treats two unspecified-nullness types as equivalent (in contrast to the usual policy from #69).
My final random note is that @PolyNull would require some additional rules.
The Checker Framework implemented improved type inference (more closely patterned off the JLS, IIUC) in 3.43.0. They've since made some fixes, though we at Google still need to minimize and report some issues upstream before we can upgrade.
One somewhat obvious thing that I could have mentioned earlier but didn't: "Normal" Java compilation forces our hand somewhat:
It picks among the available overloads.
It selects the base types for all types that it infers.
This implies that we can write code that could pass both "normal" and nullness type checking if the "normal" type checking were to incorporate nullness information but that does not pass in practice.
Such a problem with the first bullet is fairly easy to imagine: Just picture a call to foo(null) when we have both foo(@NonNull String) and foo(@Nullable Object). "Normal" compilation will pick the String overload, but null isn't valid there. If only normal compilation were to pick the Object overload, then nullness checking could pass. (IIRC, Kotlin incorporates nullness from the beginning. I know we've seen a handful of unfortunate outcomes from differences in Kotlin and Java overload resolution, but I don't remember offhand if any of them were caused by this.)
The second bullet probably more relevant to tool authors: They frequently aren't doing overload resolution themselves, just analyzing the method that the normal compilation has already picked. But they do still need to do type inference from there on. They just have to limit themselves to the base types that have already been chosen, leaving them only the ability to infer non-null/nullable/parametric/unspecified(?) for each type. We have an example of "could pass checking if normal inference incorporated nullness" in our samples.
One other note: I had at one point hoped that we could at least have relatively straightforward rules for what the constraints to be fulfilled are. For example, "If we dereference the parameter x, then x < MINUS_NULL. (I'm already oversimplifying, since there's complexity around the fact that NO_CHANGE may or may not be a subtype of MINUS_NULL for a given type, but I knew that.) But obviously it's worse than that: A dereference of x can be fine if it's guarded by if (x ! = null). So we end up needing to do dataflow for the lambda, and that interacts with the dataflow for the surrounding code. This is, I gather, how the Checker Framework works, and I believe it's how kevin1e100 did things in google/kotlin_convert#12.
Activity
cpovirk commentedon May 8, 2025
The Checker Framework implemented improved type inference (more closely patterned off the JLS, IIUC) in 3.43.0. They've since made some fixes, though we at Google still need to minimize and report some issues upstream before we can upgrade.
IntelliJ IDEA now has an issue about nullness type inference more broadly.
One somewhat obvious thing that I could have mentioned earlier but didn't: "Normal" Java compilation forces our hand somewhat:
This implies that we can write code that could pass both "normal" and nullness type checking if the "normal" type checking were to incorporate nullness information but that does not pass in practice.
foo(null)when we have bothfoo(@NonNull String)andfoo(@Nullable Object). "Normal" compilation will pick theStringoverload, butnullisn't valid there. If only normal compilation were to pick theObjectoverload, then nullness checking could pass. (IIRC, Kotlin incorporates nullness from the beginning. I know we've seen a handful of unfortunate outcomes from differences in Kotlin and Java overload resolution, but I don't remember offhand if any of them were caused by this.)cpovirk commentedon Oct 8, 2025
One other note: I had at one point hoped that we could at least have relatively straightforward rules for what the constraints to be fulfilled are. For example, "If we dereference the parameter
x, thenx < MINUS_NULL. (I'm already oversimplifying, since there's complexity around the fact thatNO_CHANGEmay or may not be a subtype ofMINUS_NULLfor a given type, but I knew that.) But obviously it's worse than that: A dereference ofxcan be fine if it's guarded byif (x ! = null). So we end up needing to do dataflow for the lambda, and that interacts with the dataflow for the surrounding code. This is, I gather, how the Checker Framework works, and I believe it's how kevin1e100 did things in google/kotlin_convert#12.