Skip to content

Rules for type inference #705

Open
Open

Description

@cpovirk

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.

Activity

  1. added
    specification
    on Nov 8, 2024
  2. cpovirk commented on May 8, 2025

    @cpovirk
    CollaboratorAuthor

    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:

    • 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.
  3. cpovirk commented on Oct 8, 2025

    @cpovirk
    CollaboratorAuthor

    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.

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

    specification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions